Handle Security exceptions in Spring Boot Resource Server

后端 未结 8 663
执念已碎
执念已碎 2021-01-31 05:36

How can I get my custom ResponseEntityExceptionHandler or OAuth2ExceptionRenderer to handle Exceptions raised by Spring security on a pure resource ser

8条回答
  •  野的像风
    2021-01-31 05:46

    Spring 3.0 Onwards,You can use @ControllerAdvice (At Class Level) and extends org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler class from CustomGlobalExceptionHandler

    @ExceptionHandler({com.test.CustomException1.class,com.test.CustomException2.class})
    public final ResponseEntity customExceptionHandler(RuntimeException ex){
         return new ResponseEntity(new CustomErrorMessage(false,ex.getMessage(),404),HttpStatus.BAD_REQUEST);
    }
    

提交回复
热议问题