-
Notifications
You must be signed in to change notification settings - Fork 243
Add automatic token refresh to TokenRelayGatewayFilterFactory #175
Add automatic token refresh to TokenRelayGatewayFilterFactory #175
Comments
We use |
It feels like the implementation of this would be really similar to |
Thanks for advice! I wrote a custom TokenRelayGatewayFilterFactory to implement the shouldRefresh and refreshAuthorizedClient code from ServerOAuth2AuthorizedClientExchangeFilterFunction, used webclient post instead of the origin ExchangeFunction, and it worked. |
Hi Alan210 Could you be so kind and share your implementation, please? Cheers |
We use TokenRelayGatewayFilterFactory with OAuth2 and the access token expiry is 1 min. |
Here is my implementation of the custom TokenRelayGatewayFilterFactory with automatic token refresh based on the functionality from class |
Thanks ;) |
this link do not open . can you let me see your demo? thanks |
The link works fine for me |
@RequestMapping("/") |
thank you . I can open it with VPN |
An alternative CustomTokenRelayGatewayFilterFactory This class is also inspired by I am also looking forward to the implementation of the access token refreshment in the class |
@tschlegel & @boal thanks both for sharing your implementations of token refresh; either of you willing to contribute this to spring-cloud-security? That way it's easier to pick up going forward. |
@spencergibb You can easily inherit automatic token refresh using the new (in 5.2.0) Take a look at |
I have a new TokenRelayFilter implementation that requires Spring Boot 2.2.0. See TokenRelayWithTokenRefreshGatewayFilterFactory. |
Thanks @tschlegel! That seems to work here; Also meant I could drop the dependency on |
Thanks @tschlegel & @boal for the samples. I'm getting below error when I tried to integrate the given custom token relay filter in my sample cloud gateway. Could you guys please help. My sample gateway follows the code https://github.com/benwilcock/spring-cloud-gateway-demo/tree/master/security-gateway/security-gateway APPLICATION FAILED TO START Description: Parameter 0 of constructor in gateway.config.TokenRelayWithTokenRefreshGatewayFilterFactory required a bean of type 'org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository' that could not be found. Action: Consider defining a bean of type 'org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository' in your configuration. |
You have to update to spring boot 2.2.x |
Thank you very much @tschlegel for the hint. I have upgraded it to 2.2.4. |
One interesting fact is in the controller REST method if I use @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient it will automatically renews the token when it is called, but it is missing in the TokenRelayFilter. |
The Websession replication using Redis also fails to replicate the Oauth2Token in the SecurityContext. When I tried to replicate the Websession using 2 instances of Cloud Gateway, the TokenRelayGatewayFilterFactory is not forwarding the token in the replicated instance since it is missing in the SecurityContext. Eventhough the replicated Websession has OAuth2AuthenticationToken in the SecurityContext, it don't have the access token and refresh token. |
Thanks for the input @tschlegel but I seem to be missing a configuration to allow your relay to work It tries to contact keycloak on a URL we don't use for refresh and gets 500 2020-02-21 09:32:19.058 ERROR 14024 --- [or-http-epoll-2] a.w.r.e.AbstractErrorWebExceptionHandler : [6666ee3e] 500 Server Error for HTTP GET "/login/oauth2/code/keycloak" org.springframework.security.oauth2.core.OAuth2AuthorizationException: [authorization_request_not_found] In another application we refresh by calling a url I assume (hope) this is just misconfiguration on my part |
Any plans when this issue will be fixed and released? |
I can confirm that the refresh functionality works with @tschlegel solution (Nov 21, 2019 comment). Thank you!! |
I've not been able to get it to work as we dont use authorisation code flow and it always seems to try that even though i remove the auth code enabling. |
The @tschlegel solution (Nov 21, 2019 comment) works for me after removing the |
Are there any plans to work on it? I'm stuck with 2.1.9.RELEASE version for spring boot and Greenwich.SR3 and cannot update my project because of this issue. |
Are there plans to implement the filter within the Spring lib? The solution of @tschlegel works fine with the current Spring boot versions. If there should be incompatibilities with @tschlegel solution in the future, then the proposed solution would always have to be adapted in the issues comments. Therefore it would make sense to implement the functionality in a Spring standard filter, because many projects will secure their application with a token. |
Adds refresh token support. Fixes spring-attic/spring-cloud-security#175 Fixes gh-1975 See spring-attic/spring-cloud-security#231
* Migrates token relay from spring-cloud-security. Adds refresh token support. Fixes spring-attic/spring-cloud-security#175 Fixes gh-1975 See spring-attic/spring-cloud-security#231
You migrated TokenRelay from spring-cloud-security but would you say where is the refresh functionality? |
it is also in gateway |
I'm somehow lost.
The client browser knows the session cookie only, and no token details. The backend service will bind some jobs based on the oAuth Token details (here the keycloak userId is used) BUT under parallel "load" (two parallel browser sessions are already enough) the gateway is using only ONE oAuth token, altrough the users have different token with different userIds. Because of Is the code "session safe" at this place? I don't have to code this, but as a black-box tester I like such errors to get more knowledge about internal workflows 😄 |
Hi @psytester , |
For sure! I belive that you have exaclty the same issue, because it's the same project ;-) In logfile I see the output for Principal Name Inspired by that change of Principal Name, I searched and found this SO article https://stackoverflow.com/questions/37499307/whats-the-principal-in-spring-security After some more research based on „principal-attribute“ shown here: https://www.keycloak.org/docs/latest/securing_apps/#_java_adapter_config
I think and hope the fix is a simple configuration change, because currently we are using in application.yaml
Changing it to And finally I'm not longer able to get the error with wrong token for the parallel logged-in users. Can someone confirm that this change is the correct place and was the root cause? Edit: |
* Migrates token relay from spring-cloud-security. Adds refresh token support. Fixes spring-attic/spring-cloud-security#175 Fixes gh-1975 See spring-attic/spring-cloud-security#231
I'm really sorry for dredging up this old issue -- but with Spring Boot 2.4.9, and Cloud 2020.0.3, I don't see |
Bug report
As of now
TokenRelayGatewayFilterFactory
cascades access token even if it expired. If the access token is expired, this filter should attempt to refresh the token if refresh tokens are supported, else get fresh new token before sending the request downstreamServerOAuth2AuthorizedClientExchangeFilterFunction
already does this forWebClient
based on the currentOAuth2AuthenticationToken
,TokenRelayGatewayFilterFactory
should emulate itThe text was updated successfully, but these errors were encountered: