【问题标题】:Spring security 3.0: SavedRequest always nullSpring security 3.0:SavedRequest 始终为空
【发布时间】:2012-02-27 23:33:15
【问题描述】:

在我的应用程序中,我使用的是 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。是否缺少任何特定设置?如何获得正确的重定向?

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    这实际上取决于您所说的“从应用程序内的通用页面“X”登录”的意思。

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

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

    如果“X”是受保护的页面并且您尝试查看它,则在您被重定向到登录页面时,会在会话中缓存带有“X”URL 的 SavedRequest .

    【讨论】:

    猜你喜欢
    • 2021-08-02
    • 2018-09-30
    • 1970-01-01
    • 2013-05-15
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2015-11-21
    相关资源
    最近更新 更多