【发布时间】:2015-04-09 05:55:27
【问题描述】:
我正在使用 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 是否会在未来的版本中被抛出
【问题讨论】:
-
{error : "access_denied" , error_message: ".."}看起来像 Spring Boot 默认错误处理程序的输出。你用的是这个吗? -
是的 @DaveSyer 我正在使用 Spring 启动
标签: spring spring-security spring-boot spring-annotations spring-security-oauth2