response.sendError(statusCode, "error message") not working after upgrading springboot to 2.4.4

Viewed 1813

I am using AuthenticationEntryPoint to handle the Authentication issues.To deliver the exception reason to the end-user.

response.sendError(responseCode, "errorReason")

I recently upgraded the spring boot from "2.2.11.RELEASE" to "2.4.4". Since then it is not working anymore. It is not providing the "errorReason" in the response received by the client now. Is there any change recently introduced in spring-boot which I am missing?

2 Answers

As of Spring Boot 2.3, the message is no longer included in the error response by default:

The error message and any binding errors are no longer included in the default error page by default. This reduces the risk of leaking information to a client. server.error.include-message and server.error.include-binding-errors can be used to control the inclusion of the message and binding errors respectively. Supported values are always, on-param, and never.

Setting server.error.include-message=always will restore Spring Boot 2.2's behavior.

Spring boot 2.4.4 uses Spring Security 5.4.5. AuthenticationException no longer has a getMessage method in this version (link). However it is still the same instance of AuthenticationException that you throw within your application. So you can use something like...


if (authException instanceof CustomAuthenticationException) {
    CustomAuthenticationException ex = (CustomAuthenticationException) authException;
    // handle your exception
}

Related