Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security with OAuth2 losing session

We have a Spring Boot-based Gateway using Spring Security, OAuth2 login, and Zuul routing. It is also using Spring Session to store sessions in Redis. This Gateway stores an OAuth2 token in the session and forwards the OAuth2 Bearer token to backend services.

We have an issue where users are being signed out quite often. It appears this happens roughly hourly. We are not even quite sure what is causing this with all the different tools in place.

Our session cookie in the browser expires in a longer period of time. So I suspect it is either Spring invalidating the session, or the OAuth2 token expiring.

From a quick inspection of the code, it appears that OAuth2TokenRelayFilter supports refreshing the token. Is this correct?

How can track down the cause of this and fix it?

For reference, we are using these versions:

  • Spring Boot 2.1.12
  • Spring Cloud Greenwich.SR4

Here are some relevant snippets.

Our web security config for the web pages.

@Configuration
@EnableWebSecurity
@EnableOAuth2Sso
@Order(SecurityProperties.BASIC_AUTH_ORDER - 2)
@Profile("!security-disabled")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
            http
                .authorizeRequests()
                    .antMatchers("/login", "/login/**", "/favicon.ico").permitAll()
                    .antMatchers("/signout").authenticated()
                    .anyRequest().hasAnyRole("ADMIN", "MEMBER")
                    .and()
                .csrf()
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                    .and()
                .httpBasic()
                    .disable()
                .formLogin()
                    .disable()
                .logout()
                    .logoutUrl("/signout")
                    .deleteCookies("SESSION")
                    .and()
            // @formatter:on
    }

Security configuration for API paths.

@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 2 - 10)
@Profile("!security-disabled")
public class ApiSecurityConfig extends WebSecurityConfigurerAdapter
{
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
            http.requestMatchers()
                    .antMatchers("/api/**")
                    .and()
                    .authorizeRequests()
                    .antMatchers("/**").hasAnyRole("ADMIN", "MEMBER")
                        .and()
                    .csrf()
                        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                        .and()
                    .headers()
                        .frameOptions().sameOrigin()
                        .and()
                    .httpBasic()
                        .disable()
                    .formLogin()
                        .disable()
                    .logout()
                        .disable()
                    .exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint());
            // @formatter:on
    }

}

Update

We have done some debugging of the Spring internals. First, we found that we were missing an OAuth2RestTemplate. Per the OAuth2 Boot documentation we found how to add it with:

@Bean
public OAuth2RestTemplate oauth2RestTemplate(
        OAuth2ClientContext oauth2ClientContext,
        OAuth2ProtectedResourceDetails details)
{
    return new OAuth2RestTemplate(details, oauth2ClientContext);
}

This is now throwing an exception when OAuth2TokenRelayFilter calls restTemplate.getAccessToken().getValue();.

A redirect is required to get the users approval

This exception is thrown from AuthorizationCodeAccessTokenProvider.

like image 772
David V Avatar asked Nov 06 '22 07:11

David V


1 Answers

OAuth2TokenRelayFilter

OAuth2TokenRelayFilter is a pre type filter which set the contexts with ACCESS_TOKEN and TOKEN_TYPE which will be used for the further authentication. It validates the tokens using getAccessToken() method and responds with "Cannot obtain valid access token" with 401 status.

You may check the validity of tokens and refresh token is correctly configured with grant_type as refresh_token as The Refresh Token grant type is used by clients to exchange a refresh token for an access token when the access token has expired which allows clients to continue to have a valid access token without further interaction with the user.

In case if you want to disable OAuth2TokenRelayFilter, you may use the following

zuul.OAuth2TokenRelayFilter.pre.disable=true
like image 172
Romil Patel Avatar answered Nov 12 '22 18:11

Romil Patel