Skip to content

Commit 1e9fca0

Browse files
committed
Register a bean for OAuth2AuthorizedClientService
Closes gh-10837
1 parent 3b21fd5 commit 1e9fca0

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2WebSecurityConfiguration.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
21+
import org.springframework.context.annotation.Bean;
2122
import org.springframework.context.annotation.Configuration;
2223
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2324
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
25+
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
26+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
2427
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
2528

2629
/**
@@ -35,21 +38,19 @@
3538
@ConditionalOnBean(ClientRegistrationRepository.class)
3639
class OAuth2WebSecurityConfiguration {
3740

41+
@Bean
42+
@ConditionalOnMissingBean
43+
public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
44+
return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
45+
}
46+
3847
@Configuration
3948
static class OAuth2WebSecurityConfigurationAdapter
4049
extends WebSecurityConfigurerAdapter {
4150

42-
private final ClientRegistrationRepository clientRegistrationRepository;
43-
44-
OAuth2WebSecurityConfigurationAdapter(
45-
ClientRegistrationRepository clientRegistrationRepository) {
46-
this.clientRegistrationRepository = clientRegistrationRepository;
47-
}
48-
4951
@Override
5052
protected void configure(HttpSecurity http) throws Exception {
51-
http.authorizeRequests().anyRequest().authenticated().and().oauth2Login()
52-
.clientRegistrationRepository(this.clientRegistrationRepository);
53+
http.authorizeRequests().anyRequest().authenticated().and().oauth2Login();
5354
}
5455

5556
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2WebSecurityConfigurationTests.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.springframework.context.annotation.Import;
3333
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3434
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
35+
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
36+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
3537
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3638
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
3739
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
@@ -55,7 +57,7 @@ public class OAuth2WebSecurityConfigurationTests {
5557
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
5658

5759
@Test
58-
public void securityConfigurerRegistersClientRegistrations() throws Exception {
60+
public void securityConfigurerConfiguresOAuth2Login() throws Exception {
5961
this.contextRunner.withUserConfiguration(ClientRepositoryConfiguration.class,
6062
OAuth2WebSecurityConfiguration.class).run((context) -> {
6163
ClientRegistrationRepository expected = context
@@ -88,6 +90,34 @@ public void securityConfigurerBacksOffWhenOtherWebSecurityAdapterPresent()
8890
.run((context) -> assertThat(getAuthCodeFilters(context)).isEmpty());
8991
}
9092

93+
@Test
94+
public void configurationRegistersAuthorizedClientServiceBean() throws Exception {
95+
this.contextRunner
96+
.withUserConfiguration(ClientRepositoryConfiguration.class,
97+
OAuth2WebSecurityConfiguration.class)
98+
.run(context -> {
99+
OAuth2AuthorizedClientService bean = context.getBean(OAuth2AuthorizedClientService.class);
100+
OAuth2AuthorizedClientService authorizedClientService = (OAuth2AuthorizedClientService) ReflectionTestUtils
101+
.getField(getAuthCodeFilters(context).get(0),
102+
"authorizedClientService");
103+
assertThat(authorizedClientService).isEqualTo(bean);
104+
});
105+
}
106+
107+
@Test
108+
public void authorizedClientServiceBeanIsConditionalOnMissingBean() throws Exception {
109+
this.contextRunner
110+
.withUserConfiguration(OAuth2AuthorizedClientServiceConfiguration.class,
111+
OAuth2WebSecurityConfiguration.class)
112+
.run(context -> {
113+
OAuth2AuthorizedClientService bean = context.getBean(OAuth2AuthorizedClientService.class);
114+
OAuth2AuthorizedClientService authorizedClientService = (OAuth2AuthorizedClientService) ReflectionTestUtils
115+
.getField(getAuthCodeFilters(context).get(0),
116+
"authorizedClientService");
117+
assertThat(authorizedClientService).isEqualTo(bean);
118+
});
119+
}
120+
91121
@SuppressWarnings("unchecked")
92122
private List<Filter> getAuthCodeFilters(AssertableApplicationContext context) {
93123
FilterChainProxy filterChain = (FilterChainProxy) context
@@ -176,4 +206,15 @@ static class TestWebSecurityConfigurerConfig extends WebSecurityConfigurerAdapte
176206

177207
}
178208

209+
@Configuration
210+
@Import({ ClientRepositoryConfiguration.class })
211+
static class OAuth2AuthorizedClientServiceConfiguration {
212+
213+
@Bean
214+
public OAuth2AuthorizedClientService testAuthorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
215+
return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
216+
}
217+
218+
}
219+
179220
}

0 commit comments

Comments
 (0)