我实现了一个拦截器,它执行某些检查并将sendRedirect发送到某个页面。目前我正在测试它没有任何条件,只需简单地调用preHandle时sendRedirect。这是我的实施:
class HttpInterceptorMgr implements HandlerInterceptor {
public boolean preHandle(HttpServleRequest aRequest, HttpServletResponse aResponse, Object aHandler) {
try {
sendRedirect("/somepage");
return false;
}
catch(Exceptio n e) {
// Do something else
}
}
}
的web.xml
<servlet-mapping>
<servlet-name>myproject</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
....
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
的myproject-web的servlet.xml中
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:interceptors>
<bean class="org.mycompany.HttpInterceptorMgr" />
</mvc:interceptors>
applicationContextSecurityBeans.xml
<security:http auto-config="true" use-expression="true" access-denied-page="/user/denied">
<security:form-login
login-page="/login"
authentication-failure-url="login?error=true"
default-target-url="/homepage" />
<security:logout invalidate-session="true"
logout-success-url="/login"
logout-url="/j_sring_security_logout" />
</security:http>
我面临的问题是我的页面在循环中重定向。在firefox浏览器上,它说&#34;页面没有正确地重定向&#34;。我尾巴catalina.out并显示它多次进入预处理,其request.getRequestUrl为/somepage
。但我无法成功到达该页面。
答案 0 :(得分:0)
发生的事情是因为你的拦截器没有条件,即使重定向请求到“/ somepage”它再次发送重定向,这将在循环中进行。
您可以检查请求是否发往“/ somepage”,如果是,则不应再次重定向。
class HttpInterceptorMgr implements HandlerInterceptor {
public boolean preHandle(HttpServleRequest aRequest, HttpServletResponse aResponse, Object aHandler) {
try {
if(!aRequest.getRequestURL().toString().endswith("/somepage")) {
sendRedirect("/somepage");
return false;
}
}
catch(Exceptio n e) {
// Do something else
}
return true;
}
}