bgr*_*ves 3 java spring spring-security
提供AuthenticationSuccessHandlerfor aRememberMeAuthenticationFilter会破坏过滤器链,因此我想onSuccessfulAuthentication通过提供RememberMeAuthenticationFilter. 但是当使用简单的 Java Config 时,这似乎相当复杂或精细。
ApplicationEventPublisher如果需要访问HttpServletRequest或 ,则提供 an不是解决方案HttpServletResponse。
我设法做到了,但它看起来像一个黑客 - 有更好的方法吗?
我是这样做的:
http.rememberMe().addObjectPostProcessor(new ObjectPostProcessor<RememberMeAuthenticationFilter>() {
@Override
public <O extends RememberMeAuthenticationFilter> O postProcess(O object) {
RememberMeAuthenticationFilter newFilter = new RememberMeAuthenticationFilter(
(AuthenticationManager) getByReflection(object, "authenticationManager"),
(RememberMeServices) getByReflection(object, "rememberMeServices")
) {
@Override
protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) {
// business logic
}
};
return (O) newFilter;
}
private <O extends RememberMeAuthenticationFilter> Object getByReflection(O object, String name) {
Field field = ReflectionUtils.findField(object.getClass(), name);
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, object);
}
});
Run Code Online (Sandbox Code Playgroud)
如果您想在身份验证过程(具有记住我的功能)成功时实现自定义行为,您可以尝试:
定义一个新的过滤器,例如:
public class CustomRememberMeAuthenticationFilter extends RememberMeAuthenticationFilter {
@Override
protected void onSuccessfulAuthentication(final HttpServletRequest request, final HttpServletResponse response, final Authentication authResult) {
super.onSuccessfulAuthentication(request, response, authResult);
if (authResult != null) {
// process post authentication logic here..
}
}
}
Run Code Online (Sandbox Code Playgroud)
在安全链中设置客户文件:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/","/login*").permitAll()
//...
http
.addFilter(rememberMeAuthenticationFilter())
//...
}
@Bean
protected RememberMeAuthenticationFilter rememberMeAuthenticationFilter(){
return new CustomRememberMeAuthenticationFilter(authenticationManager(),rememberMeServices());
}
Run Code Online (Sandbox Code Playgroud)
检查此项以创建您的(authenticationManager(),rememberMeServices()
在前面的代码段中,刚刚添加了自定义过滤器。如果不起作用,您必须研究并在链中找到确切的过滤器以插入您的自定义过滤器:addFilterBefore、addFilterAfter、addFilterAt。
检查此添加过滤器方法
最后删除默认的http.rememberMe()以使用您自己的过滤器。因为记住我命名空间元素已经插入了一个 RememberMeAuthenticationFilter 所以它仍然会优先于你的,因为它在过滤器链中出现在它之前。
| 归档时间: |
|
| 查看次数: |
209 次 |
| 最近记录: |