代码之家  ›  专栏  ›  技术社区  ›  Eddie Jaoude

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

  •  4
  • Eddie Jaoude  · 技术社区  · 10 年前

    使用Spring引导-在使用GitHub OAuth成功验证后,不会触发Audit侦听器。

    public class AuthenticationListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
    
    @Override
    public void onApplicationEvent(final InteractiveAuthenticationSuccessEvent event) {
           System.out.println("+++++++ ================ ------------");
       }
    
    }
    

    我需要在其他地方注册吗?我已经尝试过在Stackoverflow上的其他地方创建@Bean,但这没有什么区别。

    完整代码 https://github.com/DashboardHub/PipelineDashboard/tree/feature/178-login-github

    使现代化

    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;
    }
    }
    
    3 回复  |  直到 10 年前
        1
  •  3
  •   Simon Casey    10 年前

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

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;    
    
    ...        
    
    githubFilter.setApplicationEventPublisher(applicationEventPublisher);
    

    出于某种原因,过滤器上的应用程序事件发布者默认为NullEventPublisher。

        2
  •  0
  •   wcislo    10 年前

    在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;
    }
    

    这不是你想要的,但应该解决你的问题。

        3
  •  0
  •   dtortola    9 年前

    监听AuthenticationSuccessEvent而不是InteractiveAuthenticationSuccess事件对我起到了关键作用。 然而,侦听器被调用两次:第一次事件的Authentication是UsernamePasswordAuthenticationToken,而第二次是OAuth2Authentication

    推荐文章