Setting a DefaultAuthenticationEventPublisher in WebSecurityConfigurerAdapter for Spring Security 5

Viewed 612

I've updated some of our services to Spring Security 5 from a SS 4 version where it had external support for OAuth2. I've been able to update our services for the most part.

Previously our WebSecurityConfigurerAdapter had the following configure function where we set an applicationEventPublisher on a OAuth2AuthenticationProcessingFilter so we can output logs messages about AuthenticationEvents.
But OAuth2AuthenticationProcessingFilter went away in SS5 because OAuth2 support is now directly supported.
Is their an easier way of setting the AuthenticationEventPublisher without creating my own filter and recreating the part of checking authentication to just have it done again by Spring?

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    OAuth2AuthenticationProcessingFilter filter = new OAuth2AuthenticationProcessingFilter();
    filter.setAuthenticationManager(authenticationManager);
    filter.setStateless(true);
    filter.setAuthenticationEventPublisher(
        new DefaultAuthenticationEventPublisher(applicationEventPublisher));
    filter.afterPropertiesSet();

    http.csrf()
        .disable()
        .anonymous()
        .disable()
        .httpBasic()
        .disable()
        .logout()
        .disable()
        .formLogin()
        .disable()
        .addFilterBefore(filter, SessionManagementFilter.class)
        .antMatcher("/actuator/**")
        .authorizeRequests()
        .antMatchers(HttpMethod.OPTIONS)
        .permitAll()
        .antMatchers("/actuator/**")
        .access("#oauth2.hasScope('auditor')")
        .anyRequest()
        .authenticated()
        .accessDecisionManager(accessDecisionManager);
  }

  @Bean
  public ApplicationListener<AuditApplicationEvent> onEventListener() {
    return (AuditApplicationEvent event) -> {
      LoggingUtil loggingFormat = new LoggingUtil();
      loggingFormat.setUser(event.getAuditEvent().getPrincipal());
      loggingFormat.setEvent("Resource Authentication");
      loggingFormat.setOutcome(event.getAuditEvent().getType());
      loggingFormat.setMessage(
          event.getAuditEvent().getData().containsKey("message")
              ? event.getAuditEvent().getData().get("message").toString()
              : "");
      String info = loggingFormat.toString();
      logger.info(info);
    };
  }

For our SS 5 we have

    protected void configure(HttpSecurity http) throws Exception {
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        SessionManagementFilter as;

        //TODO how do we setup the authenticationEventPublisher
    //filter.setAuthenticationEventPublisher(
//        new DefaultAuthenticationEventPublisher(applicationEventPublisher));

        http.csrf()
                .disable()
                .anonymous()
                .disable()
                .httpBasic()
                .disable()
                .logout()
                .disable()
                .formLogin()
                .disable()
//        .addFilterBefore(filter, SessionManagementFilter.class)
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS)
                .permitAll()
                .antMatchers("/actuator/**").hasAuthority("SCOPE_auditor")
                .anyRequest()
                .authenticated()
                .and()
                .oauth2ResourceServer().jwt();
    }
0 Answers
Related