Spring security 3.0:SavedRequest始终为null

时间:2012-02-27 11:56:47

标签: java spring spring-security

在我的应用程序中,我使用的是Spring Security 3.0,并且在用于拦截“后验证”的类中,我有类似的东西:

public class CustomAuthenticationHandler extends SavedRequestAwareAuthenticationSuccessHandler {

   public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {

    SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
...
   }
}
<security:http>
    <security:form-login login-page="/login/" always-use-default-target="false"  authentication-success-handler-ref="customAuthenticationHandler" authentication-failure-url="/login/?login_error=1"/>
 ...
</security:http>

<bean id="customAuthenticationHandler" class="org.myproject.CustomAuthenticationHandler"/>

当我从应用程序内部的通用页面“X”登录时,我希望在成功验证后重定向到“X”,但我总是登陆主页。 上面的'savedRequest'变量返回null。是否缺少任何具体设置?如何才能获得正确的重定向?

1 个答案:

答案 0 :(得分:0)

这实际上取决于“从应用程序内部的通用页面登录”X“的含义。

如果您的意思是您已经在查看该页面,然后选择访问登录页面(或者您有一个嵌入式登录表单),那么您将不会被重定向到页面“X”,因为Spring Security不知道是什么您决定登录时所处的页面。您必须重定向到Referer标题给出的位置,或将页面视图历史记录存储在应用程序逻辑中。

SavedRequest机制仅用于您尝试访问受保护资源并且Spring Security需要将您重定向到登录页面的情况。它会临时记录该位置,以便它可以在登录后尝试恢复原始请求行为。

如果“X”是受保护的网页,并且您尝试查看该网页,那么当您被重定向到会话时,会话中会缓存网址为“X”SavedRequest 登录页面。