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