You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/main/asciidoc/spring-cloud-gateway.adoc
+44-5Lines changed: 44 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -2032,7 +2032,46 @@ consumer can be a pure Client (like an SSO application) or a Resource
2032
2032
Server.
2033
2033
2034
2034
Spring Cloud Gateway can forward OAuth2 access tokens downstream to the services
2035
-
it is proxying. To add this functionality to the gateway, you need to add the `TokenRelayGatewayFilterFactory` like this:
2035
+
it is proxying using the `TokenRelay` `GatewayFilter`.
2036
+
2037
+
The `TokenRelay` `GatewayFilter` takes one optional parameter, `clientRegistrationId`.
2038
+
The following example configures a `TokenRelay` `GatewayFilter`:
2039
+
2040
+
.App.java
2041
+
[source,java]
2042
+
----
2043
+
2044
+
@Bean
2045
+
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
2046
+
return builder.routes()
2047
+
.route("resource", r -> r.path("/resource")
2048
+
.filters(f -> f.tokenRelay("myregistrationid"))
2049
+
.uri("http://localhost:9000"))
2050
+
.build();
2051
+
}
2052
+
----
2053
+
2054
+
or this
2055
+
2056
+
.application.yaml
2057
+
[source,yaml]
2058
+
----
2059
+
spring:
2060
+
cloud:
2061
+
gateway:
2062
+
routes:
2063
+
- id: resource
2064
+
uri: http://localhost:9000
2065
+
predicates:
2066
+
- Path=/resource
2067
+
filters:
2068
+
- TokenRelay=myregistrationid
2069
+
----
2070
+
2071
+
The example above specifies a `clientRegistrationId`, which can be used to obtain and forward an OAuth2 access token for any available `ClientRegistration`.
2072
+
2073
+
Spring Cloud Gateway can also forward the OAuth2 access token of the currently authenticated user `oauth2Login()` is used to authenticate the user.
2074
+
To add this functionality to the gateway, you can omit the `clientRegistrationId` parameter like this:
2036
2075
2037
2076
.App.java
2038
2077
[source,java]
@@ -2073,10 +2112,10 @@ To enable this for Spring Cloud Gateway add the following dependencies
Copy file name to clipboardExpand all lines: spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/TokenRelayGatewayFilterFactory.java
+32-14Lines changed: 32 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2002-2018 the original author or authors.
2
+
* Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java
+20-2Lines changed: 20 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -796,12 +796,13 @@ public GatewayFilterSpec setRequestHeaderSize(DataSize size) {
796
796
}
797
797
798
798
/**
799
-
* A filter that enables token relay.
799
+
* A filter that enables token relay by extracting the access token from the currently
800
+
* authenticated user and puts it in a request header for downstream requests.
800
801
* @return a {@link GatewayFilterSpec} that can be used to apply additional filters
Copy file name to clipboardExpand all lines: spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/TokenRelayGatewayFilterFactoryTests.java
0 commit comments