2

in fact the spring-oauth project turn into maintenance mode we trying migrate our application into pure spring security 5 which support resource server configuration as well.

Our actual resource server configuration looks like this:

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests(
            authorizeRequests -> {
                authorizeRequests.antMatchers("/api/unsecured/**").permitAll();
                authorizeRequests.anyRequest().authenticated();
            }
        );
    }

    @Bean
    @ConfigurationProperties(prefix = "security.oauth2.client")
    public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
        return new ClientCredentialsResourceDetails();
    }

    @Bean
    public TokenStore jwkTokenStore() {
        return new JwkTokenStore("http://localhost:8080/...", new JwtAccessTokenConverter());
    }

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(){
        return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails());
    }

    @Bean
    public OAuth2RestTemplate clientCredentialsRestTemplate() {
        return new OAuth2RestTemplate(clientCredentialsResourceDetails());
    }
}

and these properties:

security:
  oauth2:
    client:
      client-id: service-id
      client-secret: secret
      access-token-uri: http://localhost:8081/oauth/token

This resource server is configured to work with jwt token. To verify token uses rsa public key from link passes to jwkstore. It is also able call another resource server with Feign.

And this is new configuration:

    @Configuration
    static class OAuth2ResourceServerConfig extends WebSecurityConfigurerAdapter {

        private final JwtDecoder jwtDecoder;

        ResourceServerConfiguration(JwtDecoder jwtDecoder) {
            this.jwtDecoder = jwtDecoder;
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests(authorizeRequests -> authorizeRequests
                    .antMatchers("/public/unsecured/**").permitAll()
                    .anyRequest().authenticated())
                .sessionManagement(session ->
                    session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                )
                .oauth2ResourceServer(oauth2ResourceServer -> oauth2ResourceServer
                    .jwt(jwtConfigurer -> {
                        jwtConfigurer.decoder(NimbusJwtDecoder.withJwkSetUri("http://localhost:8080/...").build());
                        jwtConfigurer.jwtAuthenticationConverter(tokenExtractor());
                    })
                );
        }

This configuration works fine to decode and verify tokens, but Feign doesn't work. Previous configuration with spring oauth2 supports Oauth2 feign interceptor which call authorization server to get its own access token. But I don't know how to configure this in spring security 5. This is flow which I need:

  1. frontend client call spring resource server A with token
  2. resource server A need data from resource server B
  3. resource server A call authorization server to get access token with client_credentials grant type
  4. resource server A call resource server B with its access token set to request header by feign
  5. resource server A return all data to frontend client

Can you tell me how to configure 3. and 4. step in spring security 5 without spring's oauth project? Thank you.

Denis Stephanov
  • 4,563
  • 24
  • 78
  • 174
  • Why not pass the token of step 1? – Chris Mar 27 '20 at 14:12
  • @theshadog in my knowledge resource server which need data from another one should behave as oauth client, and get its own access token with grant type "client_credentials". This token can has different scope or authorities. Technically your solution should works but I am not sure if it right. – Denis Stephanov Mar 28 '20 at 01:24

0 Answers0