Skip to content

Commit 05e3baf

Browse files
committed
Migrates token relay from spring-cloud-gateway-server-security.
It moves it to spring-cloud-gateway-server See gh-1975
1 parent dfa048f commit 05e3baf

File tree

11 files changed

+122
-217
lines changed

11 files changed

+122
-217
lines changed

docs/src/main/asciidoc/spring-cloud-gateway.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,14 +1644,12 @@ it is proxying. To add this functionlity to gateway you need to add the
16441644
.App.java
16451645
[source,java]
16461646
----
1647-
@Autowired
1648-
private TokenRelayGatewayFilterFactory filterFactory;
16491647
16501648
@Bean
16511649
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
16521650
return builder.routes()
16531651
.route("resource", r -> r.path("/resource")
1654-
.filters(f -> f.filter(filterFactory.apply()))
1652+
.filters(f -> f.tokenRelay())
16551653
.uri("http://localhost:9000"))
16561654
.build();
16571655
}
@@ -1680,7 +1678,7 @@ pass the authentication token downstream to the services (in this case
16801678

16811679
To enable this for Spring Cloud Gateway add the following dependencies
16821680

1683-
- `org.springframework.cloud:spring-cloud-gateway-server-security`
1681+
- `org.springframework.boot:spring-boot-starter-oauth2-client`
16841682

16851683
How does it work? The
16861684
{githubmaster}/src/main/java/org/springframework/cloud/gateway/security/TokenRelayGatewayFilterFactory.java[filter]
@@ -1689,6 +1687,8 @@ and puts it in a request header for the downstream requests.
16891687

16901688
For a full working sample see https://github.com/spring-cloud-samples/sample-gateway-oauth2login[this project].
16911689

1690+
NOTE: A `TokenRelayGatewayFilterFactory` bean will only be created if the proper `spring.security.oauth2.client.*` properties are set which will trigger creation of a `ReactiveClientRegistrationRepository` bean.
1691+
16921692
NOTE: The default implementation of `ReactiveOAuth2AuthorizedClientService` used by `TokenRelayGatewayFilterFactory`
16931693
uses an in-memory data store. You will need to provide your own implementation `ReactiveOAuth2AuthorizedClientService`
16941694
if you need a more robust solution.

spring-cloud-gateway-server-security/pom.xml

Lines changed: 0 additions & 81 deletions
This file was deleted.

spring-cloud-gateway-server-security/src/main/java/org/springframework/cloud/gateway/security/TokenRelayAutoConfiguration.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

spring-cloud-gateway-server-security/src/main/resources/META-INF/spring.factories

Lines changed: 0 additions & 3 deletions
This file was deleted.

spring-cloud-gateway-server-security/src/test/java/org/springframework/cloud/gateway/security/TokenRelayAutoConfigurationTests.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

spring-cloud-gateway-server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<groupId>org.springframework.boot</groupId>
2828
<artifactId>spring-boot-starter-validation</artifactId>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-oauth2-client</artifactId>
33+
<optional>true</optional>
34+
</dependency>
3035
<dependency>
3136
<groupId>org.springframework.boot</groupId>
3237
<artifactId>spring-boot-starter-actuator</artifactId>

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4646
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4747
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
48+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
4849
import org.springframework.boot.autoconfigure.web.ServerProperties;
4950
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
5051
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
@@ -96,6 +97,7 @@
9697
import org.springframework.cloud.gateway.filter.factory.SetResponseHeaderGatewayFilterFactory;
9798
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
9899
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
100+
import org.springframework.cloud.gateway.filter.factory.TokenRelayGatewayFilterFactory;
99101
import org.springframework.cloud.gateway.filter.factory.rewrite.GzipMessageBodyResolver;
100102
import org.springframework.cloud.gateway.filter.factory.rewrite.MessageBodyDecoder;
101103
import org.springframework.cloud.gateway.filter.factory.rewrite.MessageBodyEncoder;
@@ -148,6 +150,14 @@
148150
import org.springframework.core.convert.ConversionService;
149151
import org.springframework.core.env.Environment;
150152
import org.springframework.http.codec.ServerCodecConfigurer;
153+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
154+
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager;
155+
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProvider;
156+
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProviderBuilder;
157+
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
158+
import org.springframework.security.oauth2.client.web.DefaultReactiveOAuth2AuthorizedClientManager;
159+
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
160+
import org.springframework.security.web.server.SecurityWebFilterChain;
151161
import org.springframework.util.CollectionUtils;
152162
import org.springframework.util.StringUtils;
153163
import org.springframework.validation.Validator;
@@ -831,4 +841,31 @@ static class VerboseDisabled {
831841

832842
}
833843

844+
@Configuration(proxyBeanMethods = false)
845+
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
846+
@ConditionalOnClass({ OAuth2AuthorizedClient.class, SecurityWebFilterChain.class, SecurityProperties.class })
847+
@ConditionalOnEnabledFilter(TokenRelayGatewayFilterFactory.class)
848+
@ConditionalOnBean(ReactiveClientRegistrationRepository.class)
849+
protected static class TokenRelayConfiguration {
850+
851+
@Bean
852+
public TokenRelayGatewayFilterFactory tokenRelayGatewayFilterFactory(
853+
ReactiveOAuth2AuthorizedClientManager clientManager) {
854+
return new TokenRelayGatewayFilterFactory(clientManager);
855+
}
856+
857+
@Bean
858+
public ReactiveOAuth2AuthorizedClientManager gatewayReactiveOAuth2AuthorizedClientManager(
859+
ReactiveClientRegistrationRepository clientRegistrationRepository,
860+
ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
861+
ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder
862+
.builder().authorizationCode().refreshToken().build();
863+
DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager = new DefaultReactiveOAuth2AuthorizedClientManager(
864+
clientRegistrationRepository, authorizedClientRepository);
865+
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
866+
return authorizedClientManager;
867+
}
868+
869+
}
870+
834871
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.cloud.gateway.security;
17+
package org.springframework.cloud.gateway.filter.factory;
1818

1919
import reactor.core.publisher.Mono;
2020

2121
import org.springframework.cloud.gateway.filter.GatewayFilter;
22-
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
2322
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
2423
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
2524
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager;
2625
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
2726
import org.springframework.security.oauth2.core.OAuth2AccessToken;
28-
import org.springframework.stereotype.Component;
2927
import org.springframework.web.server.ServerWebExchange;
3028

3129
/**
3230
* @author Joe Grandja
3331
*/
34-
@Component
3532
public class TokenRelayGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
3633

3734
private final ReactiveOAuth2AuthorizedClientManager clientManager;

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
6868
import org.springframework.cloud.gateway.filter.factory.SpringCloudCircuitBreakerFilterFactory;
6969
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
70+
import org.springframework.cloud.gateway.filter.factory.TokenRelayGatewayFilterFactory;
7071
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory;
7172
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
7273
import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction;
@@ -700,6 +701,21 @@ public GatewayFilterSpec setRequestHeaderSize(DataSize size) {
700701
return filter(getBean(RequestHeaderSizeGatewayFilterFactory.class).apply(c -> c.setMaxSize(size)));
701702
}
702703

704+
/**
705+
* A filter that enables token relay.
706+
* @return a {@link GatewayFilterSpec} that can be used to apply additional filters
707+
*/
708+
public GatewayFilterSpec tokenRelay() {
709+
try {
710+
return filter(getBean(TokenRelayGatewayFilterFactory.class).apply(o -> {
711+
}));
712+
}
713+
catch (NoSuchBeanDefinitionException e) {
714+
throw new IllegalStateException("No TokenRelayGatewayFilterFactory bean was found. Did you include the "
715+
+ "org.springframework.boot:spring-boot-starter-oauth2-client dependency?");
716+
}
717+
}
718+
703719
/**
704720
* Adds hystrix execution exception headers to fallback request. Depends on @{code
705721
* org.springframework.cloud::spring-cloud-starter-netflix-hystrix} being on the

0 commit comments

Comments
 (0)