更新:我找到了这个example,看来真正的问题是如何从
获取cookie /令牌DefaultGraphQLWebSocketContext context = env.getContext();
我将graphql-java-demo用作使用spring boot(和graphql-java-tools)的基础项目,并且尝试使用request.cookie
设置身份验证。
我可以将@Autowired HttpServletRequest request
注入
@Component
public class Query implements GraphQLQueryResolver {
@Autowired
private CloudService cloudService;
private @Autowired
HttpServletRequest request;
public Optional<EnvMetadata> get_env_metadata() {
System.out.println(request.getHeader("Cookie"));
return cloudService.get_env_metadata();
}
(它会输出cookie)
但是当我尝试对Subscription做同样的事情时:
@Component
class Subscription implements GraphQLSubscriptionResolver {
存在以下错误:
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
如何注入DispatcherServlet
或从订阅请求中提取request.cookie
的东西?
我想我应该覆盖 WebSocket连接属性,以某种方式保存该request.cookie
:link。
答案 0 :(得分:0)
注入似乎很适合您,但是您必须记住,从技术上讲,获取请求对象本身是不可能的,但是代理可以在运行时解析当前请求。 您的异常表明,尝试获取请求时没有请求上下文。您的方法可能在请求之外被调用。例如。预定任务或类似任务?