Skip to content
Advertisement

Custom Exceptions for JWT authentication don’t work JAVa

I implemented in my API Rest JWT authentication, but the exceptions that I created don’t work.

This is what I expect:

JavaScript

This is what I get:

JavaScript

This is my CustomException Class

JavaScript

This is what I have in my globalExceptionHandler

JavaScript

And finally this is what I have in the method validateToken in JwtTokenProvider:

public boolean validateToken(String token){

JavaScript

I really appreciate if someone can give a hand.

Advertisement

Answer

This is because Spring filters have a different error response mechanism, separate to general exceptions. In this sample of mine I am using a custom filter to do the OAuth work.

The more standard Spring option is to customize the AccessDeniedHandler as discussed in this tutorial.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement