We have a leisure API that uses Spring OAuth2 . After user authentication, all JSON responses are in the following format:
{"code" : 12345, "data" : "..." }
But the JSON response for authentication failures is not a string with the above format, since this is handled by Spring.
For example, in the case of incorrect credentials, clients receive an HTTP 400 status code with a JSON response as follows:
{"error": "invalid_grant", "error_description": "Bad credentials" }
If the user account is locked, clients receive an HTTP 400 status code with a JSON response as follows
{"error":"invalid_grant","error_description":"User account is locked"}
This is because Spring TokenEndpoint.handleException () handles exceptions related to / oauth / token
I would like to modify the JSON response for OAuth2 crashes to follow the first format.
This is what I have tried so far without success:
- Use the ControllerAdvice with the highest acknowledgment order and use @ExceptionHandler as described here
- implementation of OAuth2ExceptionRenderer as described here
- implement ExceptionMapper
- Added a new ObjectMapper with the StdSerializer extension. Although my object matrix is ββinitialized, it is not used to serialize exceptions. Perhaps because Spring calls MappingJackson2HttpMessageConverter directly, and there seem to be several instances of this class in my application.
Any help in any of the above approaches or new would be greatly appreciated.
I have not tried using this , as I cannot change the context path for existing clients.
source share