使用Spring启动 - 使用GitHub OAuth成功验证后,审计侦听器未被触发。
person
我需要在其他地方注册吗?我已经尝试过在Stackoverflow上创建@Bean的其他地方,但这没有任何区别。
完整代码https://github.com/DashboardHub/PipelineDashboard/tree/feature/178-login-github
更新
SecurityConfig类
public class AuthenticationListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(final InteractiveAuthenticationSuccessEvent event) {
System.out.println("+++++++ ================ ------------");
}
}
答案 0 :(得分:3)
我通过将默认的ApplicationEventPublisher注入到安全配置bean中来实现这一点。然后将其设置为processingfilter上的应用程序事件发布者:
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
...
githubFilter.setApplicationEventPublisher(applicationEventPublisher);
出于某种原因,默认情况下,过滤器上的应用程序事件发布者是NullEventPublisher。
答案 1 :(得分:0)
在AuthenticationListener类上使用@Configuration批注在应用程序上下文中注册它。
修改强>
由于我无法弄清楚为什么事件没有被解雇,我提出了解决这个问题的替代解决方案。您必须创建实现AuthenticationSuccessHanddler
的类并实现其onAuthenticationSuccess
方法:
@Component(value="customAuthenticationSuccessHandler")
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler{
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
//implementation
}
}
然后将其添加到您的配置中:
@Resource
private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
...
.loginPage("/login").and()
.successHandler(getCustomAuthenticationSuccessHandler())
.permitAll()
...
}
public CustomAuthenticationSuccessHandler getCustomAuthenticationSuccessHandler() {
return customAuthenticationSuccessHandler;
}
这不是你想要的,但应该解决你的问题。
答案 2 :(得分:0)
而不是InteractiveAuthenticationSuccessEvent,监听AuthenticationSuccessEvent对我来说是个窍门。 但是,监听器被调用两次:第一个事件的身份验证是UsernamePasswordAuthenticationToken,而第二个是OAuth2Authentication