|
29 | 29 | import org.junit.jupiter.api.BeforeEach; |
30 | 30 | import org.junit.jupiter.api.Test; |
31 | 31 | import org.junit.jupiter.api.extension.ExtendWith; |
| 32 | +import org.mockito.Mockito; |
32 | 33 |
|
33 | 34 | import org.springframework.beans.factory.BeanCreationException; |
34 | 35 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
|
50 | 51 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
51 | 52 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
52 | 53 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 54 | +import org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2LoginConfigurerTests.OAuth2LoginConfigCustomWithPostProcessor.SpyObjectPostProcessor; |
53 | 55 | import org.springframework.security.config.oauth2.client.CommonOAuth2Provider; |
54 | 56 | import org.springframework.security.config.test.SpringTestContext; |
55 | 57 | import org.springframework.security.config.test.SpringTestContextExtension; |
56 | 58 | import org.springframework.security.context.DelegatingApplicationListener; |
57 | 59 | import org.springframework.security.core.Authentication; |
58 | | -import org.springframework.security.core.AuthenticationException; |
59 | 60 | import org.springframework.security.core.GrantedAuthority; |
60 | 61 | import org.springframework.security.core.authority.AuthorityUtils; |
61 | 62 | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
@@ -219,28 +220,6 @@ public void oauth2Login() throws Exception { |
219 | 220 | .hasToString("OAUTH2_USER"); |
220 | 221 | } |
221 | 222 |
|
222 | | - // gh-17175 |
223 | | - @Test |
224 | | - public void postProcessorSucceedsWhenProcessorReturnsAuthenticationProvider() throws Exception { |
225 | | - loadConfig(OAuth2LoginConfigCustomWithPostProcessor.class); |
226 | | - // setup authorization request |
227 | | - OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); |
228 | | - this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, this.request, this.response); |
229 | | - // setup authentication parameters |
230 | | - this.request.setParameter("code", "code123"); |
231 | | - this.request.setParameter("state", authorizationRequest.getState()); |
232 | | - // perform test |
233 | | - this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
234 | | - // assertions |
235 | | - Authentication authentication = this.securityContextRepository |
236 | | - .loadContext(new HttpRequestResponseHolder(this.request, this.response)) |
237 | | - .getAuthentication(); |
238 | | - assertThat(authentication.getAuthorities()).hasSize(1); |
239 | | - assertThat(authentication.getAuthorities()).first() |
240 | | - .isInstanceOf(OAuth2UserAuthority.class) |
241 | | - .hasToString("OAUTH2_USER"); |
242 | | - } |
243 | | - |
244 | 223 | @Test |
245 | 224 | public void requestWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
246 | 225 | loadConfig(OAuth2LoginConfig.class, SecurityContextChangedListenerConfig.class); |
@@ -736,6 +715,22 @@ public void oidcLoginWhenOAuth2ClientBeansConfiguredThenNotShared() throws Excep |
736 | 715 | verifyNoInteractions(clientRegistrationRepository, authorizedClientRepository); |
737 | 716 | } |
738 | 717 |
|
| 718 | + // gh-17175 |
| 719 | + @Test |
| 720 | + public void oauth2LoginWhenAuthenticationProviderPostProcessorThenUses() throws Exception { |
| 721 | + loadConfig(OAuth2LoginConfigCustomWithPostProcessor.class); |
| 722 | + // setup authorization request |
| 723 | + OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); |
| 724 | + this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, this.request, this.response); |
| 725 | + // setup authentication parameters |
| 726 | + this.request.setParameter("code", "code123"); |
| 727 | + this.request.setParameter("state", authorizationRequest.getState()); |
| 728 | + // perform test |
| 729 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
| 730 | + // assertions |
| 731 | + verify(this.context.getBean(SpyObjectPostProcessor.class).spy).authenticate(any()); |
| 732 | + } |
| 733 | + |
739 | 734 | private void loadConfig(Class<?>... configs) { |
740 | 735 | AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); |
741 | 736 | applicationContext.register(configs); |
@@ -1323,50 +1318,46 @@ OAuth2AuthorizedClientRepository authorizedClientRepository() { |
1323 | 1318 |
|
1324 | 1319 | @Configuration |
1325 | 1320 | @EnableWebSecurity |
1326 | | - static class OAuth2LoginConfigCustomWithPostProcessor |
1327 | | - extends CommonLambdaSecurityFilterChainConfig { |
| 1321 | + static class OAuth2LoginConfigCustomWithPostProcessor { |
1328 | 1322 |
|
1329 | | - private ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository( |
| 1323 | + private final ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository( |
1330 | 1324 | GOOGLE_CLIENT_REGISTRATION); |
1331 | 1325 |
|
1332 | | - OAuth2AuthorizationRequestResolver resolver = mock(OAuth2AuthorizationRequestResolver.class); |
| 1326 | + private final ObjectPostProcessor<AuthenticationProvider> postProcessor = new SpyObjectPostProcessor(); |
1333 | 1327 |
|
1334 | 1328 | @Bean |
1335 | 1329 | SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
1336 | 1330 | // @formatter:off |
1337 | 1331 | http |
1338 | | - .oauth2Login((oauth2Login) -> |
1339 | | - oauth2Login |
1340 | | - .clientRegistrationRepository(this.clientRegistrationRepository) |
1341 | | -// .authorizedClientRepository(this.authorizedClientRepository) |
1342 | | - .withObjectPostProcessor(new CustomProcessor()) |
1343 | | - ); |
| 1332 | + .oauth2Login((oauth2Login) -> oauth2Login |
| 1333 | + .clientRegistrationRepository(this.clientRegistrationRepository) |
| 1334 | + .withObjectPostProcessor(this.postProcessor) |
| 1335 | + ); |
1344 | 1336 | // @formatter:on |
1345 | | - return super.configureFilterChain(http); |
| 1337 | + return http.build(); |
1346 | 1338 | } |
1347 | 1339 |
|
1348 | | - class CustomProcessor implements ObjectPostProcessor<AuthenticationProvider> { |
1349 | | - @Override |
1350 | | - public <O extends AuthenticationProvider> O postProcess(O object) { |
1351 | | - AuthenticationProvider p = new NoopWrapperProvider(object); |
| 1340 | + @Bean |
| 1341 | + ObjectPostProcessor<AuthenticationProvider> mockPostProcessor() { |
| 1342 | + return this.postProcessor; |
| 1343 | + } |
1352 | 1344 |
|
1353 | | - return (O) p; |
1354 | | - } |
| 1345 | + @Bean |
| 1346 | + HttpSessionOAuth2AuthorizationRequestRepository oauth2AuthorizationRequestRepository() { |
| 1347 | + return new HttpSessionOAuth2AuthorizationRequestRepository(); |
1355 | 1348 | } |
1356 | 1349 |
|
1357 | | - record NoopWrapperProvider( |
1358 | | - AuthenticationProvider delegate |
1359 | | - ) implements AuthenticationProvider { |
| 1350 | + static class SpyObjectPostProcessor implements ObjectPostProcessor<AuthenticationProvider> { |
1360 | 1351 |
|
1361 | | - @Override |
1362 | | - public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
1363 | | - return delegate.authenticate(authentication); |
1364 | | - } |
| 1352 | + AuthenticationProvider spy; |
1365 | 1353 |
|
1366 | 1354 | @Override |
1367 | | - public boolean supports(Class<?> authentication) { |
1368 | | - return delegate.supports(authentication); |
| 1355 | + public <O extends AuthenticationProvider> O postProcess(O object) { |
| 1356 | + O spy = Mockito.spy(object); |
| 1357 | + this.spy = spy; |
| 1358 | + return spy; |
1369 | 1359 | } |
| 1360 | + |
1370 | 1361 | } |
1371 | 1362 |
|
1372 | 1363 | } |
|
0 commit comments