Skip to content

Commit e98264d

Browse files
author
Dave Syer
committed
Move base configuration class to a separate file
to stop it from being included in the enclosing @configuration. That way, if the app is not a web app, then there really is a client_credentials OAuth2 resource (as claimed in the user guide). Fixes gh-5735
1 parent 42af5eb commit e98264d

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.client;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Primary;
22+
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
23+
24+
/**
25+
* Common base class providing beans for authorization code clients. Does not work if
26+
* nested inside a <code>@Configuration</code> class because it is considered as
27+
* configuration.
28+
*/
29+
abstract class BaseConfiguration {
30+
31+
@Bean
32+
@ConfigurationProperties("security.oauth2.client")
33+
@Primary
34+
public AuthorizationCodeResourceDetails oauth2RemoteResource() {
35+
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
36+
return details;
37+
}
38+
39+
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
5151
import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest;
5252
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
53-
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
5453
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
5554
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
5655
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
@@ -78,19 +77,6 @@ public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientCon
7877
return template;
7978
}
8079

81-
@Configuration
82-
protected abstract static class BaseConfiguration {
83-
84-
@Bean
85-
@ConfigurationProperties("security.oauth2.client")
86-
@Primary
87-
public AuthorizationCodeResourceDetails oauth2RemoteResource() {
88-
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
89-
return details;
90-
}
91-
92-
}
93-
9480
@Configuration
9581
@ConditionalOnNotWebApplication
9682
protected static class SingletonScopedConfiguration {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/OAuth2AutoConfigurationTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
3939
import org.springframework.boot.test.EnvironmentTestUtils;
4040
import org.springframework.boot.test.TestRestTemplate;
41+
import org.springframework.context.ApplicationContext;
42+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4143
import org.springframework.context.annotation.Bean;
4244
import org.springframework.context.annotation.Configuration;
4345
import org.springframework.context.annotation.Import;
@@ -63,6 +65,7 @@
6365
import org.springframework.security.core.authority.AuthorityUtils;
6466
import org.springframework.security.crypto.codec.Base64;
6567
import org.springframework.security.oauth2.client.OAuth2ClientContext;
68+
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
6669
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
6770
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
6871
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
@@ -183,6 +186,18 @@ public void testClientIsNotResourceServer() {
183186
assertThat(countBeans(OAuth2ClientContext.class), equalTo(2));
184187
}
185188

189+
@Test
190+
public void testClientIsNotAuthCode() {
191+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
192+
context.register(MinimalSecureNonWebApplication.class);
193+
EnvironmentTestUtils.addEnvironment(context,
194+
"security.oauth2.client.clientId=client");
195+
context.refresh();
196+
assertThat(countBeans(context, ClientCredentialsResourceDetails.class),
197+
equalTo(1));
198+
context.close();
199+
}
200+
186201
@Test
187202
public void testDisablingAuthorizationServer() {
188203
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
@@ -363,7 +378,11 @@ private void assertEndpointUnauthorized(String baseUrl, RestTemplate rest) {
363378
}
364379

365380
private int countBeans(Class<?> type) {
366-
return this.context.getBeanNamesForType(type).length;
381+
return countBeans(this.context, type);
382+
}
383+
384+
private int countBeans(ApplicationContext context, Class<?> type) {
385+
return context.getBeanNamesForType(type).length;
367386
}
368387

369388
@Configuration
@@ -375,6 +394,12 @@ protected static class MinimalSecureWebApplication {
375394

376395
}
377396

397+
@Configuration
398+
@Import({ SecurityAutoConfiguration.class, OAuth2AutoConfiguration.class })
399+
protected static class MinimalSecureNonWebApplication {
400+
401+
}
402+
378403
@Configuration
379404
protected static class TestSecurityConfiguration
380405
extends WebSecurityConfigurerAdapter {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
4+
<logger name="org.springframework.cloud.zookeeper" level="DEBUG"/>
5+
</configuration>

0 commit comments

Comments
 (0)