【问题标题】:403 error on websocket handshake when using Java Spring boot Stomp app on Azure web apps在 Azure Web 应用程序上使用 Java Spring boot Stomp 应用程序时 websocket 握手出现 403 错误
【发布时间】:2019-08-24 07:40:20
【问题描述】:

我部署了一个带有 websocket 应用程序的 java spring boot,该应用程序在 azure web 应用程序本地工作。当我进入 html 页面时,我收到错误

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

我的猜测是因为使用了反向代理 Azure Web 应用程序,但不确定如何修复或绕过它。

感谢任何反馈。

【问题讨论】:

    标签: azure web websocket


    【解决方案1】:

    其实首先,当你创建 Azure WebApp 并在Application settings 中配置时,你需要启用Web sockets 功能,如下所示。

    为了重现您的问题,我从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文件使其运行。

    这里是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://&lt;my webapp name&gt;.azurewebsites.net 并单击 Connect 按钮与服务器建立 websocket 连接时,我遇到了同样的问题。

    我注意到 websocket 请求使用 wss:// 和 SSL。于是我尝试访问http://&lt;my webapp name&gt;.azurewebsites.net做同样的操作,结果出乎意料。

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

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2015-12-28
      • 2021-12-18
      • 2020-08-16
      相关资源
      最近更新 更多