在Azure Web应用程序上使用Java Spring Boot Stomp应用程序时Websocket握手出现403错误

时间:2019-04-02 21:37:27

标签: azure web websocket

我部署了一个带websocket应用程序的java spring引导程序,该程序在本地可用于azure Web应用程序。当我进入html页面时出现错误

websocket.js:6到'wss://ctiadapter.azurewebsites.net/ws/806/0qbqlnon/websocket'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:403

我的猜测是由于反向代理Azure网络应用程序的使用,但不确定如何解决或绕过它。

感谢您的反馈。

1 个答案:

答案 0 :(得分:0)

实际上,首先,当您创建Azure WebApp并在Application settings中进行配置时,您需要启用以下Web sockets功能。

enter image description here

为了重现您的问题,我从https://github.com/spring-guides/gs-messaging-stomp-websocket/releases下载了最新版本的spring-guides/gs-messaging-stomp-websocket。然后,通过gradlew build构建可执行的jar文件,并通过Kudo控制台将其上传到我现有WebApp的路径site/wwwroot中,并配置web.config文件使其运行。

enter image description here

这里是web.config的内容。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
        </handlers>
        <!-- <httpPlatform processPath="%JAVA_HOME%\bin\java.exe" -->
        <httpPlatform processPath="D:\Program Files\Java\jre1.8.0_181\bin\java.exe"
                      arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\gs-messaging-stomp-websocket-0.1.0.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>

接下来,当我访问https://<my webapp name>.azurewebsites.net并单击Connect按钮与服务器建立Websocket连接时,我也遇到了同样的问题。

enter image description here

我注意到websocket请求与SSL一起使用wss://。因此,我尝试访问http://<my webapp name>.azurewebsites.net来执行相同的操作,但它的工作异常。

enter image description here

我搜索了它并研究了示例源代码,我认为此示例中缺少Spring Security WebSocket支持。您可以参考旧的Spring博客Preview Spring Security WebSocket Support,并按照有关WebSocket Configuration的Spring Security文档进行修复。有两个个人GitHub存储库BijanVan/Spring-Boot-Websocket-Samplerstoyanchev/spring-websocket-portfolio,它们都使用org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer来支持带SSL的WebSocket。

相关问题