一、该问题的重现步骤是什么?
1. 启动Biz工程里面的blade-websocket
2. 修改index.html,修改js和css的请求路径,增加 /blade-websocket 前缀(通过gateway)
3. 修改main.js 里面的ws相关请求路径,也增加 /blade-websocket 前缀(通过gateway)
二、你期待的结果是什么?实际看到的又是什么?
期待:能跟修改之前正常使用WS聊天
实际:输入完名字后,页面反馈:Could not connect to WebSocket server. Please refresh this page to try again!
后端gateway服务报错:io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Invalid handshake response getStatus: 403 Forbidden
三、你正在使用的是什么产品,什么版本?在什么操作系统上?
bladex3.4.0,Windows 11
四、请提供详细的错误堆栈信息,这很重要。
前端控制台:
/blade-websocket/ws/142/uue1jd0t/xhr_streaming?t=1709799641383
Invalid CORS request
/blade-websocket/ws/142/t04wglhj/xhr_send?t=1709799641424
Invalid CORS request
后端:
================ Gateway Response Start ================
<=== 200 GET: /blade-websocket/ws/142/geq3c5wy/websocket
================ Gateway Response End =================
2024-03-07 16:20:41.380 WARN 18296 --- [ctor-http-nio-3] r.netty.http.client.HttpClientConnect : [e5475859-4, L:/192.168.42.150:2507 - R:/192.168.42.150:7006] The connection observed an error
io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Invalid handshake response getStatus: 403 Forbidden
at io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker13.verify(WebSocketClientHandshaker13.java:312) ~[netty-codec-http-4.1.101.Final.jar:4.1.101.Final]
at io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker.finishHandshake(WebSocketClientHandshaker.java:364) ~[netty-codec-http-4.1.101.Final.jar:4.1.101.Final]
at reactor.netty.http.client.WebsocketClientOperations.onInboundNext(WebsocketClientOperations.java:121) ~[reactor-netty-http-1.0.39.jar:1.0.39]
at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:114) [reactor-netty-core-1.0.39.jar:1.0.39]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [netty-codec-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346) [netty-codec-4.1.101.Final.jar:4.1.101.Final]
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:318) [netty-codec-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) [netty-transport-4.1.101.Final.jar:4.1.101.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) [netty-common-4.1.101.Final.jar:4.1.101.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-common-4.1.101.Final.jar:4.1.101.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-common-4.1.101.Final.jar:4.1.101.Final]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
2024-03-07 16:20:41.382 WARN 18296 --- [ctor-http-nio-3] reactor.netty.channel.FluxReceive : [e5475859-4, L:/192.168.42.150:2507 ! R:/192.168.42.150:7006] An exception has been observed post termination, use DEBUG level to see the full stack: io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Connection prematurely closed BEFORE opening handshake is complete.
2024-03-07 16:20:41.389 INFO 18296 --- [ctor-http-nio-6] o.s.g.filter.GlobalRequestLogFilter :
五、若有更多详细信息,请在下面提供。
目前还没有做Gateway转发到websocket的操作,你在gateway加个这个配置试试,然后换成转发的地址
spring:
cloud:
gateway:
routes:
- id: websocket_route
uri: ws://localhost:8080 # 指向的WebSocket服务
predicates:
- Path=/ws/**
似乎不行,也可能是我操作的不对,还请大佬继续指点
我修改了gateway服务里面的application-dev.yml文件,增加了:
spring:
cloud:
gateway:
routes:
- id: websocket_route
# uri: ws://localhost:7006 # 指向的WebSocket服务
uri: lb://blade-websocket # 指向的WebSocket服务
predicates:
- Path=/ws/**
上面的两种 uri 都试了,问题依旧
贴出完整解决方案:
修改 blade-websocket 服务的 WebSocketConfig类
// 增加 setAllowedOriginPatterns("*") 的调用 registry.addEndpoint("/ws").setAllowedOriginPatterns("*").withSockJS();
修改 blade-websocket 服务的 index.html
// 增加请求前缀 /blade-websocket <link rel="stylesheet" href="/blade-websocket/css/main.css" /> <script src="/blade-websocket/js/sockjs.min.js"></script> <script src="/blade-websocket/js/stomp.min.js"></script> <script src="/blade-websocket/js/main2.js"></script>
修改 blade-websocket 服务的 main.js
// 建立 WS 连接时增加前缀 /blade-websocket var socket = new SockJS('/blade-websocket/ws');
访问:http://localhost/blade-websocket
扫一扫访问 Blade技术社区 移动端