Java Spring security 3.0:SavedRequest始终为空
在我的应用程序中,我使用的是Spring Security 3.0,在用于拦截“后身份验证”的类中,我有如下内容:Java Spring security 3.0:SavedRequest始终为空,java,spring,spring-security,Java,Spring,Spring Security,在我的应用程序中,我使用的是Spring Security 3.0,在用于拦截“后身份验证”的类中,我有如下内容: public class CustomAuthenticationHandler extends SavedRequestAwareAuthenticationSuccessHandler { public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse respons
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"/>
公共类CustomAuthenticationHandler扩展了SavedRequestAwareAuthenticationSuccessHandler{
AuthenticationSuccess(HttpServletRequest请求、HttpServletResponse响应、身份验证)上的公共void引发ServletException、IOException{
SavedRequest SavedRequest=新建HttpSessionRequestCache().getRequest(请求,响应);
...
}
}
...
当我从应用程序内的通用页面“X”登录时,我希望在成功验证后重定向到“X”,但我总是登录到主页。
上面的“savedRequest”变量始终返回null。是否缺少任何特定设置?如何获得正确的重定向?这实际上取决于您所说的“从应用程序内部的通用页面“X”登录”是什么意思 如果您的意思是您已经在查看该页面,然后选择访问登录页面(或者您在其中嵌入了一个登录表单),那么您将不会被重定向到页面“X”,因为Spring Security不知道您决定登录时所处的页面。您必须重定向到
Referer
标题给定的位置,或者将页面视图历史记录存储在应用程序逻辑中
SavedRequest
机制仅在您尝试访问受保护的资源并且Spring Security需要将您重定向到登录页面的情况下使用。它会临时记录位置,以便在登录后尝试恢复原始请求行为
如果“X”是受保护的页面,并且您试图查看它,则URL为“X”的
SavedRequest
将在会话中缓存,同时您将被重定向到登录页面 对于页面“X”,我指的是一个通用的不受保护的页面(所以你不需要登录就可以看到它)。这一页最上面有古典音乐。根据您在我的案例中所说的,我应该使用“Referer”标题或页面查看历史记录。“Referer”在onAuthenticationSuccess请求中对我不起作用。getHeader(“Referer”);==localhost:8080/登录名。对于普通的“a-href”,页面查看历史记录是唯一的解决方案?Referer仅适用于嵌入式登录表单。当Spring Security处理登录表单提交时,它无法知道单击登录链接时您在哪个页面上,因此您必须将该信息存储在某个地方。