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
Chrome Web Store
chromewebstore.google.com › detail › request-interceptor › bfgblailifedppfilabonohepkofbkpm
Request Interceptor - Chrome Web Store
Intercept and modify network requests: headers, query params, delay, response body, custom script/CSS injection.
Javadoc.io
javadoc.io › doc › io.github.openfeign › feign-core › 10.12 › feign › RequestInterceptor.html
RequestInterceptor - feign-core 10.12 javadoc
Bookmarks · 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....
01:43:32
Network Interceptor | Frontend Javascript Machine Coding | Fetch ...
17:48
This API Client Is Amazing! (HTTP Interceptor, API Mocking, Redirect, ...
04:19
Requestly Overview - HTTP Interceptor & Mocking - YouTube
🎯 Angular 18 Functional Interceptors: The Best Way to Handle ...
How to Implement Interceptor at Client Side | Client Side ...
Spring boot: Custom Interceptors | How to Intercept Incoming ...
Box
opensource.box.com › box-java-sdk › javadoc › com › box › sdk › RequestInterceptor.html
RequestInterceptor (Box Java SDK)
A RequestInterceptor can be very useful for testing purposes.
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › http › client › ClientHttpRequestInterceptor.html
ClientHttpRequestInterceptor (Spring Framework 7.0.7 API)
A typical implementation of this method would follow the following pattern: Examine the request and body. Optionally wrap the request to filter HTTP attributes. Optionally modify the body of the request. either execute the request using ClientHttpRequestExecution.execute(HttpRequest, byte[]) ...
Oatpp
oatpp.io › api › latest › oatpp › web › server › interceptor › RequestInterceptor
RequestInterceptor.hpp | Oat++
namespace oatpp { namespace web { namespace server { namespace interceptor { class RequestInterceptor {} }}}}
Alamofire
alamofire.github.io › Alamofire › Protocols › RequestInterceptor.html
RequestInterceptor Protocol Reference
@preconcurrency public static func interceptor(adapters: [any RequestAdapter] = [], retriers: [any RequestRetrier] = [], interceptors: [any RequestInterceptor] = []) -> Interceptor
SmartBear Community
community.smartbear.com › smartbear community › swagger open source › swagger open source tools questions
requestInterceptor with Python | SmartBear Community
app = FastAPI( swagger_ui_parameters={"requestSnippetsEnabled": True,"persistAuthorization":True, "requestInterceptor": """ (req) => { // Modify the request object as needed req.headers['Authorization'] = 'Bearer <your_access_token>'; return req; } """ } )
Spring
docs.spring.io › spring-cloud-openfeign › docs › current › reference › html
Spring Cloud OpenFeign
This replaces the SpringMvcContract with feign.Contract.Default and adds a RequestInterceptor to the collection of RequestInterceptor.