Spring Boot Security:未经身份验证和未经授权的用户的自定义错误消息

时间:2021-07-14 11:15:47

标签: java spring-boot rest java-8 spring-boot-starter-security

我想在我的 springBoot 应用程序中向未经身份验证和未经授权的用户(401 和 403 错误)发送我的自定义错误模板,如下所示

{
   timestamp : 14-07-2021,
   reason : UnAuthorised,
   code : 401 
}

我尝试了许多解决方案,如下所示:

1-AuthenticationEntryPoint:- 但我可以发送自定义错误字符串( response.getOutputStream().println("{ "error": "" + authenticationException.getMessage() + "" } ");)

2-AuthenticationFailureHandler:- 在身份验证失败时,我的应用程序不会调用它。

我不确定以上 2 个解决方案是否涵盖所有与身份验证和授权相关的情况。

是否有任何方法可以针对所有授权和身份验证情况发送自定义 (MyApiError.class) 错误?

0 个答案:

没有答案