Spring cloud feign Security consumer calls 401

return authentication error
Full authentication is required to access this resource

when using feign consumer to call provider

detailed error
{"timestamp": 1524018915085, "status": 401, "error": "Unauthorized", "message": "Full authentication is required to access this resource", "path": "/ activity-provider-service/user/add/12312321"}

Direct access to the provider can be successful, and this error will be reported by invoking the provider service through the consumer

if you turn off security authentication, you can access it successfully

clipboard.png

clipboard.png

clipboard.png

clipboard.png

Mar.07,2021

have you solved


solution add interceptor

  1. add yml profile
security:
  oauth2:
    client:
      clientId: yourClientId
      clientSecret: yourClientSecret
      accessTokenUri: http://localhost:3000/oauth/token
      grant-type: client_credentials
      scope: service
    resource:
      user-info-uri: http://localhost:3000/me
  1. write Feign interceptor
@Configuration
public class FeignConfig {
    @Bean
    @ConfigurationProperties(prefix = "security.oauth2.client")
    public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
        return new ClientCredentialsResourceDetails();
    }

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

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