【发布时间】: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 中的 <user-data-constraint> 部分被完全删除时,网页将在 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