(转) ssm 拦截器验证用户登录session

public class CommonInterceptor extends HandlerInterceptorAdapter{
    private final Logger log = LoggerFactory.getLogger(CommonInterceptor.class);
    /*
     * 利用正则映射到需要拦截的路径

    private String mappingURL;

    public void setMappingURL(String mappingURL) {
               this.mappingURL = mappingURL;
    }
  */
    /**
     * 在业务处理器处理请求之前被调用
     * 如果返回false
     *     从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
     * 如果返回true
     *    执行下一个拦截器,直到所有的拦截器都执行完毕
     *    再执行被拦截的Controller
     *    然后进入拦截器链,
     *    从最后一个拦截器往回执行所有的postHandle()
     *    接着再从最后一个拦截器往回执行所有的afterCompletion()
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if ("GET".equalsIgnoreCase(request.getMethod())) {
            RequestUtil.saveRequest();
        }
        log.info("==============执行顺序: 1、preHandle================");
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestUri.substring(contextPath.length());

        log.info("requestUri:"+requestUri);
        log.info("contextPath:"+contextPath);
        log.info("url:"+url);

        User user =  (User) request.getSession().getAttribute("userbean");
        if(user == null){
            response.setCharacterEncoding("UTF-8");
            log.info("Interceptor:跳转到login页面!");
            request.getRequestDispatcher("login.do").include(request, response);
            return false;
        }else
            return true;
    }

    /**
     * 在业务处理器处理请求执行完成后,生成视图之前执行的动作
     * 可在modelAndView中加入数据,比如当前时间
     */
    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        log.info("==============执行顺序: 2、postHandle================");
        if(modelAndView != null){  //加入当前时间
            modelAndView.addObject("var", "测试postHandle");
        }
    }

    /**
     * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
     *
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     */
    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        log.info("==============执行顺序: 3、afterCompletion================");
    }

}


springMVC  配置文件

<mvc:interceptors>
    <mvc:interceptor>
        <!-- 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->
        <mvc:mapping path="/" />
        <mvc:mapping path="/main.do" />
        <bean class="com.hys.interceptor.CommonInterceptor"></bean>
    </mvc:interceptor>
    <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>

不拦截 配置

<!--不拦截的连接
            <mvc:exclude-mapping path="/login/load.do"/>-->


session 过期处理 iframe 跳转

HttpSession session = request.getSession();  
Object obj = session.getAttribute(Constant.LOGIN_USER);  
    if (obj == null) {  
        PrintWriter out = response.getWriter();  
        out.println("<html>");      
        out.println("<script>");      
        out.println("window.open ('"+request.getContextPath()+"/index.html','_top')");      
        out.println("</script>");      
        out.println("</html>");    
        return false;  
    }  



展开阅读全文

没有更多推荐了,返回首页