Java WebSocket 握手期间出错:意外响应代码:403

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39627017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:17:47  来源:igfitidea点击:

Error during WebSocket handshake: Unexpected response code: 403

javaspringwebsocketcorsspring-websocket

提问by M.Bel

I have implemented WebSocketswith Spring Boot Applicationand have the below error message when trying to test the ws connection with the chrome extension 'Smart websocket client'. However, I have no problem when run the Spring Boot Application locally.

我已经使用Spring Boot 应用程序实现了WebSockets,并且在尝试使用 chrome 扩展“智能 websocket 客户端”测试 ws 连接时出现以下错误消息。但是,在本地运行 Spring Boot 应用程序时我没有问题。

WebSocket connection to 'ws://192.168.X.XYZ:8080/test' failed: 
Error during WebSocket handshake: Unexpected response code: 403

The only difference which I see is in the Request headers:

我看到的唯一区别是在请求标头中:

In the one it works - Origin:http://192.168.X.XYZ:8080

在它工作的一个 - 起源:http: //192.168.X.XYZ: 8080

In the one it does not work - Origin:chrome-extension://omalebghpgejjiaoknljcfmglgbpocdp

在一个它不起作用 - 来源:chrome-extension://omalebghpgejjiaoknljcfmglgbpocdp

What I did in the WebSocketConfigclass is below:

我在WebSocketConfig类中所做的如下:

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(myHandler(), "/test").setAllowedOrigins("http://192.168.X.XYZ:8080");
}

and still does notwork.

而仍然无法正常工作。

Could you please advise what the reason for that error might be and how to fix it?

您能否告知该错误的原因可能是什么以及如何解决?

Thank you in advance.

先感谢您。

采纳答案by Brian Clozel

You need to configure your "chrome-extension://..."origin as an allowed origin or even "*", otherwise it's rejected by the server.

您需要将您的"chrome-extension://..."来源配置为允许的来源或什至"*",否则它会被服务器拒绝。