今天數據隱私是一個十分關注的問題,作為開發人員,我們需要準備好解決這個問題。至少我們需要熟悉加密協議 SSL 和 TLS 等之上的其他協議實現數據安全。作為一個 HTTPS 網站的用戶,你是安全。當然,這些協議是廣泛不基于 http 的應用程序,例如安全SMTP(SMTPS)郵件服務,甚至關系數據庫系統。
為了支持 SSL/TLS,Java 提供了 javax.net.ssl API 的類SslContext 和 SslEngine 使它相對簡單的實現解密和加密。Netty 的利用該 API 命名 SslHandler 的 ChannelHandler 實現, 有一個內部 SslEngine 做實際的工作。
圖8.1顯示了一個使用 SslHandler 數據流圖。
[](https://github.com/waylau/essential-netty-in-action/blob/master/images/Figure%208.1%20Data%20flow%20through%20SslHandler%20for%20decryption%20and%20encryption.jpg)
1. 加密的入站數據被 SslHandler 攔截,并被解密
2. 前面加密的數據被 SslHandler 解密
3. 平常數據傳過 SslHandler
4. SslHandler 加密數據并它傳遞出站
Figure 8.1 Data flow through SslHandler for decryption and encryption
如清單8.1所示一個 SslHandler 使用 ChannelInitializer 添加到 ChannelPipeline。(回想一下,當 Channel 注冊時 ChannelInitializer 用于設置 ChannelPipeline 。)
Listing 8.1 Add SSL/TLS support
~~~
public class SslChannelInitializer extends ChannelInitializer<Channel> {
private final SslContext context;
private final boolean startTls;
public SslChannelInitializer(SslContext context,
boolean client, boolean startTls) { //1
this.context = context;
this.startTls = startTls;
}
@Override
protected void initChannel(Channel ch) throws Exception {
SSLEngine engine = context.newEngine(ch.alloc()); //2
engine.setUseClientMode(client); //3
ch.pipeline().addFirst("ssl", new SslHandler(engine, startTls)); //4
}
}
~~~
1. 使用構造函數來傳遞 SSLContext 用于使用(startTls 是否啟用)
2. 從 SslContext 獲得一個新的 SslEngine 。給每個 SslHandler 實例使用一個新的 SslEngine
3. 設置 SslEngine 是 client 或者是 server 模式
4. 添加 SslHandler 到 pipeline 作為第一個處理器
在大多數情況下,SslHandler 將成為 ChannelPipeline 中的第一個 ChannelHandler 。這將確保所有其他 ChannelHandler 應用他們的邏輯到數據后加密后才發生,從而確保他們的變化是安全的。
SslHandler 有很多有用的方法,如表8.1所示。例如,在握手階段兩端相互驗證,商定一個加密方法。您可以配置 SslHandler 修改其行為或提供 在SSL/TLS 握手完成后發送通知,這樣所有數據都將被加密。 SSL/TLS 握手將自動執行。
Table 8.1 SslHandler methods
| 名稱 | 描述 |
| --- | --- |
| setHandshakeTimeout(...) setHandshakeTimeoutMillis(...) getHandshakeTimeoutMillis() | |
Set and get the timeout, after which the handshake ChannelFuture is notified of failure. setCloseNotifyTimeout(...) setCloseNotifyTimeoutMillis(...) getCloseNotifyTimeoutMillis() | Set and get the timeout after which the close notify will time out and the connection will close. This also results in having the close notify ChannelFuture fail. handshakeFuture() | Returns a ChannelFuture that will be notified once the handshake is complete. If the handshake was done before it will return a ChannelFuture that contains the result of the previous handshake. close(...) | Send the close_notify to request close and destroy the underlying SslEngine.
- Introduction
- 開始
- Netty-異步和數據驅動
- Netty 介紹
- 構成部分
- 關于本書
- 第一個 Netty 應用
- 設置開發環境
- Netty 客戶端/服務端 總覽
- 寫一個 echo 服務器
- 寫一個 echo 客戶端
- 編譯和運行 Echo 服務器和客戶端
- 總結
- Netty 總覽
- Netty 快速入門
- Channel, Event 和 I/O
- 什么是 Bootstrapping 為什么要用
- ChannelHandler 和 ChannelPipeline
- 近距離觀察 ChannelHandler
- 總結
- 核心功能
- Transport(傳輸)
- 案例研究:Transport 的遷移
- Transport API
- 包含的 Transport
- Transport 使用情況
- 總結
- Buffer(緩沖)
- Buffer API
- ByteBuf - 字節數據的容器
- 字節級別的操作
- ByteBufHolder
- ByteBuf 分配
- 總結
- ChannelHandler 和 ChannelPipeline
- ChannelHandler 家族
- ChannelPipeline
- ChannelHandlerContext
- 總結
- Codec 框架
- 什么是 Codec
- Decoder(解碼器)
- Encoder(編碼器)
- 抽象 Codec(編解碼器)類
- 總結
- 提供了的 ChannelHandler 和 Codec
- 使用 SSL/TLS 加密 Netty 程序
- 構建 Netty HTTP/HTTPS 應用
- 空閑連接以及超時
- 解碼分隔符和基于長度的協議
- 編寫大型數據
- 序列化數據
- 總結
- Bootstrap 類型
- 引導客戶端和無連接協議
- 引導服務器
- 從 Channel 引導客戶端
- 在一個引導中添加多個 ChannelHandler
- 使用Netty 的 ChannelOption 和屬性
- 關閉之前已經引導的客戶端或服務器
- 總結
- 引導
- Bootstrap 類型
- 引導客戶端和無連接協議
- 引導服務器
- 從 Channel 引導客戶端
- 在一個引導中添加多個 ChannelHandler
- 使用Netty 的 ChannelOption 和屬性
- 關閉之前已經引導的客戶端或服務器
- 總結
- NETTY BY EXAMPLE
- 單元測試
- 總覽
- 測試 ChannelHandler
- 測試異常處理
- 總結
- WebSocket
- WebSocket 程序示例
- 添加 WebSocket 支持
- 測試程序
- 總結
- SPDY
- SPDY 背景
- 示例程序
- 實現
- 啟動 SpdyServer 并測試
- 總結
- 通過 UDP 廣播事件
- UDP 基礎
- UDP 廣播
- UDP 示例
- EventLog 的 POJO
- 寫廣播器
- 寫監視器
- 運行 LogEventBroadcaster 和 LogEventMonitor
- 總結
- 高級主題
- 實現自定義的編解碼器
- 編解碼器的范圍
- 實現 Memcached 編解碼器
- 了解 Memcached 二進制協議
- Netty 編碼器和解碼器
- 測試編解碼器
- EventLoop 和線程模型
- 線程模型的總覽
- EventLoop
- EventLoop
- I/O EventLoop/Thread 分配細節
- 總結
- 用例1:Droplr Firebase 和 Urban Airship
- 用例2:Facebook 和 Twitter