Implementation of Intercept a Request and Add Headers in WebFlux

We will develop a simple spring reactive project that can intercept requests and modify their headers by implementing the WebFilter. This filter allows us to inspect and transform the requests throughout the application.

Step 1: Create the Spring Boot WebFlux Project

Create a new Spring Boot project using Spring Initializr and add the required dependencies as mentioned below,

  • Spring Web Reactive
  • Lombok
  • Spring DevTools

After the project creation is done, the folder structure will be like below.


Step 2: Configure the Application properties

Open the application.properties file and add the configuration for the server port in the project.

server.port=8080


Step 3: Implement the WebFilter

We will create a new class that implements the WebFilter. In this filter, we will manipulate the ServerHttpRequest to add the custom header of the application.

Go to src > main > java > org.example.springheaderwebflux > config > HeaderAdditionFilter and put the below code.

Java
package org.example.springheaderwebflux.config;

import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Component
public class HeaderAdditionFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerWebExchange mutatedExchange = exchange.mutate()
                .request(exchange.getRequest().mutate().header("Custom-Header", "CustomValue").build())
                .build();
        return chain.filter(mutatedExchange);
    }
}


Step 4: Create the SampleController class

We will create a simple controller to demonstrate that the application is working.

Go to src > main > java > org.example.springheaderwebflux > controller > SampleController and put the below code.

Java
package org.example.springheaderwebflux.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class SampleController {

    @GetMapping("/")
    public Mono<String> hello() {
        return Mono.just("Hello World!");
    }
}


Step 5: Main Class

No changes are required in the main class.

Java
package org.example.springheaderwebflux;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringHeaderWebfluxApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringHeaderWebfluxApplication.class, args);
    }

}


Step 6: Run the application

Now, we will run the application and it will start at port 8080.


Endpoint Testing

Open the Postman tool and add the below request with the GET method and also add the custom header with header value then send the request.

GET http://localhost:8080/

Output:

This example project demonstrates how to Intercept a Request and Add Headers in WebFlux in the Spring Boot project.



How to Intercept a Request and Add Headers in WebFlux?

Spring WebFlux is the reactive stack web framework that allows developers to build non-blocking, asynchronous applications that can handle a large number of concurrent with fewer hardware resources. In this article, we will explore how to intercept the HTTP requests in the Spring WebFlux application and modify them by adding custom headers. This capability can be useful for a variety of cross-cutting concerns such as authentication, logging, and modifying the request or response headers.

Similar Reads

Intercepting Requests in WebFlux

In Spring WebFlux, we can intercept the requests using a WebFliter. The WebFliter can operate on the non-blocking reactive WebFlux stack and allows us to manipulate the request and response to the objects before they can reach the controllers or after they can sent back to the client....

Implementation of Intercept a Request and Add Headers in WebFlux

We will develop a simple spring reactive project that can intercept requests and modify their headers by implementing the WebFilter. This filter allows us to inspect and transform the requests throughout the application....