Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cloud Feign OAuth2 request interceptor is not working

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);
    }
like image 876
Pravin K Avatar asked Oct 29 '22 03:10

Pravin K


1 Answers

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

like image 125
Stephan De Avatar answered Nov 15 '22 10:11

Stephan De