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:
<bean id="sessionInterceptor" class="com.xxx.xxx.xxx.SessionInterceptor" />
<bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="sessionInterceptor" />
</list>
</property>
</bean>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.xxx.xxx.xxx.SessionInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
The web.xml is:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
you can try redirecting to a logical path that will be catched from a controller Try
And then create a function like this:
I hope it will work for you!