session超时或未登录时回到登录界面,如有frame跳出frame

本文介绍了一种通过自定义Filter实现登录验证的方法,并详细解释了如何处理不同场景下的页面跳转,包括跳出Frame限制及跨域登录界面的跳转。

1、写Filter类:例如LoginFilter:

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

 public void destroy() {
  // TODO Auto-generated method stub

 }

 public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain arg2) throws IOException, ServletException {
  HttpServletRequest request = (HttpServletRequest) arg0;
  HttpServletResponse response = (HttpServletResponse) arg1;
  
  /** login filter */
  //signed
  if(request.getSession().getAttribute("USER_NAME") != null ||
    request.getRequestURL().indexOf("login.jsp") > -1 ||
    request.getRequestURL().indexOf("login.action") > -1){
   arg2.doFilter(arg0, arg1);
  }else{
   HttpServletResponse resp = (HttpServletResponse) response;
   String requestType = request.getHeader("X-Requested-With");
   if (requestType != null
     && requestType.equalsIgnoreCase("XMLHttpRequest")) {
    resp.setStatus(401);
   } else {
    resp.sendRedirect(request.getContextPath()+"/main/login.jsp");
   }
  }
  
 }

 public void init(FilterConfig arg0) throws ServletException {
  // TODO Auto-generated method stub

 }

2、如果未登录,直接在地址栏上输入url时会跳转到登录界面。但是如果是超时,那么他的登录界面还在那个frame里。要跳出这个frame。那么要在login.jsp里加一段代码:

<script type="text/javascript">
if(window!=top){
 top.location.href=location.href;
}
</script>

3、logout在一个frame里,如果点logout也跳不出frame。

<SCRIPT type="text/javascript">
function logout(){
 document.form1.submit();
}
</SCRIPT>
</head>
<body>

<div class="h2">
 <table>
  <tr>
   <td class="h3"><div class="h3"></div></td>
   <td class="h4" align="right"><div class="h4">User&nbsp;:&nbsp;<span class="h61"><s:property value="#session.USER_NAME.email"></s:property></span></div></td>
   <td class="h5" align="center"><div class="h5">|</div></td>
   <td class="h6">
     <div class="h6">
      <a href="#" class="hand" onclick="logout()">Logout</a>
     </div>
   </td>
  </tr>
 </table>
 <form name="form1" action="logout.action" method="post"  target="_top"> </form>
</div>

</body>

加个form,并设置target为_top

 

 4.如果是用struts2,要在struts.xml 里的result加个type=redirect

<action name="logout" method="logout" class="com.tvunetworks.center.gs.web.action.LoginAction">
        <result name="success"  type="redirect">/main/login.jsp </result>
</action>


5.如果登录界面是在别的服务器,也就是说用户管理服务不是与你在同一个域。

http://user_ip/userservice

http://business_ip/business

你的business的用户是邮userservice,但userservice的登录界面你是不能改的。也就是说你不能在userservice的界面加_top。解决的办法是在自己的loginfilter里加。

把前面代码里的resp.sendRedirect(request.getContextPath()+"/main/login.jsp");

换成 goLogin(resp);


private void goLogin(HttpServletResponse resp) throws IOException{
  PrintWriter out=resp.getWriter();
  out.println("<html>");
  out.println("<script>");
  out.println("window.open('"http://user_ip/userservice"','_top')");
  out.println("</script>");
  out.println("</html>");
 }

 


static int StreamTrans_Take(StreamTransCtx_S *ep) { HttpStreamCtx_S *ctx = ep->ctx; int http_type = ctx->http_type; int ret = ERR_NONE; #ifdef TELEMETRY_SUPPORT TELEMETRY_HTTP telemetry_http; memset(&telemetry_http, 0, sizeof(telemetry_http)); HttpCtx_S *http_ctx = NULL; if (ctx) { http_ctx = (HttpCtx_S *)ctx->session_ctx; } #endif /* 1. 取帧(根据流类型从回放/avdm中取帧) */ #ifdef LOCAL_STORAGE_ENABLE if (http_type == eStreamTransType_Playback || http_type == eStreamTransType_Download) { ret = stg_playback_get_next_unit(ep->src_id, &ep->cursor, &ep->unit); } else #endif { ret = avdm_get_next_unit(ep->src_id, &ep->cursor, &ep->unit); if (ret == ERR_NONE || ret == -ERR_OW_GET_LATEST) { ep->is_attached = 1; } } /* 2. 取帧返回码处理 */ switch (ret) { case ERR_NONE: break; case (-ERR_OW_GET_LATEST): TRANSMIT_ERROR("take newest frame(%d)", ret); break; case (-ERR_FRAME_NOT_READY): return ret; case (-ERR_WRONG_PARAMS): case (-ERR_AVDM_NOT_READY): case (-ERR_BBUFFER_RELEASED): default: TRANSMIT_ERROR("take errno(%d)", ret); return ret; } #if (defined AUDIO_G711_16K_SUPPORT) if (TP_MEDIA_TYPE_AUDIO == ((FRAME_HEAD_S *)ep->unit.addr)->media_type) { ctx->ts_info.c_info.sampling_rate = ep->cursor.sample_freq_index; } #endif #if (defined AUDIO_G711_16K_SUPPORT) int decode_len = 0; int encode_len = 0; int iSessionID = ((HttpCtx_S *)ep->ctx->session_ctx)->iSessionID; int payload_len = avdm_frame_payload_len(ep->unit.len); unsigned char *ptr = avdm_frame_payload_ptr(ep->unit.addr); FRAME_HEAD_S *a_header = (FRAME_HEAD_S *)ep->unit.addr; if (a_header->media_type == TP_MEDIA_TYPE_AUDIO && g_audio_config.iSessionID[iSessionID]) { if ((ep->cursor.codec_type != TP_ENCODE_TYPE_AUDIO_G711A) || (AUDIO_SAMPLING_RATE_8000HZ != ep->cursor.sample_freq_index)) { /*decode*/ if (TP_ENCODE_TYPE_AUDIO_G711U == ep->cursor.codec_type) { decode_len = g711_decode(g_audio_config.audio_pcm_buffer, ptr, payload_len, G711_U_LAW); } else if (TP_ENCODE_TYPE_AUDIO_G711A == ep->cursor.codec_type) { decode_len = g711_decode(g_audio_config.audio_pcm_buffer, ptr, payload_len, G711_A_LAW); } if (decode_len <= 0) { TRANSMIT_ERROR("decode error"); return (-ERR_FRAME_NOT_READY); } /*downsample*/ if (AUDIO_SAMPLING_RATE_16000HZ == ep->cursor.sample_freq_index) { decode_len = audio_pcm_half_downsample(g_audio_config.audio_pcm_buffer, decode_len); } /* rencode G711alaw */ /* 当前仅对http预览、下载、回放做了兼容旧客户端的处理,没有对双向语音做兼容处理, * 且重采样是共用一块g_tmp_audio_buff,如果音频数据太大没能一次打包完,可能因为 * 多条连接取用的音频数据相互覆盖导致播放出来的声音异常. */ if(!g_tmp_audio_buff || last_malloc_size < (sizeof(FRAME_HEAD_S) + payload_len)) { if (g_tmp_audio_buff) { free(g_tmp_audio_buff); g_tmp_audio_buff = NULL; } g_tmp_audio_buff = malloc(sizeof(FRAME_HEAD_S) + payload_len); last_malloc_size = sizeof(FRAME_HEAD_S) + payload_len; } if (NULL != g_tmp_audio_buff) { memset(g_tmp_audio_buff, 0, last_malloc_size); memcpy(g_tmp_audio_buff, ep->unit.addr, sizeof(FRAME_HEAD_S)); encode_len = g711_encode(g_tmp_audio_buff + sizeof(FRAME_HEAD_S), g_audio_config.audio_pcm_buffer, decode_len, G711_A_LAW); ep->unit.addr = g_tmp_audio_buff; ep->unit.len = encode_len + sizeof(FRAME_HEAD_S); } else { TRANSMIT_ERROR("g_tmp_audio_buff is null"); return (-ERR_FRAME_NOT_READY); } } ctx->ts_info.c_info.sampling_rate = AUDIO_SAMPLING_RATE_8000HZ; ep->cursor.codec_type = TP_ENCODE_TYPE_AUDIO_G711A; } #endif /* 3. 回放类型取帧处理逻辑: 取到帧直接返回 */ if (http_type == eStreamTransType_Playback || http_type == eStreamTransType_Download) { return ERR_NONE; } /* 4. 预览类型取帧处理逻辑 */ FRAME_HEAD_S *header = (FRAME_HEAD_S *)ep->unit.addr; int is_video = 0; int is_iframe = 0; if (header->media_type == TP_MEDIA_TYPE_VIDEO) { is_video = 1; if (header->video_frame_type == TP_FRAME_TYPE_I || header->video_frame_type == TP_FRAME_TYPE_ONLY_DECODE_I) { is_iframe = 1; } } //1. 预览首帧需要为I帧 if (ctx->is_need_iframe) { if (!is_iframe) { return (-ERR_FRAME_NOT_READY); } ctx->is_need_iframe = 0; } //2. 预览播放延迟处理策略: 当前帧间落后于系统间超过阈值,使用跳帧策略 */ time_t now = time(NULL); time_t unit_time = (time_t)ep->unit.utc_time; if (unit_time + TREANSMIT_PREVIEW_DELAY_MAXTIME < now) { TRANSMIT_INFO("Preview is delay! jump! stream_id(%d) delay(%ds)", ep->src_id, (int)(now - unit_time)); #ifdef TELEMETRY_SUPPORT ds_read(TELEMETRY_HTTP_PATH, &telemetry_http, sizeof(TELEMETRY_HTTP)); if (http_ctx) { if (HTTP_STREAM_PREVIEW == http_ctx->iSessionStream) { if (http_ctx->web_connection) { telemetry_http.relay_view_delay_count++; } else if (http_ctx->p2p_connection) { telemetry_http.p2p_view_delay_count++; } else { telemetry_http.local_view_delay_count++; } ds_write(TELEMETRY_HTTP_PATH, &telemetry_http, sizeof(TELEMETRY_HTTP)); } } #endif avdm_detach_unit(ep->src_id, &ep->cursor); memset(&ep->cursor, 0, sizeof(avdm_cursor_t)); ep->is_attached = 0; ctx->is_need_iframe = 1; //是视频码流则强制I帧 if (is_video) { avdm_force_iframe(ep->src_id); } return (-ERR_FRAME_NOT_READY); } if (is_iframe) { TRANSMIT_DEBUG("frame type_I(%d) len(%dKB) time(%d) seq(%d)\n", is_iframe, ep->unit.len / 1024, (int)ep->unit.utc_time, ctx->seq); } return 0; } 这个函数[Transmit]take errno(-1) 这样报错是啥问题
最新发布
10-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值