【问题标题】:Uncaught SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS未捕获的安全错误:无法构造“WebSocket”:可能无法从通过 HTTPS 加载的页面启动不安全的 WebSocket 连接
【发布时间】:2015-04-21 22:01:38
【问题描述】:

我使用的是 GlassFish Server 4.1/Java EE 7。在我的 web.xml 文件中,我提到了以下安全约束。

<security-constraint>
    <display-name>UserConstraint</display-name>
    <web-resource-collection>
        <web-resource-name>Provide a Name</web-resource-name>
        <description/>
        <url-pattern>/admin_side/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <description/>
        <role-name>ROLE_ADMIN</role-name>
    </auth-constraint>
    <user-data-constraint>
        <description/>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

其他当局也是如此。

由于transport-guarantee 设置为CONFIDENTIAL,匹配指定URL 模式/admin_side/* 的网页将通过安全通道(HTTPS) 运行。

如下使用 WebSockets (JavaScript),

var ws = new WebSocket("ws://localhost:8181/Context/Push");

它无法建立初始握手。浏览器在浏览器控制台上显示以下警告。

[blocked] The page at 'https://localhost:8181/Context/admin_side/Category' was loaded over HTTPS, but ran insecure content from 'ws://localhost:8080/Context/Push': this content should also be loaded over HTTPS.

Uncaught SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.

当 web.xml 中的 &lt;user-data-constraint&gt; 部分被完全删除时,网页将在 HTTP 上运行。在那之后,改变,

var ws = new WebSocket("ws://localhost:8181/Context/Push");

var ws = new WebSocket("ws://localhost:8080/Context/Push");

工作正常。

如何使 WebSockets 在安全通道上工作?

【问题讨论】:

  • 是的!有效。我以前从未见过wss://。请添加它作为答案。谢谢。
  • 谢谢!我还添加了一些其他信息以供将来参考。

标签: html jakarta-ee ssl websocket java-ee-7


【解决方案1】:

尝试使用wss://,这表明您需要安全的 websocket 连接。浏览器会阻止您从安全页面创建不安全的连接,因为它知道,由于您使用https:// 请求页面,因此存在传输安全要求。

如果你的 WebSocket 服务器与你的 Web 服务器一起运行,它可能会使用相同的安全证书,你不需要做任何事情。如果它不起作用,请查看您的 WebSocket 服务器文档以了解如何添加证书和启用wss://

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 2021-04-01
    • 2020-04-09
    • 2020-04-18
    • 2019-07-17
    • 2013-03-31
    • 1970-01-01
    • 2019-11-25
    • 2014-07-09
    相关资源
    最近更新 更多