我使用 spring 来实现安全性和 graphql,
首先我想引用 spring graphql 文档中的内容“Web GraphQL 端点的路径可以通过 HTTP URL 安全性进行保护,以确保只有经过身份验证的用户才能访问它。”
因此,我创建了一个正常的 spring 安全配置,并在虚拟控制器上对其进行了测试,一切顺利。
当我在@QueryMapping 上尝试时 我收到 403,但没有错误消息。
我尝试过调试,但由于上下文具有“用户匿名”而不是我在安全过滤器中注入的内容,因此授权检查失败。 我已经通过将 .permitAll() 放入 /graphql 并使用 aop 来验证我的请求来修复它,但这是一个错误还是预期的行为?
看看我的安全性(它是一样的,但没有 graphql 许可)
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
// Enable CORS and disable CSRF
// Setup authorization
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.requestMatchers("/swagger-ui/**").permitAll()
.requestMatchers("/v3/api-docs/**").permitAll()
.requestMatchers("/error").permitAll()
.requestMatchers("/graphql/**").permitAll()
.requestMatchers("/actuator/**").permitAll()
.requestMatchers("/**").hasRole("LOGIN")
.anyRequest()
.authenticated())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
).addFilterBefore(
this.jwtTokenFilter,
UsernamePasswordAuthenticationFilter.class
)
.cors(cors -> cors.disable())
.csrf(csrf -> csrf.disable())
.build();
更新:我发现到底是什么问题,解析器(@QueryMapping)被一个 completableFuture (内部)包装,运行后,completable future 正在清除上下文,然后另一个内部过滤器检查权限,但发现上下文已被匿名上下文替换并返回 403。