Can you change the 401 error response?

My question is, can you change the structure of the response of the 401 error message?

{
    "error": "unauthorized",
    "error_description": "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken"
}

for example

{
    "timestamp" : 123124354,
    "status" : 401,
    "message" : "The username or password are not valid!"
}

Upvotes: 0

Views: 2790

Answers (1)

Shawn Clark
Shawn Clark

Reputation: 3440

This was answered a while ago: Modify default JSON error response from Spring Boot Rest Controller. Checking the reference guide I would recommend going with the @ControllerAdvice to give you the most flexibility on defining how the JSON response is returned.

Upvotes: 2

Related Questions