You don't really need your own implementation of the FeignRequestInterceptor as there is already BasicAuthRequestInterceptor in the feign.auth package that does exactly the same.
That being said, you basically have almost everything set up already. All is left to do is to define the basicAuthRequestInterceptor bean with specific username and password:
@Bean
public RequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("username", "password");
}
Answer from Aleksandr Erokhin on Stack OverflowYou don't really need your own implementation of the FeignRequestInterceptor as there is already BasicAuthRequestInterceptor in the feign.auth package that does exactly the same.
That being said, you basically have almost everything set up already. All is left to do is to define the basicAuthRequestInterceptor bean with specific username and password:
@Bean
public RequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("username", "password");
}
I know the thread is a bit old but wanted to give some explanation on what's happening here.
If you'd like to customize your Feign requests, you can use a RequestInterceptor. This can be a custom implementation or you can reuse what's available in the Feign library, e.g. BasicAuthRequestInterceptor.
How to register it? Well, there are 2 ways to do it depending on how you use Feign.
If you're using plain Feign without Spring, then you gotta set the interceptor to the Feign builder. An example is here.
Feign.builder()
.requestInterceptor(new MyCustomInterceptor())
.target(MyClient.class, "http://localhost:8081");
If you're using Spring Cloud OpenFeign and you use the @FeignClient annotation to construct your clients, then you have to create a bean from your RequestInterceptor by either defining it as a @Component or as a @Bean in one of your @Configuration classes. Example here.
@Component
public class MyCustomInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
// do something
}
}
Also, you can check out one of my articles in this topic, maybe that clears it up better: Customizing each request with Spring Cloud Feign
If your JWT token provider is OAuth 2.0 compliant, you can configure the OAuth2FeignRequestInterceptor, with an OAuth2ProtectedResourceDetails object. This object is the base class for all OAuth 2.0 grant type information. In your case, I recommend using ResourceOwnerPasswordResourceDetails instead. This will allow you to configure an interceptor using a username and password.
@Configuration
public class OAuth2RequestInterceptorConfiguration {
@Bean
public OAuth2FeignRequestInterceptor requestInterceptor() {
OAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
OAuth2ProtectedResourceDetails resourceDetails =
new ResourceOwnerPasswordResourceDetails();
resourceDetails.setUsername("username");
resourceDetails.setPassword("password");
return new OAuth2FeignRequestInterceptor(clientContext, resourceDetails);
}
}
For other cases, you will need to create your own RequestInterceptor
public class MyRequestInterceptor implements RequestInterceptor {
private String jwt;
private LocalDateTime expirationDate;
@Override
public void apply(RequestTemplate requestTemplate) {
/* validate and refresh your token, this sample is not thread safe */
if (LocalDateTime.now().isAfter(expirationDate)) {
requestToken();
}
/* use the token */
requestTemplate.header("Authorization: Bearer " + this.jwt);
}
}
You can try with something like:
public class FeignConfiguration {
@Value("${security.jwt.token}")
private String jwtToken;
@Bean
public RequestInterceptor requestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("Authorization", jwtToken);
}
}
}