I have a spring application. I introduced a sessionInterceptor to prevent direct access to index.jsp. If the user is not logged in it shouldn't be able to access index.jsp and should be redirected to login.html. The code is hitting the preHandle() method and running all the code but after return false it's not redirecting to login.html. What's wrong? Any gurus out there for help? Thanks in advance.
My preHandle() in SessionInterceptor.java is:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
// if displaying the home page, make sure the user is reloaded.
if (request.getRequestURI().endsWith("login.html")) {
session.removeAttribute("isUserLoggedIn");
}
if (session.getAttribute("isUserLoggedIn") == null && !request.getRequestURI().endsWith("login")) {
response.sendRedirect(request.getContextPath() + "/login.html");
return false;
}
return true;
}
I have tried the following as well but all in vain.
RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/login.html");
dispatcher.forward(request, response);
My dispatcher-servlet.xml settings are:
The web.xml is:
dispatcher
/rest/*
login.html
解决方案
you can try redirecting to a logical path that will be catched from a controller
Try
response.sendRedirect("/NotLogged");
And then create a function like this:
@RequestMapping(value = {"/NotLogged"}, method = RequestMethod.GET)
public String notLogged() {
return "login.html";
}
I hope it will work for you!