获取 java.lang.IllegalStateException:使用 Kafka Consumer 命中 API 时未找到线程绑定请求
我的要求是从 API 请求中获取 cookie,然后将相同的 cookie 传递给后续请求,以便使用相同的 JsessionID 维护会话。
为此,我尝试通过自动装配 HttpServletRequests 使用 HttpHeaders 来获取 cookie。 如果我使用控制器访问 API,我不会收到任何异常,并且能够获取所有请求属性,但如果我运行 Kafka 并且 API 通过 Kafka Consumer 运行,则会出现以下异常:
java.lang.IllegalStateException :未找到线程绑定请求:您是指实际 Web 请求之外的请求属性,还是在原始接收线程之外处理请求?如果您实际上在 Web 请求中进行操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。
@Autowired
HttpServletRequest req;
@Autowired
HttpServletResponse res;
public ResponseBean putData() {
String jsessionId = null;
String id = null;
// Creating Headers for request
String authValue = req.getHeader("authorization");
String headerCookie = req.getHeader("Cookie");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("authorization", authValue);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set(HttpHeaders.COOKIE, headerCookie);
HttpEntity httpEntity = new HttpEntity(httpHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange
("https://url", HttpMethod.POST, httpEntity, String.class);
jsessionId = responseEntity.getHeaders().getFirst("Set-Cookie");
if (jsessionId !=null && jsessionId.contains("JSESSION")) {
logger.info("New login session created");
id = jsessionId.substring(jsessionId.indexOf('=') + 1, jsessionId.indexOf(';'));
Cookie cookie = new Cookie("JSESSIONID", id);
res.addCookie(cookie);
}
}
PS-我没有使用 SpringSecurity 。
谁能帮我解决上述问题。
My requirement is to fetch cookie from the API requests and then pass on the same cookie to subsequent requests in order to maintain session using the same JsessionID.
For that I am trying to fetch cookie using HttpHeaders by Autowiring HttpServletRequests.
If I hit an API using Controller I don't get any exception and I am able to fetch all the request attributes but if I run Kafka and API runs through Kafka Consumer, I am getting below exception:
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.
@Autowired
HttpServletRequest req;
@Autowired
HttpServletResponse res;
public ResponseBean putData() {
String jsessionId = null;
String id = null;
// Creating Headers for request
String authValue = req.getHeader("authorization");
String headerCookie = req.getHeader("Cookie");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("authorization", authValue);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set(HttpHeaders.COOKIE, headerCookie);
HttpEntity httpEntity = new HttpEntity(httpHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange
("https://url", HttpMethod.POST, httpEntity, String.class);
jsessionId = responseEntity.getHeaders().getFirst("Set-Cookie");
if (jsessionId !=null && jsessionId.contains("JSESSION")) {
logger.info("New login session created");
id = jsessionId.substring(jsessionId.indexOf('=') + 1, jsessionId.indexOf(';'));
Cookie cookie = new Cookie("JSESSIONID", id);
res.addCookie(cookie);
}
}
PS- I am not using SpringSecurity.
Can anyone please help me in fixing the above issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此异常试图告诉您,已自动连接到您的类中的 HttpServletRequest 没有正在处理当前请求。
有东西正在调用
putData()
方法,但不是通过调用 HTTP 控制器。您提到您正在使用“Kafka Consumer”,所以也许是消费者代码正在调用 putData() ?您无法调用它,因为该方法需要具有 Web 请求上下文才能工作,因为它需要请求标头和 cookie 信息。您可能应该分离出数据持久化代码,以便控制器 putData() 方法和 kafka 消费者都可以调用它。
This exception is trying to tell you that there is no current request being processed by the
HttpServletRequest
that has been auto-wired into your class.Something is calling the
putData()
method but not as through a call to the HTTP controller. You mention that you are using the "Kafka Consumer" so maybe it is the consumer code which is callingputData()
? You can't call it because the method needs to have a web request context to work since it needs the request headers and cookie information.You should probably separate out the data persistence code so that both the controller
putData()
method and the kafka consumer can both call it.