Spring cloud Feign OAuth2 request interceptor is not working

一曲冷凌霜 提交于 2021-01-27 14:52:10

问题


I am trying to create a simple REST client using spring cloud feign to consume a service which is secured with OAuth2 security tokens. I am using OAuth2FeignRequestInterceptor for adding the bearer token, check my below code. I am facing 401. and when try to debug my code I don't find the bearer token in my Request object.

@Configuration
@EnableConfigurationProperties(value=OAuth2ClientCredentialsProperties.class)
@EnableOAuth2Client
@Profile(OAuth2Profiles.CLIENT_CREDENTIALS)
public class ClientCredentialsConfiguration {

    @Autowired
    private OAuth2ClientCredentialsProperties oAuth2ClientCredentialsProperties;

    @Bean
    @Qualifier("ClientCredentialsOAuth2FeignRequestInterceptor")
    public OAuth2FeignRequestInterceptor oauth2schemeRequestInterceptor() {
        return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), oauth2schemeResourceDetails());
    }

    @Bean
    public ClientCredentialsResourceDetails oauth2schemeResourceDetails() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        details.setClientId(oAuth2ClientCredentialsProperties.getClientId());
        details.setClientSecret(oAuth2ClientCredentialsProperties.getClientSecret());
        details.setAccessTokenUri(oAuth2ClientCredentialsProperties.getAccessTokenUri());
        details.setScope(oAuth2ClientCredentialsProperties.getScopes());
        return details;
    }

}

Here is my client interface

@FeignClient(name = "test", url = "http://localhost:8080", configuration = ClientCredentialsConfiguration.class)
interface GitHubClient {


    @RequestMapping(value = "/api/v1/products",
            produces = "application/json",
            consumes = "application/json;charset=UTF-8",
            method = RequestMethod.POST)
    ResponseEntity<Object> testUsingPOST(@RequestBody TestDTO testDTO);

and my properties are below

server.port=10080

security.user.name=admin
security.user.password=admin
security.basic.enabled=false

org.springframework.boot.autoconfigure.EnableAutoConfiguration=sgcib.clips.bcsapi.configuration.ClientCredentialsConfiguration

feign.oauth2.enabled=true

feign.hystrix.enabled=false

My Main class

@SpringBootApplication
@EnableWebMvc
@Controller
@EnableFeignClients
@EnableAutoConfiguration
public class App extends SpringBootServletInitializer {


    @Autowired
    private GitHubClient gitHub;

    @RequestMapping("/")
    public String home() {
        return "index";
    }

    @RequestMapping("/{owner}")
    @ResponseBody
    public ResponseEntity<Object> contributors(@PathVariable String owner) {
        return gitHub.productsUsingPOST(new TestDTO());
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

回答1:


I am also using feign with request interceptors. For me it worked to change the @Bean method return type into a generic RequestInterceptor. Like this:

@Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
    return new OAuth2FeignRequestInterceptor(...);
}

Also this tutorial describes pretty well how to setup OAuth with feign: spring-cloud-feign-oauth2




回答2:


I found the issue, it is due to the annotation @Profile(OAuth2Profiles.CLIENT_CREDENTIALS) in the ClientCredentialsConfiguration class. I missed to enable the profile, so my interceptor Bean is not loaded at all.



来源:https://stackoverflow.com/questions/45859676/spring-cloud-feign-oauth2-request-interceptor-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!