Skip to content

Commit 3c93725

Browse files
committed
Updates tokenrelay.adoc for MVC
1 parent 7c275f5 commit 3c93725

File tree

1 file changed

+38
-30
lines changed
  • docs/modules/ROOT/pages/spring-cloud-gateway-server-mvc/filters

1 file changed

+38
-30
lines changed

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ 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-
Spring Cloud Gateway can forward OAuth2 access tokens downstream to the services
9+
////
10+
TODO: support TokenRelay clientRegistrationId
11+
Spring Cloud Gateway Server MVC can forward OAuth2 access tokens downstream to the services
1012
it is proxying using the `TokenRelay` filter.
1113
1214
The `TokenRelay` filter takes one optional parameter, `clientRegistrationId`.
@@ -44,21 +46,28 @@ spring:
4446
----
4547
4648
The example above specifies a `clientRegistrationId`, which can be used to obtain and forward an OAuth2 access token for any available `ClientRegistration`.
49+
////
4750

48-
Spring Cloud Gateway can also forward the OAuth2 access token of the currently authenticated user `oauth2Login()` is used to authenticate the user.
49-
To add this functionality to the gateway, you can omit the `clientRegistrationId` parameter like this:
51+
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:
5053

51-
.App.java
54+
.RouteConfiguration.java
5255
[source,java]
5356
----
54-
55-
@Bean
56-
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
57-
return builder.routes()
58-
.route("resource", r -> r.path("/resource")
59-
.filters(f -> f.tokenRelay())
60-
.uri("http://localhost:9000"))
61-
.build();
57+
import static org.springframework.cloud.gateway.server.mvc.filter.TokenRelayFilterFunctions.tokenRelay;
58+
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
59+
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
60+
61+
@Configuration
62+
class RouteConfiguration {
63+
64+
@Bean
65+
public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
66+
return route("resource")
67+
.GET("/resource", http("http://localhost:9000"))
68+
.filter(tokenRelay())
69+
.build();
70+
}
6271
}
6372
----
6473

@@ -70,34 +79,33 @@ or this
7079
spring:
7180
cloud:
7281
gateway:
73-
routes:
74-
- id: resource
75-
uri: http://localhost:9000
76-
predicates:
77-
- Path=/resource
78-
filters:
79-
- TokenRelay=
82+
mvc:
83+
routes:
84+
- id: resource
85+
uri: http://localhost:9000
86+
predicates:
87+
- Path=/resource
88+
filters:
89+
- TokenRelay=
8090
----
8191

8292
and it will (in addition to logging the user in and grabbing a token)
8393
pass the authentication token downstream to the services (in this case
8494
`/resource`).
8595

86-
To enable this for Spring Cloud Gateway add the following dependencies
96+
To enable this for Spring Cloud Gateway Server MVC add the following dependencies
8797

8898
- `org.springframework.boot:spring-boot-starter-oauth2-client`
8999

90-
How does it work? The {github-code}/src/main/java/org/springframework/cloud/gateway/security/TokenRelayGatewayFilterFactory.java[filter]
91-
extracts an OAuth2 access token from the currently authenticated user for the provided `clientRegistrationId`.
92-
If no `clientRegistrationId` is provided, the currently authenticated user's own access token (obtained during login) is used.
93-
In either case, the extracted access token is placed in a request header for the downstream requests.
100+
How does it work?
101+
// The filter extracts an OAuth2 access token from the currently authenticated user for the provided `clientRegistrationId`.
102+
// If no `clientRegistrationId` is provided,
103+
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.
94104

95-
For a full working sample see https://github.com/spring-cloud-samples/sample-gateway-oauth2login[this project].
105+
//For a full working sample see https://github.com/spring-cloud-samples/sample-gateway-oauth2login[this project].
96106

97-
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.
107+
NOTE: The Token Relay filter will only work if the proper `spring.security.oauth2.client.*` properties are set which will trigger creation of a `OAuth2AuthorizedClientManager` bean.
98108

99-
NOTE: The default implementation of `ReactiveOAuth2AuthorizedClientService` used by `TokenRelayGatewayFilterFactory`
100-
uses an in-memory data store. You will need to provide your own implementation `ReactiveOAuth2AuthorizedClientService`
109+
NOTE: The default implementation used by the Token Relay filter
110+
uses an in-memory data store. You will need to provide your own implementation `OAuth2AuthorizedClientService`
101111
if you need a more robust solution.
102-
103-

0 commit comments

Comments
 (0)