首页 > 解决方案 > 将标头(设置 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);

标签: javaspring-mvcspring-cloud-gateway

解决方案


推荐阅读