简体   繁体   English

如何从 spring 启动应用程序中的 graphQL 端点中的传入消息中读取请求标头

[英]How to read request headers from incoming message in a graphQL endpoint in spring boot application

I have a spring boot application running with a graphql endpoint that validates and executes queries and mutations, however, I need to read one header in the incoming message in order to pass its value to another endpoint.我有一个 spring 启动应用程序运行一个 graphql 端点来验证和执行查询和突变,但是,我需要读取一个 header 以将其值传递给传入消息中的另一个端点。 Is there a way in graphql to read these values? graphql 中有没有办法读取这些值? some sort of getHeaders or something like that?某种 getHeaders 或类似的东西?

GraphQL itself does not define any things related to how to expose it over the network , so it does not define any things related to get HTTP header.It is up to developers to use their ways.So, it depends on the underlaying technologies you use to serve GraphQL over HTTP. GraphQL 本身并没有定义任何与如何通过网络公开它相关的东西,因此它没有定义任何与获取 HTTP 标头相关的东西。开发人员使用他们的方式。所以,这取决于你使用的底层技术通过 HTTP 提供 GraphQL。

Consider you use graphql-spring-boot and graphql-java-tools , and assuming that you does not customize GraphQLContext , you can try to add DataFetchingEnvironment argument to your resolver function and then get the GraphQLContext from it.考虑您使用graphql-spring-bootgraphql-java-tools ,并假设您没有自定义GraphQLContext ,您可以尝试将DataFetchingEnvironment参数添加到您的解析器函数,然后从中获取GraphQLContext You can then get HttpServletRequest from the context and access the headers :然后,您可以从上下文中获取HttpServletRequest并访问标头:

    public Foo resolveFoo(Map<String,String> input , DataFetchingEnvironment env){

        GraphQLContext context =  env.getContext();
        HttpServletRequest request = context.getHttpServletRequest().get();
        request.getHeader("content-type");
    }

The solution by @Ken Chan was not working for me. @Ken Chan 的解决方案对我不起作用。 GraphQLContext had no method named getHttpServletRequest . GraphQLContext没有名为getHttpServletRequest方法。
Solved it by using GraphQLServletContext instead.改用GraphQLServletContext解决了这个问题。 You can change the code to:您可以将代码更改为:

public Foo resolveFoo(Map<String,String> input , DataFetchingEnvironment env){

    GraphQLServletContext context =  env.getContext();
    String header = context.getHttpServletRequest().getHeader("content-type");
}

Apparently the type of the context is not standardized.显然,上下文的类型没有标准化。 I use SPQR, and in my case I discovered (via debug):我使用 SPQR,就我而言,我发现(通过调试):

        DefaultGlobalContext<ServletWebRequest> context = handlerParameters.getDataFetchingEnvironment().getContext();
        context.getNativeRequest().getHeader("something");

Google brought me this answer and unfortunately it was for Spring framework.谷歌给我带来了这个答案,不幸的是它是针对 Spring 框架的。 Many use Spring framework but if you use Apache Tomcat with GraphQL integration like I do, you can do use this.许多人使用 Spring 框架,但如果你像我一样使用 Apache Tomcat 和 GraphQL 集成,你可以使用它。

        graphql.kickstart.servlet.context.DefaultGraphQLServletContext.DefaultGraphQLServletContext context =  dataFetchingEnvironment.getContext();
        jakarta.servlet.http.HttpServletRequest request = context.getHttpServletRequest();
        String tokenBearer = request.getHeader("Authorization");

hth

Not a direct answer to your problem statement, but one can use a Filter to handle it before the request hits the resolver endpoints (if that's a requirement):不是对您的问题陈述的直接回答,但可以在请求到达解析器端点之前使用过滤器来处理它(如果这是必需的):

public class HeaderFilter implements Filter {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    
    String headerVal= httpServletRequest.getHeader("<header string>");
    
    try {
        filterChain.doFilter(httpServletRequest, servletResponse);
    } catch (IOException | ServletException e) {
        //handle as you wish
    }
}

These answers are too old, for latest versions.对于最新版本,这些答案太旧了。 you can try to autowire one HttpServletRequest in your controller , such as您可以尝试在controller中自动连接一个 HttpServletRequest ,例如

@Slf4j
@Controller
public class YourQueryController {
@Autowired
private HttpServletRequest request;
....

Then implement following logic to get header:然后执行以下逻辑得到header:

request.getHeader("Authorization")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 MessageContext 获取 SOAP 标头 - 在 Spring Boot WS 应用程序中 - How to get SOAP headers from MessageContext - In Spring Boot WS Application 对 Spring Boot Endpoint 的 Ajax 请求无法读取 HTTP MSG - Ajax Request to Spring Boot Endpoint Failed to Read HTTP MSG Spring-boot 未创建 graphql 端点 - Spring-boot not creating graphql endpoint 如何使用 Spring 引导传入请求将多部分文件映射到 DTO - How To Mapping Multipart file to DTO using Spring Boot for incoming request 为Spring Boot的传入消息添加缺少的ContentType - Add missing ContentType for incoming message for Spring Boot Spring Boot中的访问请求标头? - Access Request Headers in Spring Boot? 如何从 java Z91F1F487C7FB768642B3EB98F683BF5 应用程序调用 GraphQL api? 是否有任何注释支持 graphQL 查询格式? - How to invoke GraphQL api from a java spring-boot application? Is there any annotation which supports graphQL query formation? 在 Spring 引导中获取传入请求的地理位置 - Get geoLocation of incoming request in Spring boot 如何在 GraphQL Spring Boot 应用程序中跳过验证规则 - How to skip validation rules in GraphQL Spring Boot application 如何在Spring Boot应用程序中使用HornetMQ消息 - How to consume HornetMQ message in spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM