Reputation: 315
I have created a simple WebMVC application - with handler interceptor configured. The interceptors responsibility is simple - it should check if a valid session exists in the HttpRequest - and if this is true, redirect to a registration page.
The issue i encounter is that on redirect - the browser is throwing the message:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
The interceptors code is as follows:
public class LoginInterceptor extends HandlerInterceptorAdapter{
// to be used checking session management for user.
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println(" <interceptor> - this is pre handle");
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println(" <interceptor> - this is post handle");
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
System.out.println(" <interceptor> - session id is --> " + session.getId());
System.out.println(" <interceptor> - session create time is --> " + session.getCreationTime());
System.out.println(" <interceptor> - session last accessed time is --> " + session.getLastAccessedTime());
try {
System.out.println(" <interceptor> - user info is --> " + user.toString());
}catch(Exception e){
e.toString();
}
try{
if(session != null){
response.sendRedirect("register");
return;
}
}catch(Exception e){
e.toString();
}
}
public void afterCompletetion(){
System.out.println(" <interceptor> - this is after completion");
}
}
I have attempted to append the full request context path to the redirect - as well as the root context - both of which fail. I cannot see where I am failing on this - so any help is most appreciated.
Upvotes: 0
Views: 6726
Reputation: 3431
Whatever the interceptor you have written will come into picture for all requests.
So when it will redirect to /register
that time also it will reach interceptor it will find that session
is not null and again redirect..and same thing continues...infinite redirection to same request.
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HttpSession session = request.getSession();
try{
if(session != null){ //Infinite
response.sendRedirect("register");
return;
}
}catch(Exception e){
e.toString();
}
}
So Make sure that it will intercept for only one request by configuration
or by code
if((request.getRequestURI().equals(contextPath+"/someURI") && session != null){
response.sendRedirect("register");
}
Then it will check only for particular request and redirect to register.It wont fell in infinite loop.
Upvotes: 5
Reputation: 76
Check your interceptors configuration to see if its accepting only certain url pattern.
See the example below.
<!-- Configures Interceptors -->
<mvc:interceptors>
<!-- This XML will intercept all URIs -->
<bean class="com.howtodoinjava.interceptor.DemoInterceptor"></bean>
<!-- This XML will apply interceptor to only certain URIs -->
<!--
<mvc:interceptor>
<mvc:mapping path="/users"></mvc:mapping>
<bean class="com.howtodoinjava.interceptor.DemoInterceptor"></bean>
<mvc:interceptor>
-->
</mvc:interceptors>
Upvotes: 0