有人可以帮助我自定义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"
}


真的很期待
谢谢...

最佳答案

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

.exceptionHandling()
.accessDeniedHandler(myCoolAccessDeniedHandler())


documentation中指定的一样

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

public class MyExceptionHandlingController extends ResponseEntityExceptionHandler implements ErrorController

10-07 13:20
查看更多