java - Spring 安全3.0 : SavedRequest always null

标签 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。是否缺少任何特定设置?如何获得正确的重定向?

最佳答案

这实际上取决于“从应用程序内的通用页面“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/

相关文章:

java - 在启用安全性的 Spring 5 中加载静态资源时出现问题

java - 如何使用流从两个列表或数组乘法中查找元素对

java - 我如何将 mobicents 部署到 glassfish v3

Java8 : how to copy values of selected fields from one object to other using lambda expression

java - 使用 Hibernate 和 Spring 的 Swing 应用程序

maven - 缺少 Artifact org.springframework.security :spring-security-parent:jar:3. 0.7.RELEASE:compile

java - 如何导出java小程序

java - 在ExternalContext中找不到pages/index.xhtml作为资源

java - Spring + Thymeleaf 资源处理程序与 RequestMapping 冲突

java - 阻止通过spring boot应用程序中的url栏转到其他网页