gpt4 book ai didi

Spring Security 记住我 RequireFully 授权后重定向

转载 作者:行者123 更新时间:2023-12-02 05:48:18 25 4
gpt4 key购买 nike

全部,

我正在尝试实现“记住我”功能

我有两个 protected 网址。

/操作/完全(用户必须经过完全身份验证,不允许记住我)

/operation/authenticated(记住我,好吗)

如果我没有记住我的 cookie,并且我访问任一 URL,系统会提示我输入凭据并重定向到原始 URL,生活会很好。

如果我处于记住我模式,我可以毫无问题地导航到/操作/已验证。如果我导航到/操作/完全,我将被重定向到登录页面。然后我进行身份验证并返回到“/”,我想返回到我的原始目标/操作/完全。

<http auto-config="true" use-expressions="true" access-denied-page="/login">
<form-login login-page="/login"
login-processing-url="/static/j_spring_security_check"
authentication-failure-url="/login" />
<logout logout-url="/j_spring_security_logout" logout-success-url="/logout"/>
<intercept-url pattern="/favicon.ico" access="permitAll" />
<intercept-url pattern="/operations/fully" access="hasRole('ROLE_USER') and isFullyAuthenticated()"/>
<intercept-url pattern="/operations/authenticated" access="hasRole('ROLE_USER')"/>
<intercept-url pattern="/login" />
<remember-me key="myKey"
token-validity-seconds="2419200" />
</http>

当用户未经过完全身份验证时,我需要以某种方式让用户返回到原始请求的 URL。关于执行此操作的最佳方法有什么想法吗?

我想出了一个解决方案,但这让我感到畏缩,因为这似乎比必要的工作更多。

在我的场景中,ExceptionTranslationFilter 不会调用登录过程,因此不会存储原始 URL。

以下行未被调用

    requestCache.saveRequest(request, response);

相反,我通过配置捕获了 403 错误并将用户发送到登录页面。在我的例子中,用户应该被视为匿名用户,并且不会生成 403,并且应该开始登录过程。

我发现更改此行为的最简单方法是更改​​ AuthenticationTrustResolverImpl

    public boolean isAnonymous(Authentication authentication) {
if ((anonymousClass == null) || (authentication == null)) {
return false;
}

//if this is a RememberMe me situation, the user should be treated as
//if they were anonymous
if (this.isRememberMe(authentication)){
return true;
}

return anonymousClass.isAssignableFrom(authentication.getClass());
}

这似乎完全符合我的要求,但是由于使用 http 命名空间时无法访问 ExceptionTranslationFilter,我不得不进行大量困惑的手动配置。

有没有更优雅的方法来做到这一点?

最佳答案

有一个PR计划在 Spring Security 4.2.0 M1 中解决这一需求。它的相关提交可能给出了在以前版本的 Spring Security 中实现相同功能的提示。

关于Spring Security 记住我 RequireFully 授权后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com