拦截器预处理不会重定向到登录页面

时间:2017-04-10 17:01:41

标签: jsp spring-mvc interceptor

如果用户登录ie浏览器并在登录后重定向到index.jsp,请复制此网址并粘贴到另一个浏览器chrome,这样它就会重定向到登录页面,以防止直接访问index.jsp 。 我引入了sessionInterceptor以防止直接访问index.jsp(应重定向到login.html)。代码正在使用preHandle()方法并运行所有代码,但在返回false后,它不会重定向到login.html。怎么了?

public class LoggingInterceptor implements HandlerInterceptor {
    public static Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class);



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();


        if (session.getAttribute("isUserLoggedIn") == null) {
         response.sendRedirect(request.getContextPath() + "/login.html");
         return false;
        }

        System.out.println(request.getSession() + "--Before Method ");
        System.out.println("---Before Method Execution---");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        System.out.println(request.getSession()+ "---post Method ");
        System.out.println("---post Method Execution---");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println(request.getSession() + "---after Method ");
        System.out.println("---after Method Execution---");

    }

}

0 个答案:

没有答案