SpringBoot OAuth2自定义异常响应

问题描述 投票:0回答:1

有人可以帮助我自定义SpringBoot 2 OAuth 2.0的Exception响应吗?基本上,我需要将异常响应跟踪到一些特定于应用程序的定制响应中。

{
    "error": "unauthorized",
    "error_description": "Full authentication is required to access this resource"
}

{
    "error": "invalid_token",
    "error_description": "Access token expired: sdfhjkudxnkjjkJJHGhgnbHnmbhJGJH"
}

{
    "timestamp": "2020-04-29T11:30:47.045+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/testf"
}

类似

{
    "status": "error",
    "message": "Full authentication is required to access this resource"
}

谢谢...

java spring-boot spring-security oauth-2.0 spring-security-oauth2
1个回答
0
投票

您可以在配置HttpSecurity时针对403进行此操作:

.exceptionHandling()     
.accessDeniedHandler(myCoolAccessDeniedHandler())

类似于documentation中指定的

其他两个可能由您可能定义的某些ExceptionHandler捕获并处理:

public class MyExceptionHandlingController extends ResponseEntityExceptionHandler implements ErrorController
© www.soinside.com 2019 - 2024. All rights reserved.