我有一个使用 Spring Security OAuth2 保护的 Spring Boot 3.4 服务。
我的安全配置是标准的:
@Bean
public SecurityFilterChain oauth2FilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(sm -> sm.sessionCreationPolicy(STATELESS))
.exceptionHandling(eh -> eh.authenticationEntryPoint(handleException()));
http.securityMatcher("/api/**")
.authorizeHttpRequests(auth ->
auth.requestMatchers("/api/**").authenticated())
.oauth2ResourceServer(resourceServer ->
resourceServer.jwt(jwt -> jwt.decoder(jwtDecoder())));
return http.build();
}
我的端点带有以下注释:@PreAuthorize(hasAuthority('SCOPE_MY_SCOPE'))
。
此外,在我的 API 模型上,我使用 Jakarta Validation 3 进行验证(例如,在字段上使用注释@NotNull
)。
鉴于此配置,我有以下场景:我使用没有所需范围的安全承载令牌发出无效请求。
服务器响应 400,因为验证约束失败。但是,我预计这个请求会返回 403;我认为 Spring 向未经授权调用我的端点的人公开了有关我的验证约束的不必要信息。如果我使用有效的请求,它确实会返回 403,所以问题在于检查的顺序:我希望先看到安全检查,然后是验证检查,但似乎情况正好相反。