Spring boot OAuth成功登录侦听器未触发

Spring boot OAuth成功登录侦听器未触发,spring,oauth,spring-security,spring-boot,listener,Spring,Oauth,Spring Security,Spring Boot,Listener,使用Spring引导-在成功使用GitHub OAuth进行身份验证后,不会触发审计侦听器 public class AuthenticationListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> { @Override public void onApplicationEvent(final InteractiveAuthenticationSuccessEvent even

使用Spring引导-在成功使用GitHub OAuth进行身份验证后,不会触发审计侦听器

public class AuthenticationListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

@Override
public void onApplicationEvent(final InteractiveAuthenticationSuccessEvent event) {
       System.out.println("+++++++ ================ ------------");
   }

}
公共类AuthenticationListener实现ApplicationListener{
@凌驾
ApplicationEvent上的公共无效(最终InteractiveAuthenticationSuccessEvent事件){
System.out.println(“+;
}
}
我需要在其他地方注册吗?我已经按照Stackoverflow上的建议尝试在其他地方创建@Bean,但这没有什么不同

完整代码

更新

SecurityConfig类

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
OAuth2ClientContext oauth2ClientContext;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/**")
        .authorizeRequests()
            .antMatchers("/", "/login**", "/webjars/**").permitAll()
            .anyRequest().authenticated()
        .and()
            .formLogin()
            .loginPage("/login")//.failureUrl("/login?error")
            .permitAll()
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class)
    ;
}

@Bean
@ConfigurationProperties("security.oauth2")
ClientResourcesConfig github() {
    return new ClientResourcesConfig();
}

private Filter ssoFilter() {
    CompositeFilter filter = new CompositeFilter();
    List<Filter> filters = new ArrayList<>();
    filters.add(ssoFilter(this.github(), "/login/github"));
    filter.setFilters(filters);
    return filter;
}

private Filter ssoFilter(ClientResourcesConfig client, String path) {
    OAuth2ClientAuthenticationProcessingFilter githubFilter = new OAuth2ClientAuthenticationProcessingFilter(
            path);
    OAuth2RestTemplate githubTemplate = new OAuth2RestTemplate(client.getClient(),
            oauth2ClientContext);
    githubFilter.setRestTemplate(githubTemplate);
    githubFilter.setTokenServices(new UserInfoTokenServices(
            client.getResource().getUserInfoUri(), client.getClient().getClientId()));
    return githubFilter;
}
}
@配置
公共类SecurityConfig扩展了WebSecurity配置适配器{
@自动连线
OAuth2ClientContext OAuth2ClientContext;
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.antMatcher(“/**”)
.授权请求()
.antMatchers(“/”、“/login**”、“/webjars/**”).permitAll()
.anyRequest().authenticated()
.及()
.formLogin()
.loginPage(“/login”)/.failureUrl(“/login?error”)
.permitAll()
.and().logout().logoutSuccessUrl(“/”).permitAll()
.and().addFilterBefore(ssoFilter(),BasicAuthenticationFilter.class)
;
}
@豆子
@ConfigurationProperties(“security.oauth2”)
ClientResourcesConfig github(){
返回新的ClientResourcesConfig();
}
专用筛选器ssoFilter(){
CompositeFilter过滤器=新的CompositeFilter();
列表过滤器=新的ArrayList();
添加(ssoFilter(this.github(),“/login/github”);
过滤器。设置过滤器(过滤器);
回流过滤器;
}
专用筛选器ssoFilter(ClientResourcesConfig客户端,字符串路径){
OAuth2ClientAuthenticationProcessingFilter githubFilter=新的OAuth2ClientAuthenticationProcessingFilter(
路径);
OAuth2RestTemplate githubTemplate=新的OAuth2RestTemplate(client.getClient(),
oauth2ClientContext);
setRestTemplate(githubTemplate);
setTokenServices(新的UserInfoTokenServices(
client.getResource().getUserInfoUri(),client.getClient().getClientId());
返回过滤器;
}
}

在AuthenticationListener类上使用@Configuration注释在应用程序上下文中注册它

编辑

由于我无法找出事件未被触发的原因,我提出了解决此问题的替代方案。您必须创建实现
AuthenticationSuccessHandler
的类,并实现其
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;
}

这并不是您想要的,但应该可以解决您的问题。

我通过将默认的ApplicationEventPublisher注入到您的安全配置bean中实现了这一点。然后在processingfilter上将其设置为应用程序事件发布者:

@Autowired
private ApplicationEventPublisher applicationEventPublisher;    

...        

githubFilter.setApplicationEventPublisher(applicationEventPublisher);

由于某些原因,筛选器上的应用程序事件发布服务器默认为NullEventPublisher。

不是InteractiveAuthenticationSuccessEvent,而是侦听AuthenticationSuccessEvent。
但是,侦听器会被调用两次:第一个事件的身份验证是UsernamePasswordAuthenticationToken,而第二个是OAuth2Authentication

您确定在Spring安全上下文中获得了授权吗?您可以检查其中一个视图()或身份验证对象(您可以从
SecurityContextHolder.getContext().getAuthentication()
)访问它),感谢您的链接。是的,经OAuth2(GitHub)明确授权。OAuth有什么不同吗?我不知道我是否正确理解了您的意思-您在Spring安全上下文中得到授权了吗<代码>SecurityContextHolder.getContext().getAuthentication().isAuthenticated()返回true?请在记录过程之后调试并检查它。我刚刚再次检查,它肯定会返回true。请尝试侦听
AbstractAuthenticationEvent
并记录您收到的每个事件类名。我认为,因为您使用的是OAuth,所以应用程序发布的是
AuthenticationSuccessEvent
,而不是
InteractiveAuthenticationSuccessEvent
,这是对其他需要它的人所做的更改的提交,与问题没有直接关系,但是,有时可能需要手动触发事件,这是使用注入的ApplicationEventPublisher的publishEvent()方法完成的