Session超时跳转到指定页面(filter实现)解决了图片被过滤问题

package com.myplan.checkloginfilter;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**   
 * 用于检测用户是否登陆的过滤器,如果未登录或超时,则重定向到指的登录页面<p>     
 */
public class CheckLoginFilter extends HttpServlet implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) {
        try {
            HttpServletRequest hsr = (HttpServletRequest) request;
            HttpSession session = hsr.getSession();
            if (hsr.getRequestURI().startsWith(
                    hsr.getContextPath() + "/login.jsp")
                    || hsr.getRequestURI().startsWith(
                            hsr.getContextPath() + "/login.do")
                    || hsr.getRequestURI().equals(hsr.getContextPath() + "/")
                    ) {
                // 请求登陆页面,所以放过  
                System.out.println("放过login");
                filterChain.doFilter(request, response);
            } else {
                if (session.getAttribute("employeeEmail") != null) {
                    //这里session存在,那么则登陆,放过session  
                    // 已登录,放过请求  
                    System.out.println("test filter chain");
                    filterChain.doFilter(request, response);
                } else {
                    //session超期后,自然session就是空了,那么则跳转到指定的页面就可以了  
                    // 未登录,跳转到登录页  
                    
                    PrintWriter out = response.getWriter();
                    out.print("<script>window.top.location.href='http://blog.163.com/girl_lihuiyue@126/blog/"
                            + hsr.getContextPath() + "';</script>");
                }
            }
        } catch (ServletException sx) {
        } catch (IOException iox) {
        }
    }

    public void destroy() {

    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }
}
使用filter导致跳转到登陆页面的图片不显示。添加红色字体部分,跳过图片的过滤。

web.xml相应的配置

<filter> 
      <filter-name>CheckLoginFilter</filter-name> 
      <filter-class>com.myplan.checkloginfilter.CheckLoginFilter</filter-class> 
 </filter> 
  <filter-mapping> 
     <filter-name>CheckLoginFilter</filter-name> 
     <url-pattern>*.jsp</url-pattern>
   </filter-mapping> 
<span style="color:#FF0000;">如果使用session的话,配置
 <session-config> 
      <!-- 以分钟为单位 --> 
      <session-timeout>30</session-timeout> 
  </session-config></span>


好的,下面是前后端代码实现的思路: 后端代码: 1. 在BaseController中添加一个OnActionExecuting方法,用于判断用户是否登录,如果未登录,则跳转到登录页面。 ```csharp public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { HttpSessionStateBase session = filterContext.HttpContext.Session; if (session["UserId"] == null) { filterContext.Result = new RedirectResult("~/Account/Login"); } base.OnActionExecuting(filterContext); } } ``` 2. 在需要登录验证的Controller中,继承BaseController即可。 ```csharp public class HomeController : BaseController { public ActionResult Index() { return View(); } } ``` 前端代码: 1. 在前端页面中,使用`setInterval()`函数定时向后端发送一个心跳请求,以保持会话的活跃状态。 ```javascript setInterval(function () { $.ajax({ url: "/HeartBeat", type: "get", dataType: "json", success: function (data) { //处理返回的数据 }, error: function () { //处理请求失败的情况 } }); }, 300000); //5分钟发送一次请求 ``` 2. 在前端页面中,使用`window.addEventListener()`函数监听所有的ajax请求,如果返回的状态码为401,则跳转到登录页面。 ```javascript window.addEventListener('load', function () { $(document).ajaxComplete(function (event, xhr, options) { if (xhr.status == 401) { window.location.href = '/Account/Login'; } }); }); ``` 3. 在前端页面中,如果页面中嵌套了iframe,则在iframe加载完成后,判断iframe中的页面是否需要登录验证,如果需要,则向iframe中发送一个心跳请求。 ```javascript function checkIframeLogin() { var iframe = document.getElementById('myIframe'); if (iframe) { var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; var iframeUrl = iframeDoc.location.href; if (iframeUrl.indexOf('NeedLogin') != -1) { setInterval(function () { iframe.contentWindow.postMessage('HeartBeat', '*'); }, 300000); //5分钟发送一次请求 } } } ``` 在iframe中的页面中,添加一个监听事件,如果收到了来自父页面的心跳请求,则返回一个状态码,用于前端页面中的跳转。 ```javascript window.addEventListener('message', function (event) { if (event.data == 'HeartBeat') { $.ajax({ url: '/HeartBeat', type: 'get', dataType: 'json', success: function (data) { //处理返回的数据 }, error: function () { parent.window.location.href = '/Account/Login'; } }); } }); ``` 上述代码中,`parent.window.location.href = '/Account/Login';`表示在父页面跳转到登录页面。 希望以上代码可以帮助到您,如有任何疑问,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值