第八章、WebSocket的安全

Websocket 端点使用Web容器安全模型来保护。这样做的目的是使得Websocket开发人员可以轻松地声明是否需要验证对websocket服务端点的访问权限,谁可以访问它以及是否需要加密连接。映射到给定ws://URI(如第3章和第4章所述)的websocket在部署描述符中受到保护,并带有带有相同主机名,端口和路径的http://URI的列表,因为这是URL它的开放握手。因此,Websocket开放人员可以为其websocket端点分配一种身份验证方案,授予访问和传输保证的用户角色。

8.1、WebSocket的身份验证

  • 该规范未定义可用来验证WebSocket本身的机制。相反,通过建立在Servlet定义的安全机制上,需要身份验证的WebSocket必须依赖于试图启动先前要进行身份验证的连接的打开握手请求,通常,这将在向Websocket打开握手之前通过包含Websocket的web应用程序中的Http身份验证(可能是基本的或基于表单的)执行
  • 如果客户端针对受安全性机制保护的Websocket发送未经身份验证的打开握手请求,则websocket实现必须返回对打开握手请求的401(未授权)响应,并且可能不会启动WebSocket连接[WSC-8.1-1]。

8.2、WebSocket的授权

  • 可以通过将元素添加到要打包的Web应用程序的web.xml中来设置websocket授权。容器必须使用安全性约束中使用来匹配websocket[WSC-8.2-1]的打开握手的请求URI,该实现必须将GET(或默认值,缺少)以外的任何HTTP方法解释为不适用于websocket。[WSC-8.2-2]

8.3、传输保证

  • 容器必须将传输保证NONE解释为允许未加密的ws:// 连接到Websocket[WSC-8.3-1]。实现必须将机密的传输保证解释为仅允许通过加密的(wss://) 连接访问websocket[WSC-8.3-2], 这可能需要预先认证的请求。

8.4、例子

  • web.xml部署描述符中示例片段显示了WebSocket 端点的安全约束,在该示例中,与传入Web应用程序的上下文 root相关的传入URI“quotes/live”匹配的websocket端点受到保护,以使得只能使用wss://进行访问,并且仅可用于属于GOLD MEMBER或PLATINUM MEMBER角色的经过身份验证的用户。

  • <security-constraint>
    	<web-resource-collection>
      	<web-resource-name>LiveQuoteWebSocket</web-resource-name>
        <description>Security constraint for live quote websocket endpoint</description>
        <url-pattern>/quotes/live</url-pattern>
        <http-method>GET</http-method>
      </web-resource-collection>
      <auth-constraint>
      	<description>
        	definition of which roles may access the quote endpoint
        </description>
        <role-name>
        	GOLD_MEBMER
        </role-name>
        <role-name>PLATINUM_MEMBER</role-name>
      </auth-constraint>
      <user-data-constraint>
      	<description>WSS required</description>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
      </user-data-constraint>
    </security-constraint>
    

附录

  1. [1] I. Fette and A. Melnikov. RFC 6455: The WebSocket Protocol. RFC, IETF, December 2011. See http://www.ietf.org/rfc/rfc6455.txt.
  2. [2] Ian Hickson. The WebSocket API. Note, W3C, December 2012. See http://dev.w3.org/html5/websockets/.
  3. [3] S. Bradner. RFC 2119: Keywords for use in RFCs to Indicate Requirement Levels. RFC, IETF, March 1997. See http://www.ietf.org/rfc/rfc2119.txt.
  4. [4] Danny Coward. Java API for WebSocket. JSR, JCP, 2013. See http://jcp.org/en/jsr/detail?id=356.
  5. [5] Expert group mailing list archive. Web site. See http://java.net/projects/websocket-spec/lists/jsr356-experts/archive.
  6. [6] J. Gregorio, R. Fielding, M. Hadley, M. Nottingham, and D. Orchard. RFC 6570: URI Template. RFC, IETF, March 2012. See http://www.ietf.org/rfc/rfc6570.txt.
  7. [7] Pete Muir. Contexts and Dependency Injection for Java EE. JSR, JCP, 2013. See http://jcp.org/en/jsr/detail?id=347.
  8. [8] Linda DeMichiel and Bill Shannon. Java Platform, Enterprise Edition 7 (Java EE 7) Specification. JSR, JCP, 2013. See http://jcp.org/en/jsr/detail?id=342.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值