预授权错误处理
我使用Spring Oauth2
和Spring Pre-post Annotations
用Spring-boot
我有一个服务班MyService
。一个MyService
方法是:
@PreAuthorize("#id.equals(authentication.principal.id)")public SomeResponse getExampleResponse(String id){...}
我可以某种方式控制调用方控制器返回的json吗?
默认返回的json是:
{error : "access_denied" , error_message: ".."}
我希望能够控制error_message
参数。我正在寻找类似的东西:
@PreAuthorize(value ="#id.equals(authentication.principal.id)", onError ="throw new SomeException("bad params")")public SomeResponse getExampleResponse(String id){...}
我想到的一种方法是使用 ExceptionHandler
@ExceptionHandler(AccessDeniedException.class)public Response handleAccessDeniedException(Exception ex, HttpServletRequest request){
...
}
但我无法控制message
例外。而且我不确定这Exception
会在将来的版本中抛出
回答:
有关错误处理的Spring Boot文档:http :
//docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-
features-error-handling。控制JSON的一种方法是添加@Bean
type ErrorAttributes
。
@BeanErrorAttributes errorAttributes() {
return new MyErrorAttributes();
}
以上是 预授权错误处理 的全部内容, 来源链接: utcz.com/qa/400072.html