Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security with oidc: refresh the tokens

Spring Boot 2 with Spring Security 5 can be configured to use an openID connect ID provider for authentication. I managed to setup up my project just by configuring Spring Security - that works fine with all kinds of perfectly preconfigured security mechanisms like mitigation of session fixation.

But it seems that Spring Security does not refresh the tokens (which are stored in the session) by itself when they are expired.

Is there a setting for that or do I have to care for the refresh myself?

Update: Spring Boot 2.1 has been released, so it is time to revisit this problem. I still have no clue if the accessToken can now be automatically refreshed or if I have to write code for doing so...

like image 218
rdmueller Avatar asked Aug 10 '18 15:08

rdmueller


People also ask

What is OIDC refresh token?

A refresh token is a special token that is used to obtain additional access tokens. This allows you to have short-lived access tokens without having to collect credentials every time one expires.

How do I make my refresh token secure?

Protecting your refresh tokens Concretely, refresh tokens exposed to the browser should be protected with Refresh Token Rotation (RTR). In a nutshell, RTR makes refresh tokens only valid for one-time use. Each time a refresh token is used, the security token service issues a new access token and a new refresh token.

How to secure a REST API using Spring Security with token based authentication?

These are the main configuration classes to secure a REST API using Spring Security with token based authentication.In this section, we will talk about following classes: AuthenticationProvider : Find the user by its authentication token. AuthenticationFilter :Extract the authentication token from the request headers

Does Spring Security refresh expired tokens by itself?

But it seems that Spring Security does not refresh the tokens (which are stored in the session) by itself when they are expired. Is there a setting for that or do I have to care for the refresh myself? Update: Spring Boot 2.1 has been released, so it is time to revisit this problem.

Which OAuth stack do we use in Spring Security 5?

We'll use the OAuth stack in Spring Security 5. If you want to use the Spring Security OAuth legacy stack, have a look at this previous article: OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS (legacy OAuth stack)

What does a spring based token authentication provider look like?

This is how our Spring based token authentication provider looks like: Our AuthenticationProvider use the CustomerService to find a customer based on the token. The token authentication filter is responsible to get the authentication filter from the header and call the authentication manager for authentication.


1 Answers

According to the documentation,

https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#webclient

When using a WebClient configured correctly, as given in the documentation it will automatically be refreshed.

Spring Security will automatically refresh expired tokens (if a refresh token is present)

This is also supported by the features matrix that refresh tokens are supported.

https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Features-Matrix

There was an older blog on Spring Security 5 that gives you access to beans that you could do this manually,

Authentication authentication =
    SecurityContextHolder
        .getContext()
        .getAuthentication();

OAuth2AuthenticationToken oauthToken =
    (OAuth2AuthenticationToken) authentication;

There will be an OAuth2AuthorizedClientService automatically configured as a bean in the Spring application context, so you’ll only need to inject it into wherever you’ll use it.

OAuth2AuthorizedClient client =
    clientService.loadAuthorizedClient(
            oauthToken.getAuthorizedClientRegistrationId(),
            oauthToken.getName());

String refreshToken = client.getRefreshToken();

And, failing to find it right now, but I assume as part of the OAuth2AuthorizedClientExchangeFilterFunction has the calls to do a refresh.

like image 88
Darren Forsythe Avatar answered Sep 19 '22 15:09

Darren Forsythe