Spring Cloud OpenFeign Interceptor#
Request Interceptor Concept#
HTTP Interceptors
allow us to check or modify all the incoming or outgoing HTTP requests in our application. We can use them if we want to apply something like an authorization header to all the requests.- There are many use-cases where
HTTP Interceptors
can come in handy. Since they intercept all the API calls, we can use them to apply some logic before or after the API call. Theseinterceptors
have access to the complete request and response body. So, if we want to apply some logic to all the requests, we can add it once using aninterceptor
instead of writing separate code for every request. Here are some examples of things we can do.- Apply an authorization header to all the requests.
- Prefix all the requests with the server name.
- Log all the requests and responses.
- Set a global error catch.
- Resend the request if it fails.
Basic Auth Request Interceptor#
- In
spring-cloud-starter-openfeign
dependency , we have been already provided theBasicAuthRequestInterceptor
for supporting calling to apis which require the basic authentication token. - So, let's start with the example below then we can understand it easily.
Prepare#
- So to begin with the example, we need to prepare some application services as below:
- In this image we will use
Postman
to call to theClient Application
that we will configure below, then from this Client Application, it will call to theResource Application
which is configured with Basic Authentication. - For the source code of
Resource Application
you can download here. - Now, let's create and configure the
Client Application
as below.
Dependencies#
- To use
BasicAuthRequestInterceptor
inOpenFeign
, you need to import the dependency below into thepom.xml
of yourClient Application
which is a Spring Boot application.
pom.xml | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
Create Feign Configuration#
- Let's create a configuration class with name
BasicAuthFeignConfig
for configuringBasicAuthRequestInterceptor
as below:
BasicAuthFeignConfig.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
- As you can see, we will create a bean
BasicAuthRequestInterceptor
with theusername
andpassword
that we load from theapplication.yml
.
application.yml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
- If you don't want to apply the
BasicAuthRequestInterceptor
for all FeignClients, so please don't put the annotation@Configuration
in classBasicAuthFeignConfig.java
.
FeignClient Configuration#
- Now we will create an adapter interface name
SpringBasicAuthClient
to configure FeignClient with target api as below:
SpringBasicAuthClient.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
- As we can see, in the
@FeignClient
annotation, we will provide a client name, the server domain URL that we need to call to and we will load theBasicAuthRequestInterceptor
configuration class that we want to apply for thisFeignClient
by using paramconfiguration = {BasicAuthFeignConfig.class}
.
Controller#
- Now, we need to create a simple controller as below for testing with Postman.
OpenFeignInterceptorController.class | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Testing#
- Finally, let's start our
Client Application
andResource Application
, then you postman to call the exported api above. Then you should see the 200 result as below.
- Then if you check the log in the console, you can see
Authorization
header had been added into the request to theResource Application
for getting the data.
Header Interceptor#
- In some cases, we might need to add some custom request headers into our requests for calling apis. In this case we would like to use
RequestInterceptor
of OpenFeign which will give us more options for configuring Request Interceptor.
Prepare#
- Like the example above, we also follow the diagram and services that we created. But when we test with the postman, we will look into the console log to check our custom headers have been added into the requests to
Resource Server
Create Feign Configuration#
- Let's create a configuration class with name
FeignHeaderConfig
for configuringRequestInterceptor
as below:
FeignHeaderConfig.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
- As you can see, we will create a bean
RequestInterceptor
with some example headers and an authorization token that we load from theapplication.yml
.
application.yml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
FeignClient Configuration#
- Now we will create an adapter interface name
SpringCustomHeaderClient
to configure FeignClient with target api as below:
SpringCustomHeaderClient.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
- Like the
FeignClient
configuration that we did before inSpringBasicAuthClient
, now we just need to change the name and use paramconfiguration = {FeignHeaderConfig.class}
to load theFeignHeaderConfig
which will be applied for thisFeignClient
.
Controller#
- Now, we need to add a new api into the controller that we created before.
OpenFeignInterceptorController.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Testing#
- Finally, let's start our
Client Application
andResource Application
, then you postman to call the exported api above. Then you should see the 200 result as below.
- Then if you check the log in the console, you can see all your custom headers had been added into the request to the
Resource Application
for getting the data.
OpenFeign Interceptor With OAuth2 Client#
- Please view sections below: