关于拦截器超时的使用

本文介绍了一个使用Spring MVC实现的拦截器案例,该拦截器用于处理用户登录验证及Session超时的情况。通过配置特定的URL映射规则,实现了对指定路径的访问控制,并对Session超时进行了有效管理。
摘要由CSDN通过智能技术生成

拦截器:

<!-- 拦截器集合 --> 
    <mvc:interceptors>
      <!-- mvc 登陆鉴权拦截器 -->   
      <mvc:interceptor>
       <!-- 需要拦截的URL -->
         <mvc:mapping path="/*/**" />
         <bean class="com.suning.rca.common.interceptor.AuthLoginInterceptor">
          <!-- 登陆页面 -->
            <property name="loginUrl" value="/index.html" />
           <!--放行URL配置 -->
            <property name="excludeList">
               <list>
                  <value>/login.do</value>
                  <value>/generateQrCode.do</value>
                  <value>/doLogin.do</value> 
                  <value>/checkDeviceLogin.do</value>
                  <value>/index.do</value>
                  <value>/error.do</value>
                  <value>/checkPosBindState.do</value>
                  <value>/logout.do</value>
               </list>
            </property>
         </bean>
      </mvc:interceptor>
       <!-- session超时 -->  
      <mvc:interceptor>  
          <mvc:mapping path="/*/**" />  
          <bean class="com.suning.sdipospc.interceptor.SessionTimeoutInterceptor">  
              <property name="allowUrls">  
                  <list>  
                      <!-- 如果请求中包含以下路径,则不进行拦截 -->  
                      <value>/login.do</value>
                      <value>/generateQrCode.do</value> 
                      <value>/doLogin.do</value> 
                      <value>/checkDeviceLogin.do</value>
                      <value>/error.do</value>
                      <value>/checkPosBindState.do</value>
                      <value>/logout.do</value>
                  </list>  
              </property>  
          </bean>  
      </mvc:interceptor>    
   </mvc:interceptors>       

拦截器

public class SessionTimeoutInterceptor implements HandlerInterceptor {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(SessionTimeoutInterceptor.class);

    @Autowired
    private LoginService loginService;  

    /**
     * 放行URL
     */
    private List<String> allowUrls;

    public void setAllowUrls(List<String> allowUrls) {
        this.allowUrls = allowUrls;
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        LOGGER.info("SessionTimeoutInterceptor.preHandle enter.");
        String requestUrl = request.getRequestURI().replace(
                request.getContextPath(), "");
        response.setContentType("text/html; charset=utf-8");
        HttpSession session = request.getSession(true);
        if (null != allowUrls && allowUrls.size() >= 1)
            for (String url : allowUrls) {
                if (requestUrl.contains(url)) {
                    return true;
                }
            }
        Object obj = session.getAttribute(SystemConstants.SEESION_IUSER);
        LOGGER.info("====session对象currentUser="+obj);
        LOGGER.info("====sessionId="+session.getId());
        if (obj == null) {
            String loginToken = CookiesUtil.getLoginTokenFromCookie(request);
            String userId = CookiesUtil.getLoginUserIdFromCookie(request);
            loginService.logout(loginToken, userId);
            loginService.operatorLog(WebposConstants.StringCon.TWO,userId);
            CookiesUtil.delCookie(request);
            CookiesUtil.delOperator(request);
            PrintWriter out = response.getWriter();
            StringBuilder builder = new StringBuilder();
            builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
//          builder.append("alert(\"页面过期,请重新登录\");");
            builder.append("window.top.location.href=\""
                    + SystemConstants.LOGIN_URL + "\"");
            builder.append("</script>");
            out.print(builder.toString());
            out.close();
        }
        // 返回true,则这个方面调用后会接着调用postHandle(), afterCompletion()
        return true; 
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        LOGGER.info("SessionTimeoutInterceptor.postHandle enter.");
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        LOGGER.info("SessionTimeoutInterceptor.afterCompletion enter.");
    }
}
web.xml
 <session-config>
    <session-timeout>15</session-timeout>
    <cookie-config>
        <path>/</path>
    </cookie-config>
  </session-config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值