I used both 2.0.0.M6 and 2.0.0.BUILD-SNAPSHOT (2018-02-27) but could not get websocket requests to pass through the gateway to another service.
My route is configured in application.yml
- id: websocket_test
order: 8000
uri: lb:ws://notification-service
predicates:
- Path=/ws/notifications
When a websocket request is made, the gateway responds with:
Failed to handle request [GET http://localhost:9000/ws/notifications]:
Response status 400 with reason "Invalid 'Upgrade' header: {Host=[localhost:9000],
Pragma=[no-cache], Cache-Control=[no-cache], Origin=[http://localhost:8080],
Sec-WebSocket-Version=[13], User-Agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36],
Accept-Encoding=[gzip, deflate, br], Accept-Language=[sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6],
Sec-WebSocket-Key=[fTWJEvw9FKAEaf6dfO8HYQ==], Sec-WebSocket-Extensions=[permessage-deflate; client_max_window_bits]}"
I think the problem is that RemoveHopByHopHeadersFilter runs before the WebsocketRoutingFilter and removes the Upgrade header. Both filters are Ordered.LOWEST_PRECEDENCE.
If I add a configuration for RemoveHopByHopHeadersFilter to not remove the default headers my websocket connection works fine.
cloud:
gateway:
filter:
remove-non-proxy-headers:
headers:
- dummy
The logging from RoutePredicateHandlerMapping shows the (wrong?) filter order.
Mapping [Exchange: GET http://localhost:9000/ws/notifications] to Route{id='websocket_test', uri=lb:ws://notification-service, order=8000, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$310/1346292516@652a31ae, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RemoveNonProxyHeadersGatewayFilterFactory$$Lambda$312/1048434276@33d0ad7e, order=1}]}
api-gateway_1 | 2018-02-27 16:39:40.628 DEBUG 1 --- [ctor-http-nio-4] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@335b5620}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RemoveNonProxyHeadersGatewayFilterFactory$$Lambda$312/1048434276@33d0ad7e, order=1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@29a0cdb}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@676cf48}, order=10100}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@5a1de7fb}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@32a68f4f}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@73194df}, order=2147483647}]
I will add more info to this issue soon. I'll try to create an isolated test case.
Note: RemoveHopByHopHeadersFilter is not a GatewayFilter, but a HeadersFilter. It's actually run inside of WebsocketRoutingFilter, so there is no conflicting order. In fact, RemoveNonProxyHeadersGatewayFilterFactory and RemoveHopByHopHeadersFilter don't both need to exist. I suspect I forgot to remove RemoveNonProxyHeadersGatewayFilterFactory after I added RemoveHopByHopHeadersFilter
@goeh can you try snapshots (after a few minutes) and see if 7a2d011 helps?
It works! That was a very fast and awesome fix! Thank you very much @spencergibb . 馃憤
Closed via 7a2d011ced7ba665eabf48d2f4cf26bca1ad8cb7
i meet this question,but the answer is not help for my code
I use 2.0.1
this is the log.can u help me?
2018-04-30 18:03:23.062 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition bulletscreen applying {_genkey_0=/bullet/info/} to Path
2018-04-30 18:03:23.063 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: bulletscreen
2018-04-30 18:03:23.064 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition bulletscreen applying {_genkey_0=/chat} to Path
2018-04-30 18:03:23.064 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition bulletscreen applying {_genkey_0=/toAll/} to Path
2018-04-30 18:03:23.065 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition bulletscreen applying {_genkey_0=/bullet/} to Path
2018-04-30 18:03:23.065 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: bulletscreen
2018-04-30 18:03:23.066 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.h.RoutePredicateHandlerMapping : Route matched: bulletscreen
2018-04-30 18:03:23.066 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://127.0.0.1:8888/bullet/info?t=1525082603052] to Route{id='bulletscreen', uri=lb:ws://bullet, order=5678, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$756/1954766130@4e7da74, gatewayFilters=[]}
2018-04-30 18:03:23.066 DEBUG 4196 --- [ctor-http-nio-3] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@a44e6cd}, order=-1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@6aea89f5}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@2cd771ac}, order=10100}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@7da7ddfc}, order=2147483637}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@66dc58c5}, order=2147483646}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@d0599af}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@4b03bde6}, order=2147483647}]
2018-04-30 18:03:23.072 WARN 4196 --- [ctor-http-nio-3] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://127.0.0.1:8888/bullet/info?t=1525082603052]: Response status 400 with reason "Invalid 'Upgrade' header: {Host=[127.0.0.1:8888], Connection=[keep-alive], Origin=[http://127.0.0.1:5678], User-Agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36], Accept=[/*], Referer=[http://127.0.0.1:5678/], Accept-Encoding=[gzip, deflate, br], Accept-Language=[zh,en-US;q=0.9,en;q=0.8,en-GB;q=0.7,zh-CN;q=0.6], Cookie=[sidebar_collapsed=false; _gitlab_session=6b70d7ce2f1d7356bdc8f771f22cae7f]}"
this is my config
spring:
cloud:
gateway:
routes:
- id: bulletscreen
uri: lb:ws://bullet
predicates:
- Path=/bullet/info/*
- id: bulletscreen
uri: lb:ws://bullet
predicates:
- Path=/bullet/*
this is error of chrome .
Failed to load http://127.0.0.1:8888/bullet/info?t=1525082804110: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:5678' is therefore not allowed access. The response had HTTP status code 400.
OMG! i find the reason of the problem.
i write the wrong uri with path of '/bullet/info/**'
the right is lb://bullet.
but i write lb:ws://bullet

I am facing the same issue with routing to websocket getting 400 with headers, can some one help me here what can be done ?
I am geting the same isssue with Spring Boot 2.3.1 and cloud version is Hoxton.SR5.
Mapped to org.springframework.cloud.gateway.handler.FilteringWebHandler@63053b71
2020-06-18 08:59:56.840 DEBUG 8464 --- [or-http-epoll-2] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@6dfebd2a}, order = -2147483648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@6d41200c}, order = -2147482648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@5667dd90}, order = -1], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@58e7f930}, order = 0], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.GatewayMetricsFilter@1ba98508}, order = 0], [[StripPrefix parts = 1], order = 1], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@9dbb1d9}, order = 10000], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77226121}, order = 10100], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@28ceb25e}, order = 2147483646], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@62e586a6}, order = 2147483647], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@1bb0fa38}, order = 2147483647]]
2020-06-18 08:59:56.852 DEBUG 8464 --- [or-http-epoll-2] o.s.w.r.s.s.s.HandshakeWebSocketService : [4c83abf7-11] Invalid 'Upgrade' header: [Host:"localhost:8080", Connection:"keep-alive", Accept:"*/*", User-Agent:"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36", Sec-Fetch-Site:"same-origin", Sec-Fetch-Mode:"cors", Sec-Fetch-Dest:"empty", Referer:"http://localhost:8080/websocket-server", Accept-Encoding:"gzip, deflate, br", Accept-Language:"en-US,en;q=0.9", Cookie:"cookie_consent_cookie_20190715563=1; cookie_consent_cookie_20200403546=1; io=Foh6N7vWLciaPvqlAAAF"]
2020-06-18 08:59:56.855 DEBUG 8464 --- [or-http-epoll-2] a.w.r.e.AbstractErrorWebExceptionHandler : [4c83abf7-11] Resolved [ServerWebInputException: 400 BAD_REQUEST "Invalid 'Upgrade' header: [Host:"localhost:8080", Connection:"keep-alive", Accept:"*/*", User-Agent:"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36", Sec-Fetch-Site:"same-origin", Sec-Fetch-Mode:"cors", Sec-Fetch-Dest:"empty", Referer:"http://localhost:8080/websocket-server", Accept-Encoding:"gzip, deflate, br", Accept-Language:"en-US,en;q=0.9", Cookie:"cookie_consent_cookie_20190715563=1; cookie_consent_cookie_20200403546=1; io=Foh6N7vWLciaPvqlAAAF"]"] for HTTP GET /chat-ws/socket.io/
2020-06-18 08:59:56.856 DEBUG 8464 --- [or-http-epoll-2] o.s.http.codec.json.Jackson2JsonEncoder : [4c83abf7-11] Encoding [{timestamp=Thu Jun 18 08:59:56 IST 2020, path=/chat-ws/socket.io/, status=400, error=Bad Request, me (truncated)...]
2020-06-18 08:59:56.871 DEBUG 8464 --- [or-http-epoll-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [4c83abf7-11] Completed 400 BAD_REQUEST
But,I added this configuration,And the same mistake
Most helpful comment
I am facing the same issue with routing to websocket getting 400 with headers, can some one help me here what can be done ?