Spring MVC 处理程序拦截器:重定向失败

Spring MVC HandlerInterceptor : Redirect Failed

我创建了一个简单的 WebMVC 应用程序 - 配置了处理程序拦截器。拦截器的职责很简单——它应该检查 HttpRequest 中是否存在有效会话——如果是,则重定向到注册页面。

我遇到的问题是在重定向时 - 浏览器抛出消息:

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.

拦截器代码如下:

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");

}

}

我试图将完整的请求上下文路径附加到重定向以及根上下文中,但都失败了。我看不出我在这方面失败的地方 - 所以非常感谢任何帮助。

检查您的拦截器配置,看它是否只接受某些 url 模式。

参见下面的示例。

<!-- 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>

无论您编写什么拦截器,都会对所有请求产生影响。

所以当它重定向到 /register 时它也会到达拦截器它会发现 session 不为空并再次重定向..同样的事情继续......无限重定向到同一个请求。

@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();
    }

}

所以确保它只会拦截 configurationcode

的一个请求
if((request.getRequestURI().equals(contextPath+"/someURI") && session != null){
    response.sendRedirect("register");
}

然后它将只检查特定请求并重定向到 register.It 不会陷入无限循环。