[英]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-boot和
graphql-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.