Skip to content

Commit 5579846

Browse files
committed
AuthenticationEventPublisher Bean Lookup
Issue gh-7793 Fixes gh-7515
1 parent fc9b97c commit 5579846

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
29+
2930
import org.springframework.aop.TargetSource;
3031
import org.springframework.aop.framework.Advised;
3132
import org.springframework.aop.target.LazyInitTargetSource;
@@ -36,6 +37,7 @@
3637
import org.springframework.context.ApplicationContext;
3738
import org.springframework.core.annotation.Order;
3839
import org.springframework.core.io.support.SpringFactoriesLoader;
40+
import org.springframework.security.authentication.AuthenticationEventPublisher;
3941
import org.springframework.security.authentication.AuthenticationManager;
4042
import org.springframework.security.authentication.AuthenticationTrustResolver;
4143
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
@@ -194,8 +196,7 @@ protected final HttpSecurity getHttp() throws Exception {
194196
return http;
195197
}
196198

197-
DefaultAuthenticationEventPublisher eventPublisher = objectPostProcessor
198-
.postProcess(new DefaultAuthenticationEventPublisher());
199+
AuthenticationEventPublisher eventPublisher = getAuthenticationEventPublisher();
199200
localConfigureAuthenticationBldr.authenticationEventPublisher(eventPublisher);
200201

201202
AuthenticationManager authenticationManager = authenticationManager();
@@ -407,6 +408,13 @@ public void setAuthenticationConfiguration(
407408
this.authenticationConfiguration = authenticationConfiguration;
408409
}
409410

411+
private AuthenticationEventPublisher getAuthenticationEventPublisher() {
412+
if (this.context.getBeanNamesForType(AuthenticationEventPublisher.class).length > 0) {
413+
return this.context.getBean(AuthenticationEventPublisher.class);
414+
}
415+
return this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
416+
}
417+
410418
/**
411419
* Creates the shared objects
412420
*

config/src/test/java/org/springframework/security/config/annotation/web/WebSecurityConfigurerAdapterTests.java

+34
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@
3333
import org.springframework.context.annotation.Configuration;
3434
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3535
import org.springframework.core.annotation.Order;
36+
import org.springframework.security.authentication.AuthenticationEventPublisher;
3637
import org.springframework.security.authentication.AuthenticationTrustResolver;
3738
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
3839
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3940
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4041
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
4142
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4243
import org.springframework.security.config.test.SpringTestRule;
44+
import org.springframework.security.core.Authentication;
4345
import org.springframework.security.core.userdetails.PasswordEncodedUser;
4446
import org.springframework.security.core.userdetails.UserDetailsService;
4547
import org.springframework.security.core.userdetails.UsernameNotFoundException;
48+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
4649
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
4750
import org.springframework.test.web.servlet.MockMvc;
4851
import org.springframework.web.accept.ContentNegotiationStrategy;
@@ -51,8 +54,11 @@
5154

5255
import static org.assertj.core.api.Assertions.assertThat;
5356
import static org.assertj.core.api.ThrowableAssert.catchThrowable;
57+
import static org.mockito.ArgumentMatchers.any;
5458
import static org.mockito.Mockito.mock;
59+
import static org.mockito.Mockito.verify;
5560
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
61+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
5662
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5763
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
5864
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -364,4 +370,32 @@ static class DefaultOrderWebSecurityConfig extends WebSecurityConfigurerAdapter
364370
@Order
365371
static class LowestPriorityWebSecurityConfig extends WebSecurityConfigurerAdapter {
366372
}
373+
374+
// gh-7515
375+
@Test
376+
public void performWhenUsingAuthenticationEventPublisherBeanThenUses() throws Exception {
377+
this.spring.register(CustomAuthenticationEventPublisherBean.class).autowire();
378+
379+
AuthenticationEventPublisher authenticationEventPublisher =
380+
this.spring.getContext().getBean(AuthenticationEventPublisher.class);
381+
382+
this.mockMvc.perform(get("/")
383+
.with(httpBasic("user", "password")));
384+
385+
verify(authenticationEventPublisher).publishAuthenticationSuccess(any(Authentication.class));
386+
}
387+
388+
@EnableWebSecurity
389+
static class CustomAuthenticationEventPublisherBean extends WebSecurityConfigurerAdapter {
390+
@Bean
391+
@Override
392+
public UserDetailsService userDetailsService() {
393+
return new InMemoryUserDetailsManager(PasswordEncodedUser.user());
394+
}
395+
396+
@Bean
397+
public AuthenticationEventPublisher authenticationEventPublisher() {
398+
return mock(AuthenticationEventPublisher.class);
399+
}
400+
}
367401
}

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurerTests.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.springframework.mock.web.MockHttpServletRequest;
7474
import org.springframework.security.access.prepost.PreAuthorize;
7575
import org.springframework.security.authentication.AbstractAuthenticationToken;
76+
import org.springframework.security.authentication.AuthenticationEventPublisher;
7677
import org.springframework.security.authentication.AuthenticationManager;
7778
import org.springframework.security.authentication.AuthenticationManagerResolver;
7879
import org.springframework.security.authentication.AuthenticationProvider;
@@ -88,6 +89,7 @@
8889
import org.springframework.security.core.authority.SimpleGrantedAuthority;
8990
import org.springframework.security.core.userdetails.UserDetailsService;
9091
import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal;
92+
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
9193
import org.springframework.security.oauth2.core.OAuth2Error;
9294
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
9395
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
@@ -99,8 +101,8 @@
99101
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
100102
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
101103
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
102-
import org.springframework.security.oauth2.server.resource.authentication.JwtIssuerAuthenticationManagerResolver;
103104
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
105+
import org.springframework.security.oauth2.server.resource.authentication.JwtIssuerAuthenticationManagerResolver;
104106
import org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector;
105107
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
106108
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint;
@@ -1091,6 +1093,22 @@ public void requestWhenUsingPublicKeyAlgorithmDoesNotMatchThenReturnsInvalidToke
10911093
.andExpect(invalidTokenHeader("algorithm"));
10921094
}
10931095

1096+
// gh-7793
1097+
@Test
1098+
public void requestWhenUsingCustomAuthenticationEventPublisherThenUses() throws Exception{
1099+
this.spring.register(CustomAuthenticationEventPublisher.class).autowire();
1100+
1101+
when(bean(JwtDecoder.class).decode(anyString()))
1102+
.thenThrow(new JwtException("problem"));
1103+
1104+
this.mvc.perform(get("/").with(bearerToken("token")));
1105+
1106+
verifyBean(AuthenticationEventPublisher.class)
1107+
.publishAuthenticationFailure(
1108+
any(OAuth2AuthenticationException.class),
1109+
any(Authentication.class));
1110+
}
1111+
10941112
@Test
10951113
public void getWhenCustomJwtAuthenticationManagerThenUsed() throws Exception {
10961114
this.spring.register(JwtAuthenticationManagerConfig.class, BasicController.class).autowire();
@@ -2015,6 +2033,31 @@ JwtDecoder decoder() throws Exception {
20152033
}
20162034
}
20172035

2036+
@EnableWebSecurity
2037+
static class CustomAuthenticationEventPublisher extends WebSecurityConfigurerAdapter {
2038+
@Override
2039+
protected void configure(HttpSecurity http) throws Exception {
2040+
// @formatter:off
2041+
http
2042+
.authorizeRequests()
2043+
.anyRequest().authenticated()
2044+
.and()
2045+
.oauth2ResourceServer()
2046+
.jwt();
2047+
// @formatter:on
2048+
}
2049+
2050+
@Bean
2051+
JwtDecoder jwtDecoder() {
2052+
return mock(JwtDecoder.class);
2053+
}
2054+
2055+
@Bean
2056+
AuthenticationEventPublisher authenticationEventPublisher() {
2057+
return mock(AuthenticationEventPublisher.class);
2058+
}
2059+
}
2060+
20182061
@EnableWebSecurity
20192062
static class OpaqueTokenConfig extends WebSecurityConfigurerAdapter {
20202063
@Override

0 commit comments

Comments
 (0)