I’m developing a web application using Spring WebFlux and Spring Security with OAuth2 with KeyClock as third-party authorization. When trying to access any endpoint with a valid JWT Token, as it is a valid token, I don’t get forbidden, instead, I get a CodecException related to a type definition error saying that the ServerHttpResponse already committed a response. Here is the error log:
Error Log
2024-06-20T10:52:35.370-03:00 ERROR 27432 --- [demo] [ parallel-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [53e68de1] Error [org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.web.reactive.function.server.DefaultEntityResponseBuilder$DefaultEntityResponse]] for HTTP GET "/api/demo", but ServerHttpResponse already committed (200 OK)
Security Bean
I have set up my security configuration as follows:
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain securityFilterChain(final ServerHttpSecurity serverHttpSecurity) {
return serverHttpSecurity
.authorizeExchange(exchanges -> exchanges.anyExchange().authenticated())
.oauth2ResourceServer(securityOAuth2Specs -> {
securityOAuth2Specs.jwt(Customizer.withDefaults());
})
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.build();
}
}
Controller
It is very simple, just an endpoint that will only be called with a valid JWT.
@RestController
@RequestMapping("/api")
public class DemoController {
@GetMapping("/demo")
public Mono<ServerResponse> doSomething() {
return ServerResponse.ok().bodyValue("Hello World");
}
}
Maven Dependencies:
I’m using Spring boot 3.3.0
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Question:
How can I resolve this CodecException? It seems like the error occurs after the response has already been committed. What could be causing this, and how can I fix it to ensure the endpoint functions as expected?
Attempts:
Trying to use ResponseEntity but that is out of webflux purpose.