-
Notifications
You must be signed in to change notification settings - Fork 1.3k
How-to: Handle errors and customize the OAuth 2.0 Error response #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
type: documentation
A documentation update
Comments
Sample demonstrating how to customize the error response parameters using private AuthenticationFailureHandler authenticationFailureHandler = createAuthenticationFailureHandler();
private static AuthenticationFailureHandler createAuthenticationFailureHandler() {
OAuth2ErrorHttpMessageConverter errorResponseConverter = new OAuth2ErrorHttpMessageConverter();
errorResponseConverter.setErrorParametersConverter(error -> {
Map<String, String> parameters = new HashMap<>();
// Only return the error code
parameters.put(OAuth2ParameterNames.ERROR, error.getErrorCode());
return parameters;
});
OAuth2ErrorAuthenticationFailureHandler authenticationFailureHandler = new OAuth2ErrorAuthenticationFailureHandler();
authenticationFailureHandler.setErrorResponseConverter(errorResponseConverter);
return authenticationFailureHandler;
} |
Sample demonstrating how to customize the HTTP status for the error response using private AuthenticationFailureHandler authenticationFailureHandler = createAuthenticationFailureHandler();
private static AuthenticationFailureHandler createAuthenticationFailureHandler() {
OAuth2ErrorHttpMessageConverter errorResponseConverter = new OAuth2ErrorHttpMessageConverter() {
@Override
protected void writeInternal(OAuth2Error error, HttpOutputMessage outputMessage)
throws HttpMessageNotWritableException {
HttpServletResponse servletResponse = ((ServletServerHttpResponse) outputMessage).getServletResponse();
if (OAuth2ErrorCodes.INVALID_CLIENT.equals(error.getErrorCode())) {
servletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
} else {
servletResponse.setStatus(HttpStatus.BAD_REQUEST.value());
}
super.writeInternal(error, outputMessage);
}
};
OAuth2ErrorAuthenticationFailureHandler authenticationFailureHandler = new OAuth2ErrorAuthenticationFailureHandler();
authenticationFailureHandler.setErrorResponseConverter(errorResponseConverter);
return authenticationFailureHandler;
} |
Closed
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Publish a guide on How-to: Handle errors and customize the OAuth 2.0 Error response
Related gh-499
The text was updated successfully, but these errors were encountered: