在我的应用程序中,我使用 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。是否缺少任何特定设置?如何获得正确的重定向?
最佳答案
这实际上取决于“从应用程序内的通用页面“X”登录”的含义。
如果您的意思是您已经在查看该页面,然后选择访问登录页面(或者其中有嵌入的登录表单),那么您将不会被重定向到页面“X”,因为 Spring Security 不知道什么您决定登录时所在的页面。您必须重定向到 Referer
header 指定的位置,或者将页面 View 历史记录存储在您的应用程序逻辑中。
SavedRequest 机制仅在您尝试访问 protected 资源并且 Spring Security 需要将您重定向到登录页面的情况下使用。它会临时记录该位置,以便在登录后尝试恢复原始请求行为。
如果“X”是 protected 页面,并且您尝试查看它,则在您访问时,URL 为“X”的 SavedRequest
将被缓存在 session 中。重定向到登录页面。
关于java - Spring 安全3.0 : SavedRequest always null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9464631/