Chr*_*erl 5 spring-security spring-boot graphql graphql-java
使用 graphql-spring-boot-starter 库https://github.com/graphql-java-kickstart/graphql-spring-boot,是否可以保护所有请求,但仅允许带有身份验证的 graphql 自省查询?
该项目已启用 Spring security 使用 OAuth2,因此每个请求都是安全的。
感谢您的任何提示或帮助。
小智 1
您可以在 application.yml 中为您想要在未经授权的情况下执行的 graphql 操作定义一个属性。举个例子,在 application.yml -> 授权中: exceptedoperation: IntrospectionQuery
在 WebSecurityConfig.java 中,您可以绑定您的 exceptedOperation 变量,如下所示: @Value("${authorization.excludedoperation}") private String exceptedOperations;
并将 exceptedOperation 定义为 GraphQLServletExecutor、GraphQLServletExecutorImpl 实现中的字段。在 WebSecurityConfig.java 中,从返回 GraphQLServletExecutor(SpqrMvcAutoConfiguration.graphQLExecutor 的替换)newGraphQLExecutor 的 @Bean 方法,返回 GraphQLServletExecutorImpl,并以 exceptedOperation 作为构造函数参数之一。
在 GraphQLServletExecutorImpl 中,确保在执行函数中,仅当 GraphQL 请求中的 OperationName 不包含排除的操作(在您的情况下为 IntrospectionQuery)GraphQLServletExecutorImpl 时才进行授权,
public Map<String, Object> execute(GraphQL graphQL, GraphQLRequest graphQLRequest, NativeWebRequest nativeRequest) {
ExecutionInput input = buildInput(graphQLRequest, nativeRequest, contextFactory, dataLoaderRegistryFactory);
if (!excludedOperations.contains(graphQLRequest.getOperationName())) {
// check the access_token
HttpServletRequest request = nativeRequest.getNativeRequest(HttpServletRequest.class);
bearerTokenAuthorizer.authorize(EMPTY_AUTH_ANNOTATION, request);
//do your thing
}
return graphQL.execute(input).toSpecification();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1210 次 |
| 最近记录: |