mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Register a bean for OAuth2AuthorizedClientService
Closes gh-10837
This commit is contained in:
parent
3b21fd5ef6
commit
1e9fca0b76
@ -18,9 +18,12 @@ package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
|
||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
||||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
||||
|
||||
/**
|
||||
@ -35,21 +38,19 @@ import org.springframework.security.oauth2.client.registration.ClientRegistratio
|
||||
@ConditionalOnBean(ClientRegistrationRepository.class)
|
||||
class OAuth2WebSecurityConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
|
||||
return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class OAuth2WebSecurityConfigurationAdapter
|
||||
extends WebSecurityConfigurerAdapter {
|
||||
|
||||
private final ClientRegistrationRepository clientRegistrationRepository;
|
||||
|
||||
OAuth2WebSecurityConfigurationAdapter(
|
||||
ClientRegistrationRepository clientRegistrationRepository) {
|
||||
this.clientRegistrationRepository = clientRegistrationRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests().anyRequest().authenticated().and().oauth2Login()
|
||||
.clientRegistrationRepository(this.clientRegistrationRepository);
|
||||
http.authorizeRequests().anyRequest().authenticated().and().oauth2Login();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -32,6 +32,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
|
||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
||||
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
||||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
||||
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
|
||||
@ -55,7 +57,7 @@ public class OAuth2WebSecurityConfigurationTests {
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void securityConfigurerRegistersClientRegistrations() throws Exception {
|
||||
public void securityConfigurerConfiguresOAuth2Login() throws Exception {
|
||||
this.contextRunner.withUserConfiguration(ClientRepositoryConfiguration.class,
|
||||
OAuth2WebSecurityConfiguration.class).run((context) -> {
|
||||
ClientRegistrationRepository expected = context
|
||||
@ -88,6 +90,34 @@ public class OAuth2WebSecurityConfigurationTests {
|
||||
.run((context) -> assertThat(getAuthCodeFilters(context)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationRegistersAuthorizedClientServiceBean() throws Exception {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(ClientRepositoryConfiguration.class,
|
||||
OAuth2WebSecurityConfiguration.class)
|
||||
.run(context -> {
|
||||
OAuth2AuthorizedClientService bean = context.getBean(OAuth2AuthorizedClientService.class);
|
||||
OAuth2AuthorizedClientService authorizedClientService = (OAuth2AuthorizedClientService) ReflectionTestUtils
|
||||
.getField(getAuthCodeFilters(context).get(0),
|
||||
"authorizedClientService");
|
||||
assertThat(authorizedClientService).isEqualTo(bean);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authorizedClientServiceBeanIsConditionalOnMissingBean() throws Exception {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(OAuth2AuthorizedClientServiceConfiguration.class,
|
||||
OAuth2WebSecurityConfiguration.class)
|
||||
.run(context -> {
|
||||
OAuth2AuthorizedClientService bean = context.getBean(OAuth2AuthorizedClientService.class);
|
||||
OAuth2AuthorizedClientService authorizedClientService = (OAuth2AuthorizedClientService) ReflectionTestUtils
|
||||
.getField(getAuthCodeFilters(context).get(0),
|
||||
"authorizedClientService");
|
||||
assertThat(authorizedClientService).isEqualTo(bean);
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Filter> getAuthCodeFilters(AssertableApplicationContext context) {
|
||||
FilterChainProxy filterChain = (FilterChainProxy) context
|
||||
@ -176,4 +206,15 @@ public class OAuth2WebSecurityConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ ClientRepositoryConfiguration.class })
|
||||
static class OAuth2AuthorizedClientServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
public OAuth2AuthorizedClientService testAuthorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
|
||||
return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user