java - 将标头(设置 cookie)添加到 ProxyExchange 响应
问题描述
我尝试在 ProxyExchange 响应(spring-cloud-gateway-mvc)上添加 hader 以设置 cookie。但我收到此错误:
java.lang.UnsupportedOperationException: null
at org.springframework.http.ReadOnlyHttpHeaders.add(ReadOnlyHttpHeaders.java:86) ~[spring-web-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at com.test.server.proxy.ProxyController.proxy(FrontProxyController.java:52) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.1.12.RELEASE.jar:5.1.12.RELEASE]
这是我的代码
public ResponseEntity<byte[]> proxy(
@RequestHeader(HttpHeaders.ACCEPT) final String contentType,
@CookieValue(name = "session", required = false) final Long principalCookie,
ProxyExchange<byte[]> proxy)
{
if (isNeedCookie(contentType)) {
Long sessionId = 0L; //new sessionID
if (!sessionId.equals(principalCookie)) {
response.getHeaders().add(HttpHeaders.SET_COOKIE, sessionId);
}
}
return response;
}
private boolean isNeedCookie(String contentType)
{
return contentType != null && contentType.contains("html");
}
我还尝试在 ProxyExchange 中设置 cookie,但它没有在最终响应中发送:
proxy.header(HttpHeaders.SET_COOKIE, sessionId);
解决方案
推荐阅读
- sql-server - SQL 触发器 - DML 语句输出插入错误
- php - WordPress 自定义搜索表单(WooCommerce 产品类别)
- .htaccess - Nuxt.js 动态 301 重定向与 API 匹配参数
- r - R Shiny:在进行 withProgress 时,在超时时弹出一条消息
- angular - 如果您不关心旧浏览器,将 tsconfig 目标设置为 es6 是否安全?
- java - Appengine LocalTaskQueueTestConfig 中的不稳定行为
- python - python中的元组索引
- matlab - 删除uitable中的选定行 - Matlab
- sql-server - Google 使用经典 ASP 将 API 短语翻译成各种语言
- corda - 在 Corda 中,一方如何与另一方共享其保险库中的现有状态?