Skip to content

Commit 00f9ada

Browse files
committed
Stop creating a primary Oauth2RestTemplate
This commit removes the creation of a `@Primary` `OAuth2RestTemplate` and updates the documentation accordingly. Once #5507 is implemented we could revisit this area to provide a way for users to easily create such a bean. Closes gh-5202
1 parent c11b28c commit 00f9ada

File tree

3 files changed

+111
-21
lines changed

3 files changed

+111
-21
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343
import org.springframework.security.core.Authentication;
4444
import org.springframework.security.core.context.SecurityContextHolder;
4545
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
46-
import org.springframework.security.oauth2.client.OAuth2ClientContext;
47-
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
4846
import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
49-
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
5047
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
5148
import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest;
5249
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
@@ -69,15 +66,6 @@
6966
@Conditional(OAuth2ClientIdCondition.class)
7067
public class OAuth2RestOperationsConfiguration {
7168

72-
@Bean
73-
@Primary
74-
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext,
75-
OAuth2ProtectedResourceDetails details) {
76-
OAuth2RestTemplate template = new OAuth2RestTemplate(details,
77-
oauth2ClientContext);
78-
return template;
79-
}
80-
8169
@Configuration
8270
@ConditionalOnNotWebApplication
8371
protected static class SingletonScopedConfiguration {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.security.oauth2.sso;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.ObjectProvider;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
25+
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.context.ApplicationContext;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
31+
import org.springframework.context.annotation.Primary;
32+
import org.springframework.test.annotation.DirtiesContext;
33+
import org.springframework.test.context.TestPropertySource;
34+
import org.springframework.test.context.junit4.SpringRunner;
35+
import org.springframework.web.client.RestTemplate;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.verifyZeroInteractions;
40+
41+
/**
42+
* Test to validate that a custom {@link RestTemplate} can be defined
43+
* with OAuth2 SSO.
44+
*
45+
* @author Stephane Nicoll
46+
*/
47+
@RunWith(SpringRunner.class)
48+
@DirtiesContext
49+
@SpringBootTest
50+
@TestPropertySource(properties = {"security.oauth2.client.clientId=client",
51+
"security.oauth2.client.clientSecret=secret",
52+
"security.oauth2.client.userAuthorizationUri=http://example.com/oauth/authorize",
53+
"security.oauth2.client.accessTokenUri=http://example.com/oauth/token",
54+
"security.oauth2.resource.jwt.keyValue=SSSSHHH"})
55+
public class CustomRestTemplateBasicOAuth2SsoConfigurationTests {
56+
57+
@Autowired
58+
private ApplicationContext applicationContext;
59+
60+
@Autowired
61+
private ObjectProvider<RestTemplate> restTemplateProvider;
62+
63+
@Test
64+
public void customRestTemplateCanBePrimary() {
65+
RestTemplate restTemplate = this.restTemplateProvider.getIfAvailable();
66+
verifyZeroInteractions(restTemplate);
67+
assertThat(this.applicationContext.getBeansOfType(RestTemplate.class)).hasSize(2);
68+
}
69+
70+
@Configuration
71+
@Import(OAuth2AutoConfiguration.class)
72+
@EnableOAuth2Sso
73+
@MinimalSecureWebConfiguration
74+
protected static class TestConfiguration {
75+
76+
@Bean
77+
@Primary
78+
public RestTemplate myRestTemplate() {
79+
return mock(RestTemplate.class);
80+
}
81+
82+
}
83+
84+
}
85+
86+

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,11 +2296,27 @@ language feature). Example:
22962296

22972297
[[boot-features-security-custom-user-info-client]]
22982298
==== Client
2299-
To make your webapp into an OAuth2 client you can simply add `@EnableOAuth2Client` and
2300-
Spring Boot will create an `OAuth2RestTemplate` for you to `@Autowire`. It uses the
2301-
`security.oauth2.client.*` as credentials (the same as you might be using in the
2302-
Authorization Server), but in addition it will need to know the authorization and token
2303-
URIs in the Authorization Server. For example:
2299+
To make your web-app into an OAuth2 client you can simply add `@EnableOAuth2Client` and
2300+
Spring Boot will create a `OAuth2ClientContext` and `OAuth2ProtectedResourceDetails` that
2301+
are necessary to create an `OAuth2RestOperations`. Spring Boot does not automatically
2302+
create such bean but you can easily create your own:
2303+
2304+
[source,java,indent=0]
2305+
----
2306+
2307+
@Bean
2308+
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext,
2309+
OAuth2ProtectedResourceDetails details) {
2310+
return new OAuth2RestTemplate(details, oauth2ClientContext);
2311+
}
2312+
----
2313+
2314+
NOTE: You may want to add a qualifier and review your configuration as more than one
2315+
`RestTemplate` may be defined in your application.
2316+
2317+
This configuration uses `security.oauth2.client.*` as credentials (the same as you might
2318+
be using in the Authorization Server), but in addition it will need to know the
2319+
authorization and token URIs in the Authorization Server. For example:
23042320

23052321
.application.yml
23062322
[source,yaml,indent=0]
@@ -2332,12 +2348,12 @@ instance, your OAuth2 provider doesn't like header authentication). In fact, the
23322348
`security.oauth2.client.*` properties are bound to an instance of
23332349
`AuthorizationCodeResourceDetails` so all its properties can be specified.
23342350

2335-
TIP: In a non-web application you can still `@Autowire` an `OAuth2RestOperations` and it
2351+
TIP: In a non-web application you can still create an `OAuth2RestOperations` and it
23362352
is still wired into the `security.oauth2.client.*` configuration. In this case it is a
23372353
"`client credentials token grant`" you will be asking for if you use it (and there is no
2338-
need to use `@EnableOAuth2Client` or `@EnableOAuth2Sso`). To switch it off, just remove
2339-
the `security.oauth2.client.client-id` from your configuration (or make it the empty
2340-
string).
2354+
need to use `@EnableOAuth2Client` or `@EnableOAuth2Sso`). To prevent that infrastructure
2355+
to be defined, just remove the `security.oauth2.client.client-id` from your configuration
2356+
(or make it the empty string).
23412357

23422358

23432359

0 commit comments

Comments
 (0)