我需要一种方法从我的 grapqh-java impl 向我的其余端点发送用于身份验证和授权的 http header 。我正在 GraphQL 服务层进行身份验证和授权,并且成功。现在我需要将相同的 header 传递到所有其余端点。我有办法做到这一点吗? Grapqhl - Spring Boot 休息端点 - Dropwizard
最佳答案
也许只是在执行查询时将所需的用户特定数据附加到 GraphQL 上下文,例如:
graphQL.execute(queryString, context);
其中context
可以是任何对象,包含 token 、cookie、 session 数据、HttpServletRequest
等
然后,使用它从您的 DataFetcher(又名解析器)发送正确的 header :
public Object get(DataFetchingEnvironment environment) {
Map<String, Object> context = environment.getContext();
//get tokens, session data or whatever you need from the context to create the headers
List headers = ...;
return callRestWithHeaders("/rest/example", headers);
}
最好,通过组合、继承或 AOP 风格在一个地方执行此 header 准备逻辑。
关于java - GraphQL-Java 将 HTTP header 发送到 REST 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46159199/