Skip to content

Commit 3bc2267

Browse files
jaimesf-santaluciajaimesf
authored andcommitted
Support token relay clientRegistrationId on properties
Signed-off-by: Jaime Sánchez <[email protected]>
1 parent 03dcf23 commit 3bc2267

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway-server-webmvc/filters/tokenrelay.adoc

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@ forwards the incoming token to outgoing resource requests. The
66
consumer can be a pure Client (like an SSO application) or a Resource
77
Server.
88

9-
////
10-
TODO: support TokenRelay clientRegistrationId
119
Spring Cloud Gateway Server MVC can forward OAuth2 access tokens downstream to the services
1210
it is proxying using the `TokenRelay` filter.
1311

1412
The `TokenRelay` filter takes one optional parameter, `clientRegistrationId`.
1513
The following example configures a `TokenRelay` filter:
1614

17-
.App.java
15+
.RouteConfiguration.java
1816
[source,java]
1917
----
2018
21-
@Bean
22-
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
23-
return builder.routes()
24-
.route("resource", r -> r.path("/resource")
25-
.filters(f -> f.tokenRelay("myregistrationid"))
26-
.uri("http://localhost:9000"))
19+
@Configuration
20+
class RouteConfiguration {
21+
22+
@Bean
23+
public RouterFunction<ServerResponse> gatewayRouterFunctionsTokenRelay() {
24+
return route("resource")
25+
.GET("/resource", http())
26+
.before(uri("https://localhost:9000"))
27+
.filter(tokenRelay("myregistrationid"))
2728
.build();
29+
}
2830
}
2931
----
3032

@@ -46,19 +48,13 @@ spring:
4648
----
4749

4850
The example above specifies a `clientRegistrationId`, which can be used to obtain and forward an OAuth2 access token for any available `ClientRegistration`.
49-
////
5051

5152
Spring Cloud Gateway Server MVC can forward the OAuth2 access token of the currently authenticated user `oauth2Login()` is used to authenticate the user.
52-
//To add this functionality to the gateway, you can omit the `clientRegistrationId` parameter like this:
53+
To add this functionality to the gateway, you can omit the `clientRegistrationId` parameter like this:
5354

5455
.RouteConfiguration.java
5556
[source,java]
5657
----
57-
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
58-
import static org.springframework.cloud.gateway.server.mvc.filter.TokenRelayFilterFunctions.tokenRelay;
59-
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
60-
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
61-
6258
@Configuration
6359
class RouteConfiguration {
6460
@@ -100,9 +96,9 @@ To enable this for Spring Cloud Gateway Server MVC add the following dependencie
10096
- `org.springframework.boot:spring-boot-starter-oauth2-client`
10197

10298
How does it work?
103-
// The filter extracts an OAuth2 access token from the currently authenticated user for the provided `clientRegistrationId`.
104-
// If no `clientRegistrationId` is provided,
105-
The currently authenticated user's own access token (obtained during login) is used and the extracted access token is placed in a request header for the downstream requests.
99+
The filter extracts an OAuth2 access token from the currently authenticated user for the provided `clientRegistrationId`.
100+
If no `clientRegistrationId` is provided,
101+
the currently authenticated user's own access token (obtained during login) is used and the extracted access token is placed in a request header for the downstream requests.
106102

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

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/TokenRelayFilterFunctions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> tokenRelay()
3939
return tokenRelay(null);
4040
}
4141

42+
@Shortcut
4243
public static HandlerFilterFunction<ServerResponse, ServerResponse> tokenRelay(String defaultClientRegistrationId) {
4344
return (request, next) -> {
4445
Authentication principal = (Authentication) request.servletRequest().getUserPrincipal();

0 commit comments

Comments
 (0)