bezkoder / spring-boot-spring-security-jwt-authentication

Spring Boot + Security: Token Based Authentication example with JWT, Authorization, Spring Data & MySQL
1.29k stars 815 forks source link

getting org.springframework.security.authentication.InsufficientAuthenticationException error If I pass unknown api url or method #26

Open ambeathirumalesu opened 10 months ago

ambeathirumalesu commented 10 months ago

Hi after implementing the spring security with jwt token it is working as expected with known API urls but when I hit with unknown URL, get 401 authorized error and if any exception is raised at runtime also gets 401 authorized because fo the below exception in Cansole. getting org. spring framework.security.authentication.InsufficientAuthenticationException

do I need to add any filter in AuthEntryPointJwt under the commence? I just wanted to ignore the authentication filter if there is any unknown API hit that came to the application and should throw the NoHandlerFoundException custom exception error with below handler. how can I do that?

@ExceptionHandler(NoHandlerFoundException.class) public ResponseEntity handle(NoHandlerFoundException ex){ return new ResponseEntity<>(APIError.createInstance(HttpStatus.NOT_FOUND.toString(), List.of(new ErrorDetails(ex.getMessage(),null))), null, HttpStatus.NOT_FOUND); }