控制器中的验证错误 returns 401 而不是 400

Validation error in controller returns 401 instead of 400

我的休息控制器中有一个 post 端点,无需 authentication/authorization 即可访问。它在 WebSecurityConfigurerAdapter 中配置如下:

@Override
public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/api/v1/user");
}

端点对输入数据进行了验证(由注释 javax.validation.Valid 提供)。当我发送无效数据时,我收到 401 响应而不是 400。这个问题在安全端点中不存在,默认 spring 引导 400 消息被发送。

调试时发现在处理MethodArgumentNotValidException(controller校验错误时抛出)时,WebExpressionVoter被执行,returns值为-1,即'access denied' .如何配置我的应用程序以忽略端点的安全检查,这些端点是 public?

我知道使用 ControllerAdvice 进行异常处理是一种选择,但是是否可以使用默认的 400 条消息?

提前感谢您的任何提示!

编辑:

Security filter chain: [
  WebAsyncManagerIntegrationFilter
  SecurityContextPersistenceFilter
  HeaderWriterFilter
  LogoutFilter
  OAuth2AuthenticationProcessingFilter
  RequestCacheAwareFilter
  SecurityContextHolderAwareRequestFilter
  AnonymousAuthenticationFilter
  SessionManagementFilter
  ExceptionTranslationFilter
  FilterSecurityInterceptor
]

使用 webSecurity.ignoring() 将导致对 URL 的请求完全绕过整个安全过滤器链。所以有趣的是 WebExpressionVoter 仍然会为这些被忽略的 URLs 踢球。

所以,我猜测有以下几种可能性:

  • /api/v1/user 将被重定向到其他受保护的 URL。

  • 您认为 antMatchers("/api/v1/user") 将匹配 /api/v1/user 下的所有 URL,例如 /api/v1/user/1。但是,它只匹配 /api/v1/user

要忽略 /api/v1/user 下的所有 URL,您应该使用 :

 web.ignoring().antMatchers("/api/v1/user/**");

好的,我找到了解决方案。我不知道为什么,但是当发生异常时,会有另一个 POST 请求到这个 url: /error。当我将此 url 添加到我的忽略列表时,它开始工作。所以我的配置是这样的:

@Override
public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/api/v1/user", "/error/**")
}

@Ken Chan 感谢您提供有关调试过滤器的信息!很关键。