gpt4 book ai didi

java - 禁用错误消息 : AccessDeniedException: Access is denied

转载 作者:行者123 更新时间:2023-12-04 08:04:28 25 4
gpt4 key购买 nike

我使用这个自定义错误处理程序:

@Component
public class OAuth2AuthenticationEntryPoint implements AuthenticationEntryPoint {

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException ex) throws IOException {

ErrorDetail errorDetail = ErrorDetail.AUTHENTICATION_ERROR;

ErrorResponse errorEntry = new ErrorResponse();
errorEntry.setTitle(errorDetail.getTitle());
errorEntry.setCode(errorDetail.getErrorCode());
HttpStatus httpStatus = ErrorDetail.getHttpStatusBasedOnErrorCode(errorDetail.getErrorCode());
errorEntry.setStatus(httpStatus.value());
errorEntry.setDetail(ex.getMessage());
Map<String, String> extra = new HashMap<String, String>();
extra.put("detail", ex.getMessage());
errorEntry.setExtra(extra);

ErrorResponseDTO errorResponse = new ErrorResponseDTO();
errorResponse.setErrors(Arrays.asList(errorEntry));

response.setStatus(errorDetail.getHttpStatus().value());
String json = new ObjectMapper().setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL).writeValueAsString(errorResponse);
response.getWriter().write(json);
response.flushBuffer();
}
}
我收到此错误响应,因为它应该是:
{"errors":[{"status":404,"code":"1000","title":"Authentication error","detail":"Full authentication is required to access this resource","extra":{"detail":"Full authentication is required to access this resource"}}]}
但是我进入服务器记录这个错误:
21:34:30.498 [http-nio-8090-exec-7] DEBUG AffirmativeBased[decide:66] - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@33252ffb, returned: -1
21:34:30.498 [http-nio-8090-exec-7] DEBUG ExceptionTranslationFilter[handleSpringSecurityException:180] - Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:84)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:123)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
您知道每次收到 AuthenticationException 时如何抑制或禁用此错误吗?

最佳答案

这是一个 DEBUG用于开发目的的级别日志,这没问题。在生产级别,日志级别不应低于 WARN级别,因此不会记录这些异常,这也不是问题。
如果您坚持要修改行为,Spring 允许在类上设置日志级别,或者使用 OFF 完全禁用它。 .我发现关闭它是不鼓励的,因为您可能会错过更严重的日志,因此异常也可以记录在不同的级别上。
开通 application.properties并写出以下之一:

  • 禁用所有日志:org.springframework.security.access.AccessDeniedException=OFF
  • 至少 ERROR级别:org.springframework.security.access.AccessDeniedException=ERROR
  • 至少 WARN级别:org.springframework.security.access.AccessDeniedException=WARN
  • 关于java - 禁用错误消息 : AccessDeniedException: Access is denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66297226/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com