Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何处理Springboot应用程序中的401未授权错误_Spring Boot_Http Status Code 401 - Fatal编程技术网

Spring boot 如何处理Springboot应用程序中的401未授权错误

Spring boot 如何处理Springboot应用程序中的401未授权错误,spring-boot,http-status-code-401,Spring Boot,Http Status Code 401,我需要在Springboot应用程序中记录401个未经授权的错误。 我想在我的应用程序中记录某个端点,因为其未经授权的错误我知道在开始调用api时会捕获此尝试 有没有什么方法可以让我在spring boot中跟踪这一点? 我试图通过WebSecurity配置,但没有帮助 我正在使用springboot 2 提前谢谢。我可以想到两种方法,但可能还有更多 在控制器中使用自定义@ExceptionHandler方法,并使其对UnauthorizedException.class敏感 使用过滤器对请求/

我需要在Springboot应用程序中记录401个未经授权的错误。 我想在我的应用程序中记录某个端点,因为其未经授权的错误我知道在开始调用api时会捕获此尝试

有没有什么方法可以让我在spring boot中跟踪这一点? 我试图通过WebSecurity配置,但没有帮助

我正在使用springboot 2


提前谢谢。

我可以想到两种方法,但可能还有更多

在控制器中使用自定义@ExceptionHandler方法,并使其对UnauthorizedException.class敏感 使用过滤器对请求/响应进行内省,并记录您想要记录的任何内容,以防出现响应。
您可以获得身份验证入口点并在http中处理一些401逻辑 安全配置:

  .authenticationEntryPoint((httpServletRequest, httpServletResponse, e) -> {
                log.warn(e.getMessage());
                httpServletResponse.setStatus(401);
                httpServletResponse.getWriter().print(e.getMessage());
            })

请在此处查看我的其他问题的答案->