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 Overflow
Top answer
1 of 3
25

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");
}
2 of 3
12

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

🌐
Medium
medium.com › @sachinsindhu › feign-client-request-interceptor-pit-falls-f7f08ff61b74
Feign client Request Interceptor Pit falls | by Sachin Sindhu | Medium
September 11, 2023 - Instead of creating different interceptor for every service , we can create a single interceptor and have our custom factory to provide interceting logic for different services. CustomRequestInterceptor: RequestTemplate contains feignTarget object which is basically the feign client that made that request with some other data as well.
🌐
Medium
medium.com › @babalolaopedaniel › how-to-use-feign-client-with-interceptors-for-authentication-in-a-spring-boot-application-71d46ba89c06
How to Use Feign Client with Interceptors for Authentication in a Spring Boot Application | by Babalola Opeyemi | Medium
December 29, 2024 - Feign was developed by Netflix and now part of the Spring Cloud ecosystem. It makes writing HTTP clients easier by allowing you to define them through simple interfaces using annotations. This article will guide you through creating a Spring Boot project that uses Feign clients with interceptors for handling authentication.
🌐
DEV Community
dev.to › tarcalia › feign-clients-with-multiple-interceptors-in-spring-boot-hh2
Feign Clients with Multiple Interceptors in Spring Boot - DEV Community
June 18, 2025 - When working with Spring Boot and OpenFeign, it's quite common to add custom headers to outbound HTTP requests — like tracking IDs, authentication tokens, or metadata provided by libraries. But what happens when you want to add multiple interceptors in a clean, reusable, and testable way? This blog post shows you how to chain multiple Feign RequestInterceptor instances using a composite pattern.
🌐
Baeldung
baeldung.com › home › rest › setting request headers using feign
Setting Request Headers Using Feign | Baeldung
March 25, 2026 - Multiple Request interceptors can also be applied to the Feign client.
🌐
OneUptime
oneuptime.com › home › blog › how to implement custom feign interceptors
How to Implement Custom Feign Interceptors
January 30, 2026 - Interceptors execute before Feign sends the HTTP request. They receive a RequestTemplate object that you can modify by adding headers, query parameters, or transforming the request body. sequenceDiagram participant Client as Feign Client participant ...
🌐
Javacodemonk
javacodemonk.com › feign-requestinterceptor-in-spring-boot-cbe5d967
Feign RequestInterceptor in Spring Boot
July 14, 2020 - All we need to do is to create a Bean of type RequestInterceptor inside a config class and provide that configuration to FeignClient.
🌐
Medium
medium.com › @IlyasKeser › feignclient-interceptor-for-bearer-token-oauth-f45997673a1
FeignClient Interceptor for Bearer Token/OAuth | by Ilyas Keser | Medium
October 20, 2019 - But providing the access token for each method call is not what we want. We can implement an interceptor and provide the token for all Feign clients under the hood and remove the parameter in method signature.
Find elsewhere
🌐
Arnoldgalovics
arnoldgalovics.com › feign-interceptor
Customizing each request with Spring Cloud Feign interceptor – Arnold Galovics
February 19, 2018 - Feign has a so called RequestInterceptor which will be applied for all the requests sent by Feign clients. The only thing that’s necessary is to define a custom RequestInterceptor class and register it as a Spring bean.
🌐
Spring Cloud
cloud.spring.io › spring-cloud-openfeign › multi › multi_spring-cloud-feign.html
1. Declarative REST Client: Feign
In some cases it might be necessary to customize your Feign Clients in a way that is not possible using the methods above. In this case you can create Clients using the Feign Builder API. Below is an example which creates two Feign Clients with the same interface but configures each one with a separate request interceptor.
🌐
GitHub
github.com › spring-cloud › spring-cloud-netflix › issues › 288
Ability to configure feign.RequestInterceptor specific to a given feign client · Issue #288 · spring-cloud/spring-cloud-netflix
March 31, 2015 - There is a few use cases where we need to add different headers or authentication mechanisms on a per client basis. Currently the FeignClientFactoryBean appears to apply its configuration to all feign clients that it constructs. This was problematic in that some services that we want to use feign for require a different set of RequestInterceptors to one set of clients verse another.
Author: spring-cloud
🌐
Javadoc.io
javadoc.io › doc › io.github.openfeign › feign-core › 10.12 › feign › RequestInterceptor.html
RequestInterceptor - feign-core 10.12 javadoc
Latest version of io.github.openfeign:feign-core · https://javadoc.io/doc/io.github.openfeign/feign-core · Current version 10.12 · https://javadoc.io/doc/io.github.openfeign/feign-core/10.12 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/io.github.openfeign/feign-core/10.12/package-list ·
🌐
Medium
medium.com › @aravindcsebe › mastering-feign-requestinterceptor-from-authentication-to-advanced-use-cases-d78fad055af5
Mastering Feign RequestInterceptor: From Authentication to Advanced Use Cases | by Spring Boot Simplified | Medium
February 9, 2026 - Not Handling Null Values: Always check for null before accessing authentication contexts. Feign’s RequestInterceptor is a powerful tool that goes far beyond simple authentication.
🌐
Baeldung
baeldung.com › home › spring › spring cloud › introduction to spring cloud openfeign
Introduction to Spring Cloud OpenFeign | Baeldung
February 19, 2026 - Finally, we can create the configuration with default as the client name to configure all @FeignClient objects, or we can declare the feign client name for a configuration: ... If we have both Configuration bean and configuration properties, configuration properties will override Configuration bean values. Adding interceptors is another useful feature provided by Feign.
🌐
Medium
medium.com › @saiteja-erwa › feign-client-requestinterceptors-2435f4e5cfb1
Feign Client: RequestInterceptors - Saiteja Erwa - Medium
May 18, 2024 - @FeignClient(value="service-b", url="placeholder-url", configuration=FeignConfig.class) public interface commonFiegnClient { //requests to service-b } Interceptor will be called for every request, and add data using the provided RequestTemplate.
Top answer
1 of 1
5

Thanks to this comment, I managed to tidy up my implementation a little bit. So no more need for specifying encode/decoder nonsense, or having to manually build my Feign client.

The docs here provide some info, but as is typical they are a bit thin on concrete examples, so perhaps the below will help someone else. Note: I'm using spring boot, and including feign like so in build.gradle implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

First, create the RequestInterceptor like so:

import org.springframework.context.annotation.Bean;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;

import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;

/**
 * A Feign configuration to add the incoming Bearer token to an outgoing feign client request.
 * Only annotate this class with "@Configuration" if you want this interceptor to apply globally to all your Feign clients.
 * Otherwise you risk exposing the auth token to a third party, or adding it unnecessarily to requests that don't need it.
 */
@Slf4j
public class BearerAuthFeignConfig {

    @Bean
    public RequestInterceptor bearerAuthRequestInterceptor() {
        return requestTemplate -> {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.getPrincipal() instanceof Jwt) {
                Jwt jwt = (Jwt) authentication.getPrincipal();
                requestTemplate.header("Authorization", "Bearer " + jwt.getTokenValue());
            } else {
                log.error("Unable to add Authoriation header to Feign requestTemplate");
            }
        };
    }
}

Then when declaring your feign client, pass the configuration

@FeignClient(
    name = "my-client-that-needs-the-auth",
    configuration = BearerAuthFeignConfig.class,
    url = "http://whatever.com"
)
public interface PlayerManagementClient {
  ...

You'll also need the @EnableFeignClients annotation on your @SpringBootApplication class

Top answer
1 of 3
7

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);
    }
}
2 of 3
4

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);
        }
    }
}
🌐
Stack Overflow
stackoverflow.com › questions › 71547633 › spring-boot-feign-client-interceptor-not-working
Spring Boot Feign client - interceptor not working - Stack Overflow
March 20, 2022 - If the server responds with 401 (expired token) I want to reauthenticate and try the request again but the interceptor is not getting triggered 2nd time. ... @Override public void apply(RequestTemplate requestTemplate) { if (AuthenticationService.bearerToken == null) authenticationService.authenticate(); requestTemplate.header(AUTHORIZATION, BEARER_TOKEN_PREFIX + AuthenticationService.bearerToken ); } ... @Override public Exception decode(String s, Response response) { FeignException exception = feign.FeignException.errorStatus(s, response); switch (response.status()) { case 401: authenticationService.authenticate(); return new RetryableException(response.status(), exception.getMessage(), response.request().httpMethod(), exception, null, response.request()); case 500: throw new BadActionException(s, response.reason()); default: break; } return exception; }
🌐
HackerNoon
hackernoon.com › setting-up-multiple-configurations-for-feign-clients-a-step-by-step-guide-f51y3yb3
Setting Up Multiple Configurations for Feign Clients [A Step-by-Step Guide] | HackerNoon
June 21, 2020 - Consider a Feign client that must be used with different configurations at different places in the code, or multiple Feign clients that each must be used with its own configuration. For example Feign client A must be configured with decoder A and request interceptor A and Feign client B with decoder B and interceptor B.
🌐
GitHub
github.com › spring-cloud › spring-cloud-openfeign › issues › 200
Enabling specific Request Interceptors · Issue #200 · spring-cloud/spring-cloud-openfeign
July 25, 2019 - We have multiple feign clients in one of our services which should use different request interceptors. For example we have clients that are requesting data from other services based on an API endpoint and use the users oauth token along the way.