- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
提供 AuthenticationSuccessHandler
对于 RememberMeAuthenticationFilter
打破了过滤器链,因此我想覆盖它的 onSuccessfulAuthentication
方法通过提供 RememberMeAuthenticationFilter
的自定义实现.但是当使用简单的 Java Config 时,这似乎相当复杂或精细。
提供 ApplicationEventPublisher
如果需要访问 HttpServletRequest
,这不是一种解决方案或 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);
}
});
最佳答案
如果您想在身份验证过程(具有记住我的功能)成功时实现自定义行为,您可以尝试:
自定义记住我身份验证过滤器
定义一个新的过滤器,例如:
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..
}
}
}
@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());
}
关于java - RememberMeAuthenticationFilter 和 Java 配置 : Custom implementation to override onSuccessfulAuthentication - how to do it in a clean way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61910751/
我已经使用 Spring Security 3.1 在 Spring MVC 应用程序中实现了“记住我”功能 我的 security-context.xml 如下所示:
我正在尝试将 RememberMe 功能集成到 Spring 3 webapp 中。该应用运行良好,未显示任何其他问题。 当我启用“记住我”检查时,cookie 会正确创建并随任何请求一起发送(我已经
我想为我的登录过滤器实现自定义 AuthenticationSuccessHandler,即 org.springframework.security.web.authentication.remem
提供 AuthenticationSuccessHandler对于 RememberMeAuthenticationFilter打破了过滤器链,因此我想覆盖它的 onSuccessfulAuthent
我是一名优秀的程序员,十分优秀!