OAuth2ClientWebMvcSecurityConfiguration handles multiple OAuth2AuthorizedClientService @Bean
Fixes gh-5321
This commit is contained in:
		
							parent
							
								
									81350ca3c3
								
							
						
					
					
						commit
						b3a38fb0f6
					
				| 
						 | 
				
			
			@ -57,7 +57,6 @@ final class OAuth2ClientConfiguration {
 | 
			
		|||
 | 
			
		||||
	@Configuration
 | 
			
		||||
	static class OAuth2ClientWebMvcSecurityConfiguration implements WebMvcConfigurer {
 | 
			
		||||
		@Autowired(required = false)
 | 
			
		||||
		private OAuth2AuthorizedClientService authorizedClientService;
 | 
			
		||||
 | 
			
		||||
		@Override
 | 
			
		||||
| 
						 | 
				
			
			@ -68,5 +67,12 @@ final class OAuth2ClientConfiguration {
 | 
			
		|||
				argumentResolvers.add(authorizedClientArgumentResolver);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		@Autowired(required = false)
 | 
			
		||||
		public void setAuthorizedClientService(List<OAuth2AuthorizedClientService> authorizedClientServices) {
 | 
			
		||||
			if (authorizedClientServices.size() == 1) {
 | 
			
		||||
				this.authorizedClientService = authorizedClientServices.get(0);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,6 +17,7 @@ package org.springframework.security.config.annotation.web.configuration;
 | 
			
		|||
 | 
			
		||||
import org.junit.Rule;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 | 
			
		||||
| 
						 | 
				
			
			@ -24,12 +25,14 @@ import org.springframework.security.config.test.SpringTestRule;
 | 
			
		|||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
 | 
			
		||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
 | 
			
		||||
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
 | 
			
		||||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
 | 
			
		||||
import org.springframework.security.oauth2.core.OAuth2AccessToken;
 | 
			
		||||
import org.springframework.test.web.servlet.MockMvc;
 | 
			
		||||
import org.springframework.web.bind.annotation.GetMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
import static org.mockito.Mockito.when;
 | 
			
		||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
 | 
			
		||||
| 
						 | 
				
			
			@ -92,4 +95,41 @@ public class OAuth2ClientConfigurationTests {
 | 
			
		|||
			return AUTHORIZED_CLIENT_SERVICE;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// gh-5321
 | 
			
		||||
	@Test
 | 
			
		||||
	public void loadContextWhenOAuth2AuthorizedClientServiceRegisteredTwiceThenThrowNoUniqueBeanDefinitionException() {
 | 
			
		||||
		assertThatThrownBy(() -> this.spring.register(OAuth2AuthorizedClientServiceRegisteredTwiceConfig.class).autowire())
 | 
			
		||||
				.hasRootCauseInstanceOf(NoUniqueBeanDefinitionException.class)
 | 
			
		||||
				.hasMessageContaining("Only one matching @Bean of type " + OAuth2AuthorizedClientService.class.getName() + " should be registered.");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@EnableWebMvc
 | 
			
		||||
	@EnableWebSecurity
 | 
			
		||||
	static class OAuth2AuthorizedClientServiceRegisteredTwiceConfig extends WebSecurityConfigurerAdapter {
 | 
			
		||||
 | 
			
		||||
		@Override
 | 
			
		||||
		protected void configure(HttpSecurity http) throws Exception {
 | 
			
		||||
			http
 | 
			
		||||
				.authorizeRequests()
 | 
			
		||||
					.anyRequest().authenticated()
 | 
			
		||||
					.and()
 | 
			
		||||
				.oauth2Login();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		@Bean
 | 
			
		||||
		public ClientRegistrationRepository clientRegistrationRepository() {
 | 
			
		||||
			return mock(ClientRegistrationRepository.class);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		@Bean
 | 
			
		||||
		public OAuth2AuthorizedClientService authorizedClientService1() {
 | 
			
		||||
			return mock(OAuth2AuthorizedClientService.class);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		@Bean
 | 
			
		||||
		public OAuth2AuthorizedClientService authorizedClientService2() {
 | 
			
		||||
			return mock(OAuth2AuthorizedClientService.class);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue