Skip to content

Instantly share code, notes, and snippets.

@levancho
Created May 17, 2023 11:32
Show Gist options
  • Save levancho/dc545efd0656b271a1ba3da01156ef07 to your computer and use it in GitHub Desktop.
Save levancho/dc545efd0656b271a1ba3da01156ef07 to your computer and use it in GitHub Desktop.
public class JWTAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException {
response.setStatus(403);
response.setContentType(MediaType. APPLICATION_JSON_VALUE);
String message;
if (exception.getCause() != null) {
message = exception.getCause().getMessage();
} else {
message = exception.getMessage();
}
byte[] body = new ObjectMapper().writeValueAsBytes(Collections.singletonMap("error", message));
response.getOutputStream().write(body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment