所有,
我正在尝试实现“记住我”功能
我有两个受保护的网址。/operation/fully
(用户必须经过完全认证,不记得我了)
和/operation/authenticated
(请记住我,确定)
如果我不记得我的cookie,而我访问了任何一个URL,系统都会提示我输入我的凭据并将其重定向到原始URL,这样的日子就很好了。
如果我处于“记住我”模式,则可以导航到/ operation / authenticated没问题。如果我导航到/ operation /完全,我将被重定向到登录页面。然后,我进行身份验证,并返回到“ /”,我想完全回到我的原始目标/ operation /。
<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,因此我不得不进行许多麻烦的手动配置。
有没有更优雅的方法可以做到这一点?
最佳答案
Spring Security 4.2.0 M1安排了PR来解决此需求。它的关联提交可能提示在以前的Spring Security版本中实现相同的操作。