在任何基于 rest-api 的应用程序中,需要拦截对应用程序的请求并执行多个操作只是时间问题。如果这些操作是需要应用于应用程序的所有请求的操作,那么过滤器的使用是有意义的,例如安全性。
在基于 Servlet 的应用程序中,我们曾经有
ContentCachingRequestWrapper和ContentCachingResponseWrapper。我们在 WebFlux 环境中寻找与上述相同的品质。
等价的解决方案是webflux包提供的装饰器类:
ServerHttpRequestDecorator、ServerHttpResponeDecorator、ServerWebExchangeDecorator。
让我们从一个简单的基于 Flux 的 api 开始。
首先我们导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>