第七篇:SPDY

来源:互联网 发布:html5 game 源码 编辑:程序博客网 时间:2024/06/08 02:59

第七篇:SPDY

12.1 SPDY概念及背景

        SPDY Google 开发的基于传输控制协议(TCP)的应用层协议 ,开发组正在推动SPDY成为正式标准(现为互联网草案)。SPDY协议旨在通过压缩、多路复用和优先级来缩短网页的加载时间和提高安全性。(SPDYSpeedy的昵音,意思是更快)。

        为什么需要SPDYSPDY协议只是在性能上对HTTP做了很大的优化,其核心思想是尽量减少连接个数,而对于HTTP的语义并没有做太大的修改。具体来说是,SPDY使用了HTTP的方法和页眉,但是删除了一些头并重写了HTTP中管理连接和数据转移格式的部分,所以基本上是兼容HTTP的。

        Google SPDY 白皮书里表示要向协议栈下面渗透并替换掉传输层协议(TCP),但是因为这样无论是部署起来还是实现起来暂时相当困难,因此Google准备先对应用层协议HTTP进行改进,先在SSL之上增加一个会话层来实现SPDY协议,而HTTPGET POST 消息格式保持不变,即现有的所有服务端应用均不用做任何修改。因此在目前,SPDY的目的是为了加强HTTP,是对HTTP一个更好的实现和支持。至于未来SPDY得到广泛应用后会不会演一出狸猫换太子,替换掉HTTP并彻底颠覆整个Internet就是Google的事情了。

        距离万维网之父蒂姆·伯纳斯-李发明并推动HTTP成为如今互联网最流行的协议已经过去十几年了(现用HTTP 1.1规范也停滞了13年了),随着现在WEB技术的飞速发展尤其是HTML5的不断演进,包括WebSockets协议的出现以及当前网络环境的改变、传输内容的变化,当初的HTTP规范已经逐渐无法满足人们的需要了,HTTP需要进一步发展,因此HTTPbis工作组已经被组建并被授权考虑HTTP 2.0,希望能解决掉目前HTTP所带来的诸多限制。而SPDY正是GoogleHTTP即将从1.1跨越到2.0之际推出的试图成为下一代互联网通信的协议,长期以来一直被认为是HTTP 2.0唯一可行选择。

        SPDY相比HTTP有如下优点:

1. SPDY多路复用,请求优化;而HTTP单路连接,请求低效

2. SPDY支持服务器推送技术;而HTTP只允许由客户端主动发起请求

3. SPDY压缩了HTTP头信息,节省了传输数据的带宽流量;而HTTP头冗余,同一个会话会反复送头信息

4. SPDY强制使用SSL传输协议,全部请求SSL加密后,信息传输更安全

谷歌表示,引入SPDY协议后,在实验室测试中页面加载速度比原先快64%

        支持SPDY协议的浏览器:

· Google Chrome 19+Chromium 19+

· Mozilla Firefox 11+,从13开始默认支持

· Opera 12.10+

· Internet Explorer 11+

12.2 本例子流程图

 

12.3 Netty中使用SPDY

        支持SPDYChannelPipeline如下图:

 

        不支持SPDYChannelPipeline如下图:

 

        例子代码如下:

[java] view plaincopy

1. package netty.in.action.spdy;  

2.   

3. import java.util.Arrays;  

4. import java.util.Collections;  

5. import java.util.List;  

6.   

7. import org.eclipse.jetty.npn.NextProtoNego.ServerProvider;  

8.   

9. public class DefaultServerProvider implements ServerProvider {  

10.   

11.     private static final List<String> PROTOCOLS = Collections.unmodifiableList(Arrays  

12.             .asList("spdy/3.1""http/1.1""http/1.0""Unknown"));  

13.   

14.     private String protocol;  

15.   

16.     public String getSelectedProtocol() {  

17.         return protocol;  

18.     }  

19.   

20.     @Override  

21.     public void protocolSelected(String arg0) {  

22.         this.protocol = arg0;  

23.     }  

24.   

25.     @Override  

26.     public List<String> protocols() {  

27.         return PROTOCOLS;  

28.     }  

29.   

30.     @Override  

31.     public void unsupported() {  

32.         protocol = "http/1.1";  

33.     }  

34.   

35. }  

[java] view plaincopy

1. package netty.in.action.spdy;  

2.   

3. import io.netty.channel.ChannelFuture;  

4. import io.netty.channel.ChannelFutureListener;  

5. import io.netty.channel.ChannelHandlerContext;  

6. import io.netty.channel.SimpleChannelInboundHandler;  

7. import io.netty.handler.codec.http.DefaultFullHttpResponse;  

8. import io.netty.handler.codec.http.FullHttpRequest;  

9. import io.netty.handler.codec.http.FullHttpResponse;  

10. import io.netty.handler.codec.http.HttpHeaders;  

11. import io.netty.handler.codec.http.HttpResponseStatus;  

12. import io.netty.handler.codec.http.HttpVersion;  

13. import io.netty.util.CharsetUtil;  

14.   

15. public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {  

16.   

17.     @Override  

18.     protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request)  

19.             throws Exception {  

20.         if (HttpHeaders.is100ContinueExpected(request)) {  

21.             send100Continue(ctx);  

22.         }  

23.         FullHttpResponse response = new DefaultFullHttpResponse(  

24.                 request.getProtocolVersion(), HttpResponseStatus.OK);  

25.         response.content().writeBytes(getContent().getBytes(CharsetUtil.UTF_8));  

26.         response.headers().set(HttpHeaders.Names.CONTENT_TYPE,  

27.                 "text/plain; charset=UTF-8");  

28.         boolean keepAlive = HttpHeaders.isKeepAlive(request);  

29.         if (keepAlive) {  

30.             response.headers().set(HttpHeaders.Names.CONTENT_LENGTH,  

31.                     response.content().readableBytes());  

32.             response.headers().set(HttpHeaders.Names.CONNECTION,  

33.                     HttpHeaders.Values.KEEP_ALIVE);  

34.         }  

35.         ChannelFuture future = ctx.writeAndFlush(response);  

36.         if (!keepAlive) {  

37.             future.addListener(ChannelFutureListener.CLOSE);  

38.         }  

39.     }  

40.   

41.     private static void send100Continue(ChannelHandlerContext ctx) {  

42.         FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,  

43.                 HttpResponseStatus.CONTINUE);  

44.         ctx.writeAndFlush(response);  

45.     }  

46.   

47.     protected String getContent() {  

48.         return "This content is transmitted via HTTP\r\n";  

49.     }  

50.   

51.     @Override  

52.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)  

53.             throws Exception {  

54.         cause.printStackTrace();  

55.         ctx.close();  

56.     }  

57. }  

[java] view plaincopy

1. package netty.in.action.spdy;  

2.   

3. public class SpdyRequestHandler extends HttpRequestHandler {  

4.   

5.     @Override  

6.     protected String getContent() {  

7.         return "This content is transmitted via SPDY\r\n";  

8.     }  

9.       

10. }  

[java] view plaincopy

1. package netty.in.action.spdy;  

2.   

3. import io.netty.channel.ChannelInboundHandler;  

4. import io.netty.handler.codec.spdy.SpdyOrHttpChooser;  

5.   

6. import javax.net.ssl.SSLEngine;  

7.   

8. import org.eclipse.jetty.npn.NextProtoNego;  

9.   

10. public class DefaultSpdyOrHttpChooser extends SpdyOrHttpChooser {  

11.   

12.     protected DefaultSpdyOrHttpChooser(int maxSpdyContentLength, int maxHttpContentLength) {  

13.         super(maxSpdyContentLength, maxHttpContentLength);  

14.     }  

15.   

16.     @Override  

17.     protected SelectedProtocol getProtocol(SSLEngine engine) {  

18.         DefaultServerProvider provider = (DefaultServerProvider) NextProtoNego  

19.                 .get(engine);  

20.         String protocol = provider.getSelectedProtocol();  

21.         if (protocol == null) {  

22.             return SelectedProtocol.UNKNOWN;  

23.         }  

24.         switch (protocol) {  

25.         case "spdy/3.1":  

26.             return SelectedProtocol.SPDY_3_1;  

27.         case "http/1.0":  

28.         case "http/1.1":  

29.             return SelectedProtocol.HTTP_1_1;  

30.         default:  

31.             return SelectedProtocol.UNKNOWN;  

32.         }  

33.     }  

34.   

35.     @Override  

36.     protected ChannelInboundHandler createHttpRequestHandlerForHttp() {  

37.         return new HttpRequestHandler();  

38.     }  

39.   

40.     @Override  

41.     protected ChannelInboundHandler createHttpRequestHandlerForSpdy() {  

42.         return new SpdyRequestHandler();  

43.     }  

44.       

45. }  

[java] view plaincopy

1. package netty.in.action.spdy;  

2.   

3. import io.netty.channel.Channel;  

4. import io.netty.channel.ChannelInitializer;  

5. import io.netty.channel.ChannelPipeline;  

6. import io.netty.handler.ssl.SslHandler;  

7.   

8. import javax.net.ssl.SSLContext;  

9. import javax.net.ssl.SSLEngine;  

10.   

11. import org.eclipse.jetty.npn.NextProtoNego;  

12.   

13. public class SpdyChannelInitializer extends ChannelInitializer<Channel> {  

14.     private final SSLContext context;  

15.   

16.     public SpdyChannelInitializer(SSLContext context) {  

17.         this.context = context;  

18.     }  

19.   

20.     @Override  

21.     protected void initChannel(Channel ch) throws Exception {  

22.         ChannelPipeline pipeline = ch.pipeline();  

23.         SSLEngine engine = context.createSSLEngine();  

24.         engine.setUseClientMode(false);  

25.         NextProtoNego.put(engine, new DefaultServerProvider());  

26.         NextProtoNego.debug = true;  

27.         pipeline.addLast("sslHandler"new SslHandler(engine));  

28.         pipeline.addLast("chooser",  

29.                 new DefaultSpdyOrHttpChooser(1024 * 10241024 * 1024));  

30.     }  

31.   

32. }  

[java] view plaincopy

1. package netty.in.action.spdy;  

2.   

3. import io.netty.bootstrap.ServerBootstrap;  

4. import io.netty.channel.Channel;  

5. import io.netty.channel.ChannelFuture;  

6. import io.netty.channel.nio.NioEventLoopGroup;  

7. import io.netty.channel.socket.nio.NioServerSocketChannel;  

8. import io.netty.example.securechat.SecureChatSslContextFactory;  

9.   

10. import java.net.InetSocketAddress;  

11.   

12. import javax.net.ssl.SSLContext;  

13.   

14. public class SpdyServer {  

15.   

16.     private final NioEventLoopGroup group = new NioEventLoopGroup();  

17.     private final SSLContext context;  

18.     private Channel channel;  

19.   

20.     public SpdyServer(SSLContext context) {  

21.         this.context = context;  

22.     }  

23.   

24.     public ChannelFuture start(InetSocketAddress address) {  

25.         ServerBootstrap bootstrap = new ServerBootstrap();  

26.         bootstrap.group(group).channel(NioServerSocketChannel.class)  

27.                 .childHandler(new SpdyChannelInitializer(context));  

28.         ChannelFuture future = bootstrap.bind(address);  

29.         future.syncUninterruptibly();  

30.         channel = future.channel();  

31.         return future;  

32.     }  

33.   

34.     public void destroy() {  

35.         if (channel != null) {  

36.             channel.close();  

37.         }  

38.         group.shutdownGracefully();  

39.     }  

40.   

41.     public static void main(String[] args) {  

42.         SSLContext context = SecureChatSslContextFactory.getServerContext();  

43.         final SpdyServer endpoint = new SpdyServer(context);  

44.         ChannelFuture future = endpoint.start(new InetSocketAddress(4096));  

45.         Runtime.getRuntime().addShutdownHook(new Thread() {  

46.             @Override  

47.             public void run() {  

48.                 endpoint.destroy();  

49.             }  

50.         });  

51.         future.channel().closeFuture().syncUninterruptibly();  

52.     }  

53.   

54. }  

使用SSL需要使用到SSLContext,下面代买是获取SSLContext对象:

[java] view plaincopy

1. /* 

2.  * Copyright 2012 The Netty Project 

3.  * 

4.  * The Netty Project licenses this file to you under the Apache License, 

5.  * version 2.0 (the "License"); you may not use this file except in compliance 

6.  * with the License. You may obtain a copy of the License at: 

7.  * 

8.  *   http://www.apache.org/licenses/LICENSE-2.0 

9.  * 

10.  * Unless required by applicable law or agreed to in writing, software 

11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13.  * License for the specific language governing permissions and limitations 

14.  * under the License. 

15.  */  

16. package netty.in.action.spdy;  

17.   

18. import javax.net.ssl.ManagerFactoryParameters;  

19. import javax.net.ssl.TrustManager;  

20. import javax.net.ssl.TrustManagerFactorySpi;  

21. import javax.net.ssl.X509TrustManager;  

22. import java.security.InvalidAlgorithmParameterException;  

23. import java.security.KeyStore;  

24. import java.security.KeyStoreException;  

25. import java.security.cert.X509Certificate;  

26.   

27. /** 

28.  * Bogus {@link TrustManagerFactorySpi} which accepts any certificate 

29.  * even if it is invalid. 

30.  */  

31. public class SecureChatTrustManagerFactory extends TrustManagerFactorySpi {  

32.   

33.     private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() {  

34.         @Override  

35.         public X509Certificate[] getAcceptedIssuers() {  

36.             return new X509Certificate[0];  

37.         }  

38.   

39.         @Override  

40.         public void checkClientTrusted(X509Certificate[] chain, String authType) {  

41.             // Always trust - it is an example.  

42.             // You should do something in the real world.  

43.             // You will reach here only if you enabled client certificate auth,  

44.             // as described in SecureChatSslContextFactory.  

45.             System.err.println(  

46.                     "UNKNOWN CLIENT CERTIFICATE: " + chain[0].getSubjectDN());  

47.         }  

48.   

49.         @Override  

50.         public void checkServerTrusted(X509Certificate[] chain, String authType) {  

51.             // Always trust - it is an example.  

52.             // You should do something in the real world.  

53.             System.err.println(  

54.                     "UNKNOWN SERVER CERTIFICATE: " + chain[0].getSubjectDN());  

55.         }  

56.     };  

57.   

58.     public static TrustManager[] getTrustManagers() {  

59.         return new TrustManager[] { DUMMY_TRUST_MANAGER };  

60.     }  

61.   

62.     @Override  

63.     protected TrustManager[] engineGetTrustManagers() {  

64.         return getTrustManagers();  

65.     }  

66.   

67.     @Override  

68.     protected void engineInit(KeyStore keystore) throws KeyStoreException {  

69.         // Unused  

70.     }  

71.   

72.     @Override  

73.     protected void engineInit(ManagerFactoryParameters managerFactoryParameters)  

74.             throws InvalidAlgorithmParameterException {  

75.         // Unused  

76.     }  

77. }  

[java] view plaincopy

1. /* 

2.  * Copyright 2012 The Netty Project 

3.  * 

4.  * The Netty Project licenses this file to you under the Apache License, 

5.  * version 2.0 (the "License"); you may not use this file except in compliance 

6.  * with the License. You may obtain a copy of the License at: 

7.  * 

8.  *   http://www.apache.org/licenses/LICENSE-2.0 

9.  * 

10.  * Unless required by applicable law or agreed to in writing, software 

11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13.  * License for the specific language governing permissions and limitations 

14.  * under the License. 

15.  */  

16. package netty.in.action.spdy;  

17.   

18. import java.io.ByteArrayInputStream;  

19. import java.io.InputStream;  

20.   

21. /** 

22.  * A bogus key store which provides all the required information to 

23.  * create an example SSL connection. 

24.  * 

25.  * To generate a bogus key store: 

26.  * <pre> 

27.  * keytool  -genkey -alias securechat -keysize 2048 -validity 36500 

28.  *          -keyalg RSA -dname "CN=securechat" 

29.  *          -keypass secret -storepass secret 

30.  *          -keystore cert.jks 

31.  * </pre> 

32.  */  

33. public final class SecureChatKeyStore {  

34.     private static final short[] DATA = {  

35.         0xfe0xed0xfe0xed0x000x000x000x02,  

36.         0x000x000x000x020x000x000x000x01,  

37.         0x000x070x650x780x610x6d0x700x6c,  

38.         0x650x000x000x010x1a0x9f0x570xa5,  

39.         0x270x000x000x010x9a0x300x820x01,  

40.         0x960x300x0e0x060x0a0x2b0x060x01,  

41.         0x040x010x2a0x020x110x010x010x05,  

42.         0x000x040x820x010x820x480x6d0xcf,  

43.         0x160xb50x500x950x360xbf0x470x27,  

44.         0x500x580x0d0xa20x520x7e0x250xab,  

45.         0x140x1a0x260x5e0x2d0x8a0x230x90,  

46.         0x600x7f0x120x200x560xd10x430xa2,  

47.         0x6b0x470x5d0xed0x9d0xd40xe50x83,  

48.         0x280x890xc20x160x4c0x760x060xad,  

49.         0x8e0x8c0x290x1a0x9b0x0f0xdd0x60,  

50.         0x4b0xb40x620x820x9e0x4a0x630x83,  

51.         0x2e0xd20x430x780xc20x320x1f0x60,  

52.         0xa90x8a0x7f0x0f0x7c0xa60x1d0xe6,  

53.         0x920x9e0x520xc70x7d0xbb0x350x3b,  

54.         0xaa0x890x730x4c0xfb0x990x540x97,  

55.         0x990x280x6e0x660x5b0xf70x9b0x7e,  

56.         0x6d0x8a0x2f0xfa0xc30x1e0x710xb9,  

57.         0xbd0x8f0xc50x630x250x310x200x02,  

58.         0xff0x020xf00xc90x2c0xdd0x3a0x10,  

59.         0x300xab0xe50xad0x3d0x1a0x820x77,  

60.         0x460xed0x030x380xa40x730x6d0x36,  

61.         0x360x330x700xb20x630x200xca0x03,  

62.         0xbf0x5a0xf40x7c0x350xf00x630x1a,  

63.         0x120x330x120x580xd90xa20x630x6b,  

64.         0x630x820x410x650x700x370x4b0x99,  

65.         0x040x9f0xdd0x5e0x070x010x950x9f,  

66.         0x360xe80xc30x660x2a0x210x690x68,  

67.         0x400xe60xbc0xbb0x850x810x210x13,  

68.         0xe60xa40xcf0xd30x670xe30xfd0x75,  

69.         0xf00xdf0x830xe00xc50x360x090xac,  

70.         0x1b0xd40xf70x2a0x230x570x1c0x5c,  

71.         0x0f0xf40xcf0xa20xcf0xf50xbd0x9c,  

72.         0x690x980x780x3a0x250xe40xfd0x85,  

73.         0x110xcc0x7d0xef0xeb0x740x600xb1,  

74.         0xb70xfb0x1f0x0e0x620xff0xfe0x09,  

75.         0x0a0xc30x800x2f0x100x490x890x78,  

76.         0xd20x080xfa0x890x220x450x910x21,  

77.         0xbc0x900x3e0xad0xb30x0a0xb40x0e,  

78.         0x1c0xa10x930x920xd80x720x070x54,  

79.         0x600xe70x910xfc0xd90x3c0xe10x6f,  

80.         0x080xe40x560xf60x0b0xb00x3c0x39,  

81.         0x8a0x2d0x480x440x280x130xca0xe9,  

82.         0xf70xa30xb60x8a0x5f0x310xa90x72,  

83.         0xf20xde0x960xf20xb10x530xb10x3e,  

84.         0x240x570xfd0x180x450x1f0xc50x33,  

85.         0x1b0xa40xe80x210xfa0x0e0xb20xb9,  

86.         0xcb0xc70x070x410xdd0x2f0xb60x6a,  

87.         0x230x180xed0xc10xef0xe20x4b0xec,  

88.         0xc90xba0xfb0x460x430x900xd70xb5,  

89.         0x680x280x310x2b0x8d0xa80x510x63,  

90.         0xf70x530x990x190x680x850x660x00,  

91.         0x000x000x010x000x050x580x2e0x35,  

92.         0x300x390x000x000x020x3a0x300x82,  

93.         0x020x360x300x820x010xe00xa00x03,  

94.         0x020x010x020x020x040x480x590xf1,  

95.         0x920x300x0d0x060x090x2a0x860x48,  

96.         0x860xf70x0d0x010x010x050x050x00,  

97.         0x300x810xa00x310x0b0x300x090x06,  

98.         0x030x550x040x060x130x020x4b0x52,  

99.         0x310x130x300x110x060x030x550x04,  

100.         0x080x130x0a0x4b0x790x750x6e0x67,  

101.         0x670x690x2d0x640x6f0x310x140x30,  

102.         0x120x060x030x550x040x070x130x0b,  

103.         0x530x650x6f0x6e0x670x6e0x610x6d,  

104.         0x2d0x730x690x310x1a0x300x180x06,  

105.         0x030x550x040x0a0x130x110x540x68,  

106.         0x650x200x4e0x650x740x740x790x20,  

107.         0x500x720x6f0x6a0x650x630x740x31,  

108.         0x180x300x160x060x030x550x040x0b,  

109.         0x130x0f0x450x780x610x6d0x700x6c,  

110.         0x650x200x410x750x740x680x6f0x72,  

111.         0x730x310x300x300x2e0x060x030x55,  

112.         0x040x030x130x270x730x650x630x75,  

113.         0x720x650x630x680x610x740x2e0x65,  

114.         0x780x610x6d0x700x6c0x650x2e0x6e,  

115.         0x650x740x740x790x2e0x670x6c0x65,  

116.         0x610x6d0x790x6e0x6f0x640x650x2e,  

117.         0x6e0x650x740x300x200x170x0d0x30,  

118.         0x380x300x360x310x390x300x350x34,  

119.         0x310x330x380x5a0x180x0f0x320x31,  

120.         0x380x370x310x310x320x340x300x35,  

121.         0x340x310x330x380x5a0x300x810xa0,  

122.         0x310x0b0x300x090x060x030x550x04,  

123.         0x060x130x020x4b0x520x310x130x30,  

124.         0x110x060x030x550x040x080x130x0a,  

125.         0x4b0x790x750x6e0x670x670x690x2d,  

126.         0x640x6f0x310x140x300x120x060x03,  

127.         0x550x040x070x130x0b0x530x650x6f,  

128.         0x6e0x670x6e0x610x6d0x2d0x730x69,  

129.         0x310x1a0x300x180x060x030x550x04,  

130.         0x0a0x130x110x540x680x650x200x4e,  

131.         0x650x740x740x790x200x500x720x6f,  

132.         0x6a0x650x630x740x310x180x300x16,  

133.         0x060x030x550x040x0b0x130x0f0x45,  

134.         0x780x610x6d0x700x6c0x650x200x41,  

135.         0x750x740x680x6f0x720x730x310x30,  

136.         0x300x2e0x060x030x550x040x030x13,  

137.         0x270x730x650x630x750x720x650x63,  

138.         0x680x610x740x2e0x650x780x610x6d,  

139.         0x700x6c0x650x2e0x6e0x650x740x74,  

140.         0x790x2e0x670x6c0x650x610x6d0x79,  

141.         0x6e0x6f0x640x650x2e0x6e0x650x74,  

142.         0x300x5c0x300x0d0x060x090x2a0x86,  

143.         0x480x860xf70x0d0x010x010x010x05,  

144.         0x000x030x4b0x000x300x480x020x41,  

145.         0x000xc30xe30x5e0x410xa70x870x11,  

146.         0x000x420x2a0xb00x4b0xed0xb20xe0,  

147.         0x230xdb0xb10x3d0x580x970x350x60,  

148.         0x0b0x820x590xd30x000xea0xd40x61,  

149.         0xb80x790x3f0xb60x3c0x120x050x93,  

150.         0x2e0x9a0x590x680x140x770x3a0xc8,  

151.         0x500x250x570xa40x490x180x630x41,  

152.         0xf00x2d0x280xec0x060xfb0xb40x9f,  

153.         0xbf0x020x030x010x000x010x300x0d,  

154.         0x060x090x2a0x860x480x860xf70x0d,  

155.         0x010x010x050x050x000x030x410x00,  

156.         0x650x6c0x300x010xc20x8e0x3e0xcb,  

157.         0xb30x770x480xe90x660x610x9a0x40,  

158.         0x860xaf0xf60x030xeb0xba0x6a0xf2,  

159.         0xfd0xe20xaf0x360x5e0x7b0xaa0x22,  

160.         0x040xdd0x2c0x200xc40xfc0xdd0xd0,  

161.         0x820x200x1c0x3d0xd70x9e0x5e0x5c,  

162.         0x920x5a0x760x710x280xf50x070x7d,  

163.         0xa20x810xba0x770x9f0x2a0xd90x44,  

164.         0x000x000x000x010x000x050x6d0x79,  

165.         0x6b0x650x790x000x000x010x1a0x9f,  

166.         0x5b0x560xa00x000x000x010x990x30,  

167.         0x820x010x950x300x0e0x060x0a0x2b,  

168.         0x060x010x040x010x2a0x020x110x01,  

169.         0x010x050x000x040x820x010x810x29,  

170.         0xa80xb60x080x0c0x850x750x3e0xdd,  

171.         0xb50xe50x1a0x870x680xd10x900x4b,  

172.         0x290x310xee0x900xbc0x9d0x730xa0,  

173.         0x3f0xe90x0b0xa40xef0x300x9b0x36,  

174.         0x9a0xb20x540x770x810x070x4b0xaa,  

175.         0xa50x770x980xe10xeb0xb50x7c0x4e,  

176.         0x480xd50x080xfc0x2c0x360xe20x65,  

177.         0x030xac0xe50xf30x960xb70xd00xb5,  

178.         0x3b0x920xe40x140x050x7a0x6a0x92,  

179.         0x560xfe0x4e0xab0xd30x0e0x320x04,  

180.         0x220x220x740x470x7d0xec0x210x99,  

181.         0x300x310x640x460x640x9b0xc70x13,  

182.         0xbf0xbe0xd00x310x490xe70x3c0xbf,  

183.         0xba0xb10x200xf90x420xf40xa90xa9,  

184.         0xe50x130x650x320xbf0x7c0xcc0x91,  

185.         0xd30xfd0x240x470x0b0xe50x530xad,  

186.         0x500x300x560xd10xfa0x9c0x370xa8,  

187.         0xc10xce0xf60x0b0x180xaa0x7c0xab,  

188.         0xbd0x1f0xdf0xe40x800xb80xa70xe0,  

189.         0xad0x7d0x500x740xf10x980x780xbc,  

190.         0x580xb90xc20x520xbe0xd20x5b0x81,  

191.         0x940x830x8f0xb90x4c0xee0x010x2b,  

192.         0x5e0xc90x6e0x9b0xf50x630x690xe4,  

193.         0xd80x0b0x470xd80xfd0xd80xe00xed,  

194.         0xa80x270x030x740x1e0x5d0x320xe6,  

195.         0x5c0x630xc20xfb0x3f0xee0xb40x13,  

196.         0xc60x0e0x6e0x740xe00x220xac0xce,  

197.         0x790xf90x430x680xc10x030x740x2b,  

198.         0xe10x180xf80x7f0x760x9a0xea0x82,  

199.         0x3f0xc20xa60xa70x4c0xfe0xae0x29,  

200.         0x3b0xc10x100x7c0xd50x770x170x79,  

201.         0x5f0xcb0xad0x1f0xd80xa10xfd0x90,  

202.         0xe10x6b0xb20xef0xb90x410x260xa4,  

203.         0x0b0x4f0xc60x830x050x6f0xf00x64,  

204.         0x400xe10x440xc40xf90x400x2b0x3b,  

205.         0x400xdb0xaf0x350xa40x9b0x9f0xc4,  

206.         0x740x070xe50x180x600xc50xfe0x15,  

207.         0x0e0x3a0x250x2a0x110xee0x780x2f,  

208.         0xb80xd10x6e0x4e0x3c0x0a0xb50xb9,  

209.         0x400x860x270x6d0x8f0x530xb70x77,  

210.         0x360xec0x5d0xed0x320x400x430x82,  

211.         0xc30x520x580xc40x260x390xf30xb3,  

212.         0xad0x580xab0xb70xf70x8e0x0e0xba,  

213.         0x8e0x780x9d0xbf0x580x340xbd0x77,  

214.         0x730xa60x500x550x000x600x260xbf,  

215.         0x6d0xb40x980x8a0x180x830x890xf8,  

216.         0xcd0x0d0x490x060xae0x510x6e0xaf,  

217.         0xbd0xe20x070x130xd80x640xcc0xbf,  

218.         0x000x000x000x010x000x050x580x2e,  

219.         0x350x300x390x000x000x020x340x30,  

220.         0x820x020x300x300x820x010xda0xa0,  

221.         0x030x020x010x020x020x040x480x59,  

222.         0xf20x840x300x0d0x060x090x2a0x86,  

223.         0x480x860xf70x0d0x010x010x050x05,  

224.         0x000x300x810x9d0x310x0b0x300x09,  

225.         0x060x030x550x040x060x130x020x4b,  

226.         0x520x310x130x300x110x060x030x55,  

227.         0x040x080x130x0a0x4b0x790x750x6e,  

228.         0x670x670x690x2d0x640x6f0x310x14,  

229.         0x300x120x060x030x550x040x070x13,  

230.         0x0b0x530x650x6f0x6e0x670x6e0x61,  

231.         0x6d0x2d0x730x690x310x1a0x300x18,  

232.         0x060x030x550x040x0a0x130x110x54,  

233.         0x680x650x200x4e0x650x740x740x79,  

234.         0x200x500x720x6f0x6a0x650x630x74,  

235.         0x310x150x300x130x060x030x550x04,  

236.         0x0b0x130x0c0x430x6f0x6e0x740x72,  

237.         0x690x620x750x740x6f0x720x730x31,  

238.         0x300x300x2e0x060x030x550x040x03,  

239.         0x130x270x730x650x630x750x720x65,  

240.         0x630x680x610x740x2e0x650x780x61,  

241.         0x6d0x700x6c0x650x2e0x6e0x650x74,  

242.         0x740x790x2e0x670x6c0x650x610x6d,  

243.         0x790x6e0x6f0x640x650x2e0x6e0x65,  

244.         0x740x300x200x170x0d0x300x380x30,  

245.         0x360x310x390x300x350x340x350x34,  

246.         0x300x5a0x180x0f0x320x310x380x37,  

247.         0x310x310x320x330x300x350x340x35,  

248.         0x340x300x5a0x300x810x9d0x310x0b,  

249.         0x300x090x060x030x550x040x060x13,  

250.         0x020x4b0x520x310x130x300x110x06,  

251.         0x030x550x040x080x130x0a0x4b0x79,  

252.         0x750x6e0x670x670x690x2d0x640x6f,  

253.         0x310x140x300x120x060x030x550x04,  

254.         0x070x130x0b0x530x650x6f0x6e0x67,  

255.         0x6e0x610x6d0x2d0x730x690x310x1a,  

256.         0x300x180x060x030x550x040x0a0x13,  

257.         0x110x540x680x650x200x4e0x650x74,  

258.         0x740x790x200x500x720x6f0x6a0x65,  

259.         0x630x740x310x150x300x130x060x03,  

260.         0x550x040x0b0x130x0c0x430x6f0x6e,  

261.         0x740x720x690x620x750x740x6f0x72,  

262.         0x730x310x300x300x2e0x060x030x55,  

263.         0x040x030x130x270x730x650x630x75,  

264.         0x720x650x630x680x610x740x2e0x65,  

265.         0x780x610x6d0x700x6c0x650x2e0x6e,  

266.         0x650x740x740x790x2e0x670x6c0x65,  

267.         0x610x6d0x790x6e0x6f0x640x650x2e,  

268.         0x6e0x650x740x300x5c0x300x0d0x06,  

269.         0x090x2a0x860x480x860xf70x0d0x01,  

270.         0x010x010x050x000x030x4b0x000x30,  

271.         0x480x020x410x000x950xb30x470x17,  

272.         0x950x0f0x570xcf0x660x720x0a0x7e,  

273.         0x5b0x540xea0x8c0x6f0x790xde0x94,  

274.         0xac0x0b0x5a0xd40xd60x1b0x580x12,  

275.         0x1a0x160x3d0xfe0xdf0xa50x2b0x86,  

276.         0xbc0x640xd40x800x1e0x3f0xf90xe2,  

277.         0x040x030x790x9b0xc10x5c0xf00xf1,  

278.         0xf30xf10xe30xbf0x3f0xc00x1f0xdd,  

279.         0xdb0xc00x5b0x210x020x030x010x00,  

280.         0x010x300x0d0x060x090x2a0x860x48,  

281.         0x860xf70x0d0x010x010x050x050x00,  

282.         0x030x410x000x020xd70xdd0xbd0x0c,  

283.         0x8e0x210x200xef0x9e0x4f0x1f0xf5,  

284.         0x490xf10xae0x580x9b0x940x3a0x1f,  

285.         0x700x330xf00x9b0xbb0xe90xc00xf3,  

286.         0x720xcb0xde0xb60x560x720xcc0x1c,  

287.         0xf00xd60x5a0x2a0xbc0xa10x7e0x23,  

288.         0x830xe90xe70xcf0x9e0xa50xf90xcc,  

289.         0xc20x610xf40xdb0x400x930x1d0x63,  

290.         0x8a0x500x4c0x110x390xb10x910xc1,  

291.         0xe60x9d0xd90x1a0x620x1b0xb80xd3,  

292.         0xd60x9a0x6d0xb90x8e0x150x51 };  

293.   

294.     public static InputStream asInputStream() {  

295.         byte[] data = new byte[DATA.length];  

296.         for (int i = 0; i < data.length; i ++) {  

297.             data[i] = (byte) DATA[i];  

298.         }  

299.         return new ByteArrayInputStream(data);  

300.     }  

301.   

302.     public static char[] getCertificatePassword() {  

303.         return "secret".toCharArray();  

304.     }  

305.   

306.     public static char[] getKeyStorePassword() {  

307.         return "secret".toCharArray();  

308.     }  

309.   

310.     private SecureChatKeyStore() {  

311.         // Unused  

312.     }  

313. }  

[java] view plaincopy

1. /* 

2.  * Copyright 2012 The Netty Project 

3.  * 

4.  * The Netty Project licenses this file to you under the Apache License, 

5.  * version 2.0 (the "License"); you may not use this file except in compliance 

6.  * with the License. You may obtain a copy of the License at: 

7.  * 

8.  *   http://www.apache.org/licenses/LICENSE-2.0 

9.  * 

10.  * Unless required by applicable law or agreed to in writing, software 

11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13.  * License for the specific language governing permissions and limitations 

14.  * under the License. 

15.  */  

16. package netty.in.action.spdy;  

17.   

18. import io.netty.handler.ssl.SslHandler;  

19. import io.netty.util.internal.SystemPropertyUtil;  

20.   

21. import java.security.KeyStore;  

22. import java.security.SecureRandom;  

23.   

24. import javax.net.ssl.KeyManager;  

25. import javax.net.ssl.KeyManagerFactory;  

26. import javax.net.ssl.SSLContext;  

27. import javax.net.ssl.SSLEngine;  

28. import javax.net.ssl.TrustManager;  

29.   

30. /** 

31.  * Creates a bogus {@link SSLContext}.  A client-side context created by this 

32.  * factory accepts any certificate even if it is invalid.  A server-side context 

33.  * created by this factory sends a bogus certificate defined in {@link SecureChatKeyStore}. 

34.  * <p> 

35.  * You will have to create your context differently in a real world application. 

36.  * 

37.  * <h3>Client Certificate Authentication</h3> 

38.  * 

39.  * To enable client certificate authentication: 

40.  * <ul> 

41.  * <li>Enable client authentication on the server side by calling 

42.  *     {@link SSLEngine#setNeedClientAuth(boolean)} before creating 

43.  *     {@link SslHandler}.</li> 

44.  * <li>When initializing an {@link SSLContext} on the client side, 

45.  *     specify the {@link KeyManager} that contains the client certificate as 

46.  *     the first argument of {@link SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}.</li> 

47.  * <li>When initializing an {@link SSLContext} on the server side, 

48.  *     specify the proper {@link TrustManager} as the second argument of 

49.  *     {@link SSLContext#init(KeyManager[], TrustManager[], SecureRandom)} 

50.  *     to validate the client certificate.</li> 

51.  * </ul> 

52.  */  

53. public final class SecureChatSslContextFactory {  

54.   

55.     private static final String PROTOCOL = "TLS";  

56.     private static final SSLContext SERVER_CONTEXT;  

57.     private static final SSLContext CLIENT_CONTEXT;  

58.   

59.     static {  

60.         String algorithm = SystemPropertyUtil.get("ssl.KeyManagerFactory.algorithm");  

61.         if (algorithm == null) {  

62.             algorithm = "SunX509";  

63.         }  

64.   

65.         SSLContext serverContext;  

66.         SSLContext clientContext;  

67.         try {  

68.             KeyStore ks = KeyStore.getInstance("JKS");  

69.             ks.load(SecureChatKeyStore.asInputStream(),  

70.                     SecureChatKeyStore.getKeyStorePassword());  

71.   

72.             // Set up key manager factory to use our key store  

73.             KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);  

74.             kmf.init(ks, SecureChatKeyStore.getCertificatePassword());  

75.   

76.             // Initialize the SSLContext to work with our key managers.  

77.             serverContext = SSLContext.getInstance(PROTOCOL);  

78.             serverContext.init(kmf.getKeyManagers(), nullnull);  

79.         } catch (Exception e) {  

80.             throw new Error(  

81.                     "Failed to initialize the server-side SSLContext", e);  

82.         }  

83.   

84.         try {  

85.             clientContext = SSLContext.getInstance(PROTOCOL);  

86.             clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);  

87.         } catch (Exception e) {  

88.             throw new Error(  

89.                     "Failed to initialize the client-side SSLContext", e);  

90.         }  

91.   

92.         SERVER_CONTEXT = serverContext;  

93.         CLIENT_CONTEXT = clientContext;  

94.     }  

95.   

96.     public static SSLContext getServerContext() {  

97.         return SERVER_CONTEXT;  

98.     }  

99.   

100.     public static SSLContext getClientContext() {  

101.         return CLIENT_CONTEXT;  

102.     }  

103.   

104.     private SecureChatSslContextFactory() {  

105.         // Unused  

106.     }  

107. }  

原创粉丝点击