Skip to content

Commit 9f4a5c1

Browse files
committed
Add auto-config for WebFlux OAuth2 Login
Closes spring-projectsgh-13142
1 parent 792f0b1 commit 9f4a5c1

24 files changed

+751
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2018 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+
package org.springframework.boot.autoconfigure.security.oauth2.client;
17+
18+
import java.util.Collections;
19+
import java.util.Map;
20+
import java.util.stream.Collectors;
21+
22+
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
23+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
24+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
25+
import org.springframework.boot.context.properties.bind.Bindable;
26+
import org.springframework.boot.context.properties.bind.Binder;
27+
import org.springframework.context.annotation.ConditionContext;
28+
import org.springframework.core.env.Environment;
29+
import org.springframework.core.type.AnnotatedTypeMetadata;
30+
31+
/**
32+
* Condition that matches if any {@code spring.security.oauth2.client.registration}
33+
* properties are defined.
34+
*/
35+
public class ClientsConfiguredCondition extends SpringBootCondition {
36+
37+
private static final Bindable<Map<String, OAuth2ClientProperties.Registration>> BINDABLE_REGISTRATION = Bindable
38+
.mapOf(String.class, OAuth2ClientProperties.Registration.class);
39+
40+
@Override
41+
public ConditionOutcome getMatchOutcome(ConditionContext context,
42+
AnnotatedTypeMetadata metadata) {
43+
ConditionMessage.Builder message = ConditionMessage
44+
.forCondition("OAuth2 Clients Configured Condition");
45+
Map<String, OAuth2ClientProperties.Registration> registrations = this
46+
.getRegistrations(context.getEnvironment());
47+
if (!registrations.isEmpty()) {
48+
return ConditionOutcome.match(message
49+
.foundExactly("registered clients " + registrations.values().stream()
50+
.map(OAuth2ClientProperties.Registration::getClientId)
51+
.collect(Collectors.joining(", "))));
52+
}
53+
return ConditionOutcome.noMatch(message.notAvailable("registered clients"));
54+
}
55+
56+
private Map<String, OAuth2ClientProperties.Registration> getRegistrations(
57+
Environment environment) {
58+
return Binder.get(environment)
59+
.bind("spring.security.oauth2.client.registration", BINDABLE_REGISTRATION)
60+
.orElse(Collections.emptyMap());
61+
}
62+
63+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @author Thiago Hirata
4040
* @since 2.0.0
4141
*/
42-
final class OAuth2ClientPropertiesRegistrationAdapter {
42+
public final class OAuth2ClientPropertiesRegistrationAdapter {
4343

4444
private OAuth2ClientPropertiesRegistrationAdapter() {
4545
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
/**
18-
* Auto-configuration for Spring Security's OAuth 2 client.
18+
* Support for Spring Security's OAuth 2 client.
1919
*/
2020
package org.springframework.boot.autoconfigure.security.oauth2.client;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2018 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+
package org.springframework.boot.autoconfigure.security.oauth2.client.reactive;
17+
18+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
19+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
22+
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.Import;
25+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
26+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
27+
28+
/**
29+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Security's Reactive
30+
* OAuth2 client.
31+
*
32+
* @author Madhura Bhave
33+
* @since 2.1.0
34+
*/
35+
@Configuration
36+
@AutoConfigureBefore(ReactiveSecurityAutoConfiguration.class)
37+
@ConditionalOnClass({ EnableWebFluxSecurity.class, ClientRegistration.class })
38+
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
39+
@Import({ ReactiveOAuth2ClientRegistrationRepositoryConfiguration.class,
40+
ReactiveOAuth2WebSecurityConfiguration.class })
41+
public class ReactiveOAuth2ClientAutoConfiguration {
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2018 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+
package org.springframework.boot.autoconfigure.security.oauth2.client.reactive;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition;
23+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
24+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter;
25+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Conditional;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
30+
import org.springframework.security.oauth2.client.registration.InMemoryReactiveClientRegistrationRepository;
31+
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
32+
33+
/**
34+
* {@link Configuration} used to map {@link OAuth2ClientProperties} to client
35+
* registrations.
36+
*
37+
* @author Madhura Bhave
38+
*/
39+
@Configuration
40+
@EnableConfigurationProperties(OAuth2ClientProperties.class)
41+
@Conditional(ClientsConfiguredCondition.class)
42+
class ReactiveOAuth2ClientRegistrationRepositoryConfiguration {
43+
44+
private final OAuth2ClientProperties properties;
45+
46+
ReactiveOAuth2ClientRegistrationRepositoryConfiguration(
47+
OAuth2ClientProperties properties) {
48+
this.properties = properties;
49+
}
50+
51+
@Bean
52+
@ConditionalOnMissingBean(ReactiveClientRegistrationRepository.class)
53+
public InMemoryReactiveClientRegistrationRepository clientRegistrationRepository() {
54+
List<ClientRegistration> registrations = new ArrayList<>(
55+
OAuth2ClientPropertiesRegistrationAdapter
56+
.getClientRegistrations(this.properties).values());
57+
return new InMemoryReactiveClientRegistrationRepository(registrations);
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2018 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+
package org.springframework.boot.autoconfigure.security.oauth2.client.reactive;
17+
18+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.security.oauth2.client.InMemoryReactiveOAuth2AuthorizedClientService;
23+
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientService;
24+
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
25+
26+
/**
27+
* {@link Configuration} used to create an in-memory
28+
* {@link ReactiveOAuth2AuthorizedClientService}.
29+
*
30+
* @author Madhura Bhave
31+
*/
32+
@Configuration
33+
public class ReactiveOAuth2WebSecurityConfiguration {
34+
35+
@Bean
36+
@ConditionalOnBean(ReactiveClientRegistrationRepository.class)
37+
@ConditionalOnMissingBean
38+
public ReactiveOAuth2AuthorizedClientService authorizedClientService(
39+
ReactiveClientRegistrationRepository clientRegistrationRepository) {
40+
return new InMemoryReactiveOAuth2AuthorizedClientService(
41+
clientRegistrationRepository);
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2018 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+
/**
18+
* Auto-configuration for Spring Security's Reactive OAuth 2 client.
19+
*/
20+
package org.springframework.boot.autoconfigure.security.oauth2.client.reactive;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.security.oauth2.client;
17+
package org.springframework.boot.autoconfigure.security.oauth2.client.servlet;
1818

1919
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2020
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.security.oauth2.client;
17+
package org.springframework.boot.autoconfigure.security.oauth2.client.servlet;
1818

1919
import java.util.ArrayList;
20-
import java.util.Collections;
2120
import java.util.List;
22-
import java.util.Map;
23-
import java.util.stream.Collectors;
2421

25-
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
26-
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
2722
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28-
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
29-
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration;
23+
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition;
24+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
25+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter;
3026
import org.springframework.boot.context.properties.EnableConfigurationProperties;
31-
import org.springframework.boot.context.properties.bind.Bindable;
32-
import org.springframework.boot.context.properties.bind.Binder;
3327
import org.springframework.context.annotation.Bean;
34-
import org.springframework.context.annotation.ConditionContext;
3528
import org.springframework.context.annotation.Conditional;
3629
import org.springframework.context.annotation.Configuration;
37-
import org.springframework.core.env.Environment;
38-
import org.springframework.core.type.AnnotatedTypeMetadata;
3930
import org.springframework.security.oauth2.client.registration.ClientRegistration;
4031
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
4132
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
@@ -45,11 +36,10 @@
4536
* registrations.
4637
*
4738
* @author Madhura Bhave
48-
* @author Phillip Webb
4939
*/
5040
@Configuration
5141
@EnableConfigurationProperties(OAuth2ClientProperties.class)
52-
@Conditional(OAuth2ClientRegistrationRepositoryConfiguration.ClientsConfiguredCondition.class)
42+
@Conditional(ClientsConfiguredCondition.class)
5343
class OAuth2ClientRegistrationRepositoryConfiguration {
5444

5545
private final OAuth2ClientProperties properties;
@@ -67,38 +57,4 @@ public InMemoryClientRegistrationRepository clientRegistrationRepository() {
6757
return new InMemoryClientRegistrationRepository(registrations);
6858
}
6959

70-
/**
71-
* Condition that matches if any {@code spring.security.oauth2.client.registration}
72-
* properties are defined.
73-
*/
74-
static class ClientsConfiguredCondition extends SpringBootCondition {
75-
76-
private static final Bindable<Map<String, Registration>> BINDABLE_REGISTRATION = Bindable
77-
.mapOf(String.class, OAuth2ClientProperties.Registration.class);
78-
79-
@Override
80-
public ConditionOutcome getMatchOutcome(ConditionContext context,
81-
AnnotatedTypeMetadata metadata) {
82-
ConditionMessage.Builder message = ConditionMessage
83-
.forCondition("OAuth2 Clients Configured Condition");
84-
Map<String, Registration> registrations = this
85-
.getRegistrations(context.getEnvironment());
86-
if (!registrations.isEmpty()) {
87-
return ConditionOutcome.match(message.foundExactly(
88-
"registered clients " + registrations.values().stream()
89-
.map(OAuth2ClientProperties.Registration::getClientId)
90-
.collect(Collectors.joining(", "))));
91-
}
92-
return ConditionOutcome.noMatch(message.notAvailable("registered clients"));
93-
}
94-
95-
private Map<String, Registration> getRegistrations(Environment environment) {
96-
return Binder.get(environment)
97-
.bind("spring.security.oauth2.client.registration",
98-
BINDABLE_REGISTRATION)
99-
.orElse(Collections.emptyMap());
100-
}
101-
102-
}
103-
10460
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.security.oauth2.client;
17+
package org.springframework.boot.autoconfigure.security.oauth2.client.servlet;
1818

1919
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

0 commit comments

Comments
 (0)