12.3 Netty中使用SPDY

    **支持SPDY的ChannelPipeline如下图:**

http://img.blog.csdn.net/20140801165349500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJjX2tleQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast

    **不支持SPDY的ChannelPipeline如下图:**

http://img.blog.csdn.net/20140801165718481?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJjX2tleQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast

   ** 例子代码如下:**

[java] view plaincopy

  1. package netty.in.action.spdy;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import org.eclipse.jetty.npn.NextProtoNego.ServerProvider;
  6. public class DefaultServerProvider implements ServerProvider {
  7. private static final List<String> PROTOCOLS = Collections.unmodifiableList(Arrays
  8. .asList("spdy/3.1", "http/1.1", "http/1.0", "Unknown"));
  9. private String protocol;
  10. public String getSelectedProtocol() {
  11. return protocol;
  12. }
  13. @Override
  14. public void protocolSelected(String arg0) {
  15. this.protocol = arg0;
  16. }
  17. @Override
  18. public List<String> protocols() {
  19. return PROTOCOLS;
  20. }
  21. @Override
  22. public void unsupported() {
  23. protocol = "http/1.1";
  24. }
  25. }

[java] view plaincopy

  1. package netty.in.action.spdy;
  2. import io.netty.channel.ChannelFuture;
  3. import io.netty.channel.ChannelFutureListener;
  4. import io.netty.channel.ChannelHandlerContext;
  5. import io.netty.channel.SimpleChannelInboundHandler;
  6. import io.netty.handler.codec.http.DefaultFullHttpResponse;
  7. import io.netty.handler.codec.http.FullHttpRequest;
  8. import io.netty.handler.codec.http.FullHttpResponse;
  9. import io.netty.handler.codec.http.HttpHeaders;
  10. import io.netty.handler.codec.http.HttpResponseStatus;
  11. import io.netty.handler.codec.http.HttpVersion;
  12. import io.netty.util.CharsetUtil;
  13. public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
  14. @Override
  15. protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request)
  16. throws Exception {
  17. if (HttpHeaders.is100ContinueExpected(request)) {
  18. send100Continue(ctx);
  19. }
  20. FullHttpResponse response = new DefaultFullHttpResponse(
  21. request.getProtocolVersion(), HttpResponseStatus.OK);
  22. response.content().writeBytes(getContent().getBytes(CharsetUtil.UTF_8));
  23. response.headers().set(HttpHeaders.Names.CONTENT_TYPE,
  24. "text/plain; charset=UTF-8");
  25. boolean keepAlive = HttpHeaders.isKeepAlive(request);
  26. if (keepAlive) {
  27. response.headers().set(HttpHeaders.Names.CONTENT_LENGTH,
  28. response.content().readableBytes());
  29. response.headers().set(HttpHeaders.Names.CONNECTION,
  30. HttpHeaders.Values.KEEP_ALIVE);
  31. }
  32. ChannelFuture future = ctx.writeAndFlush(response);
  33. if (!keepAlive) {
  34. future.addListener(ChannelFutureListener.CLOSE);
  35. }
  36. }
  37. private static void send100Continue(ChannelHandlerContext ctx) {
  38. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
  39. HttpResponseStatus.CONTINUE);
  40. ctx.writeAndFlush(response);
  41. }
  42. protected String getContent() {
  43. return "This content is transmitted via HTTP\r\n";
  44. }
  45. @Override
  46. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
  47. throws Exception {
  48. cause.printStackTrace();
  49. ctx.close();
  50. }
  51. }

[java] view plaincopy

  1. package netty.in.action.spdy;
  2. public class SpdyRequestHandler extends HttpRequestHandler {
  3. @Override
  4. protected String getContent() {
  5. return "This content is transmitted via SPDY\r\n";
  6. }
  7. }

[java] view plaincopy

  1. package netty.in.action.spdy;
  2. import io.netty.channel.ChannelInboundHandler;
  3. import io.netty.handler.codec.spdy.SpdyOrHttpChooser;
  4. import javax.net.ssl.SSLEngine;
  5. import org.eclipse.jetty.npn.NextProtoNego;
  6. public class DefaultSpdyOrHttpChooser extends SpdyOrHttpChooser {
  7. protected DefaultSpdyOrHttpChooser(int maxSpdyContentLength, int maxHttpContentLength) {
  8. super(maxSpdyContentLength, maxHttpContentLength);
  9. }
  10. @Override
  11. protected SelectedProtocol getProtocol(SSLEngine engine) {
  12. DefaultServerProvider provider = (DefaultServerProvider) NextProtoNego
  13. .get(engine);
  14. String protocol = provider.getSelectedProtocol();
  15. if (protocol == null) {
  16. return SelectedProtocol.UNKNOWN;
  17. }
  18. switch (protocol) {
  19. case "spdy/3.1":
  20. return SelectedProtocol.SPDY_3_1;
  21. case "http/1.0":
  22. case "http/1.1":
  23. return SelectedProtocol.HTTP_1_1;
  24. default:
  25. return SelectedProtocol.UNKNOWN;
  26. }
  27. }
  28. @Override
  29. protected ChannelInboundHandler createHttpRequestHandlerForHttp() {
  30. return new HttpRequestHandler();
  31. }
  32. @Override
  33. protected ChannelInboundHandler createHttpRequestHandlerForSpdy() {
  34. return new SpdyRequestHandler();
  35. }
  36. }

[java] view plaincopy

  1. package netty.in.action.spdy;
  2. import io.netty.channel.Channel;
  3. import io.netty.channel.ChannelInitializer;
  4. import io.netty.channel.ChannelPipeline;
  5. import io.netty.handler.ssl.SslHandler;
  6. import javax.net.ssl.SSLContext;
  7. import javax.net.ssl.SSLEngine;
  8. import org.eclipse.jetty.npn.NextProtoNego;
  9. public class SpdyChannelInitializer extends ChannelInitializer<Channel> {
  10. private final SSLContext context;
  11. public SpdyChannelInitializer(SSLContext context) {
  12. this.context = context;
  13. }
  14. @Override
  15. protected void initChannel(Channel ch) throws Exception {
  16. ChannelPipeline pipeline = ch.pipeline();
  17. SSLEngine engine = context.createSSLEngine();
  18. engine.setUseClientMode(false);
  19. NextProtoNego.put(engine, new DefaultServerProvider());
  20. NextProtoNego.debug = true;
  21. pipeline.addLast("sslHandler", new SslHandler(engine));
  22. pipeline.addLast("chooser",
  23. new DefaultSpdyOrHttpChooser(1024 1024, 1024 1024));
  24. }
  25. }

[java] view plaincopy

  1. package netty.in.action.spdy;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.channel.Channel;
  4. import io.netty.channel.ChannelFuture;
  5. import io.netty.channel.nio.NioEventLoopGroup;
  6. import io.netty.channel.socket.nio.NioServerSocketChannel;
  7. import io.netty.example.securechat.SecureChatSslContextFactory;
  8. import java.net.InetSocketAddress;
  9. import javax.net.ssl.SSLContext;
  10. public class SpdyServer {
  11. private final NioEventLoopGroup group = new NioEventLoopGroup();
  12. private final SSLContext context;
  13. private Channel channel;
  14. public SpdyServer(SSLContext context) {
  15. this.context = context;
  16. }
  17. public ChannelFuture start(InetSocketAddress address) {
  18. ServerBootstrap bootstrap = new ServerBootstrap();
  19. bootstrap.group(group).channel(NioServerSocketChannel.class)
  20. .childHandler(new SpdyChannelInitializer(context));
  21. ChannelFuture future = bootstrap.bind(address);
  22. future.syncUninterruptibly();
  23. channel = future.channel();
  24. return future;
  25. }
  26. public void destroy() {
  27. if (channel != null) {
  28. channel.close();
  29. }
  30. group.shutdownGracefully();
  31. }
  32. public static void main(String[] args) {
  33. SSLContext context = SecureChatSslContextFactory.getServerContext();
  34. final SpdyServer endpoint = new SpdyServer(context);
  35. ChannelFuture future = endpoint.start(new InetSocketAddress(4096));
  36. Runtime.getRuntime().addShutdownHook(new Thread() {
  37. @Override
  38. public void run() {
  39. endpoint.destroy();
  40. }
  41. });
  42. future.channel().closeFuture().syncUninterruptibly();
  43. }
  44. }

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

[java] view plaincopy

  1. /*
    • Copyright 2012 The Netty Project
  2. *
    • The Netty Project licenses this file to you under the Apache License,
    • version 2.0 (the "License"); you may not use this file except in compliance
    • with the License. You may obtain a copy of the License at:
  3. *
  4. *
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    • WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    • License for the specific language governing permissions and limitations
    • under the License.
  5. */
  6. package netty.in.action.spdy;
  7. import javax.net.ssl.ManagerFactoryParameters;
  8. import javax.net.ssl.TrustManager;
  9. import javax.net.ssl.TrustManagerFactorySpi;
  10. import javax.net.ssl.X509TrustManager;
  11. import java.security.InvalidAlgorithmParameterException;
  12. import java.security.KeyStore;
  13. import java.security.KeyStoreException;
  14. import java.security.cert.X509Certificate;
  15. /**
    • Bogus {@link TrustManagerFactorySpi} which accepts any certificate
    • even if it is invalid.
  16. */
  17. public class SecureChatTrustManagerFactory extends TrustManagerFactorySpi {
  18. private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() {
  19. @Override
  20. public X509Certificate[] getAcceptedIssuers() {
  21. return new X509Certificate[0];
  22. }
  23. @Override
  24. public void checkClientTrusted(X509Certificate[] chain, String authType) {
  25. // Always trust - it is an example.
  26. // You should do something in the real world.
  27. // You will reach here only if you enabled client certificate auth,
  28. // as described in SecureChatSslContextFactory.
  29. System.err.println(
  30. "UNKNOWN CLIENT CERTIFICATE: " + chain[0].getSubjectDN());
  31. }
  32. @Override
  33. public void checkServerTrusted(X509Certificate[] chain, String authType) {
  34. // Always trust - it is an example.
  35. // You should do something in the real world.
  36. System.err.println(
  37. "UNKNOWN SERVER CERTIFICATE: " + chain[0].getSubjectDN());
  38. }
  39. };
  40. public static TrustManager[] getTrustManagers() {
  41. return new TrustManager[] { DUMMY_TRUST_MANAGER };
  42. }
  43. @Override
  44. protected TrustManager[] engineGetTrustManagers() {
  45. return getTrustManagers();
  46. }
  47. @Override
  48. protected void engineInit(KeyStore keystore) throws KeyStoreException {
  49. // Unused
  50. }
  51. @Override
  52. protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
  53. throws InvalidAlgorithmParameterException {
  54. // Unused
  55. }
  56. }

[java] view plaincopy

  1. /*
    • Copyright 2012 The Netty Project
  2. *
    • The Netty Project licenses this file to you under the Apache License,
    • version 2.0 (the "License"); you may not use this file except in compliance
    • with the License. You may obtain a copy of the License at:
  3. *
  4. *
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    • WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    • License for the specific language governing permissions and limitations
    • under the License.
  5. */
  6. package netty.in.action.spdy;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.InputStream;
  9. /**
    • A bogus key store which provides all the required information to
    • create an example SSL connection.
  10. *
    • To generate a bogus key store:
    • <pre>
    • keytool -genkey -alias securechat -keysize 2048 -validity 36500
    • -keyalg RSA -dname "CN=securechat"
    • -keypass secret -storepass secret
    • -keystore cert.jks
    • </pre>
  11. */
  12. public final class SecureChatKeyStore {
  13. private static final short[] DATA = {
  14. 0xfe, 0xed, 0xfe, 0xed, 0x00, 0x00, 0x00, 0x02,
  15. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
  16. 0x00, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c,
  17. 0x65, 0x00, 0x00, 0x01, 0x1a, 0x9f, 0x57, 0xa5,
  18. 0x27, 0x00, 0x00, 0x01, 0x9a, 0x30, 0x82, 0x01,
  19. 0x96, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01,
  20. 0x04, 0x01, 0x2a, 0x02, 0x11, 0x01, 0x01, 0x05,
  21. 0x00, 0x04, 0x82, 0x01, 0x82, 0x48, 0x6d, 0xcf,
  22. 0x16, 0xb5, 0x50, 0x95, 0x36, 0xbf, 0x47, 0x27,
  23. 0x50, 0x58, 0x0d, 0xa2, 0x52, 0x7e, 0x25, 0xab,
  24. 0x14, 0x1a, 0x26, 0x5e, 0x2d, 0x8a, 0x23, 0x90,
  25. 0x60, 0x7f, 0x12, 0x20, 0x56, 0xd1, 0x43, 0xa2,
  26. 0x6b, 0x47, 0x5d, 0xed, 0x9d, 0xd4, 0xe5, 0x83,
  27. 0x28, 0x89, 0xc2, 0x16, 0x4c, 0x76, 0x06, 0xad,
  28. 0x8e, 0x8c, 0x29, 0x1a, 0x9b, 0x0f, 0xdd, 0x60,
  29. 0x4b, 0xb4, 0x62, 0x82, 0x9e, 0x4a, 0x63, 0x83,
  30. 0x2e, 0xd2, 0x43, 0x78, 0xc2, 0x32, 0x1f, 0x60,
  31. 0xa9, 0x8a, 0x7f, 0x0f, 0x7c, 0xa6, 0x1d, 0xe6,
  32. 0x92, 0x9e, 0x52, 0xc7, 0x7d, 0xbb, 0x35, 0x3b,
  33. 0xaa, 0x89, 0x73, 0x4c, 0xfb, 0x99, 0x54, 0x97,
  34. 0x99, 0x28, 0x6e, 0x66, 0x5b, 0xf7, 0x9b, 0x7e,
  35. 0x6d, 0x8a, 0x2f, 0xfa, 0xc3, 0x1e, 0x71, 0xb9,
  36. 0xbd, 0x8f, 0xc5, 0x63, 0x25, 0x31, 0x20, 0x02,
  37. 0xff, 0x02, 0xf0, 0xc9, 0x2c, 0xdd, 0x3a, 0x10,
  38. 0x30, 0xab, 0xe5, 0xad, 0x3d, 0x1a, 0x82, 0x77,
  39. 0x46, 0xed, 0x03, 0x38, 0xa4, 0x73, 0x6d, 0x36,
  40. 0x36, 0x33, 0x70, 0xb2, 0x63, 0x20, 0xca, 0x03,
  41. 0xbf, 0x5a, 0xf4, 0x7c, 0x35, 0xf0, 0x63, 0x1a,
  42. 0x12, 0x33, 0x12, 0x58, 0xd9, 0xa2, 0x63, 0x6b,
  43. 0x63, 0x82, 0x41, 0x65, 0x70, 0x37, 0x4b, 0x99,
  44. 0x04, 0x9f, 0xdd, 0x5e, 0x07, 0x01, 0x95, 0x9f,
  45. 0x36, 0xe8, 0xc3, 0x66, 0x2a, 0x21, 0x69, 0x68,
  46. 0x40, 0xe6, 0xbc, 0xbb, 0x85, 0x81, 0x21, 0x13,
  47. 0xe6, 0xa4, 0xcf, 0xd3, 0x67, 0xe3, 0xfd, 0x75,
  48. 0xf0, 0xdf, 0x83, 0xe0, 0xc5, 0x36, 0x09, 0xac,
  49. 0x1b, 0xd4, 0xf7, 0x2a, 0x23, 0x57, 0x1c, 0x5c,
  50. 0x0f, 0xf4, 0xcf, 0xa2, 0xcf, 0xf5, 0xbd, 0x9c,
  51. 0x69, 0x98, 0x78, 0x3a, 0x25, 0xe4, 0xfd, 0x85,
  52. 0x11, 0xcc, 0x7d, 0xef, 0xeb, 0x74, 0x60, 0xb1,
  53. 0xb7, 0xfb, 0x1f, 0x0e, 0x62, 0xff, 0xfe, 0x09,
  54. 0x0a, 0xc3, 0x80, 0x2f, 0x10, 0x49, 0x89, 0x78,
  55. 0xd2, 0x08, 0xfa, 0x89, 0x22, 0x45, 0x91, 0x21,
  56. 0xbc, 0x90, 0x3e, 0xad, 0xb3, 0x0a, 0xb4, 0x0e,
  57. 0x1c, 0xa1, 0x93, 0x92, 0xd8, 0x72, 0x07, 0x54,
  58. 0x60, 0xe7, 0x91, 0xfc, 0xd9, 0x3c, 0xe1, 0x6f,
  59. 0x08, 0xe4, 0x56, 0xf6, 0x0b, 0xb0, 0x3c, 0x39,
  60. 0x8a, 0x2d, 0x48, 0x44, 0x28, 0x13, 0xca, 0xe9,
  61. 0xf7, 0xa3, 0xb6, 0x8a, 0x5f, 0x31, 0xa9, 0x72,
  62. 0xf2, 0xde, 0x96, 0xf2, 0xb1, 0x53, 0xb1, 0x3e,
  63. 0x24, 0x57, 0xfd, 0x18, 0x45, 0x1f, 0xc5, 0x33,
  64. 0x1b, 0xa4, 0xe8, 0x21, 0xfa, 0x0e, 0xb2, 0xb9,
  65. 0xcb, 0xc7, 0x07, 0x41, 0xdd, 0x2f, 0xb6, 0x6a,
  66. 0x23, 0x18, 0xed, 0xc1, 0xef, 0xe2, 0x4b, 0xec,
  67. 0xc9, 0xba, 0xfb, 0x46, 0x43, 0x90, 0xd7, 0xb5,
  68. 0x68, 0x28, 0x31, 0x2b, 0x8d, 0xa8, 0x51, 0x63,
  69. 0xf7, 0x53, 0x99, 0x19, 0x68, 0x85, 0x66, 0x00,
  70. 0x00, 0x00, 0x01, 0x00, 0x05, 0x58, 0x2e, 0x35,
  71. 0x30, 0x39, 0x00, 0x00, 0x02, 0x3a, 0x30, 0x82,
  72. 0x02, 0x36, 0x30, 0x82, 0x01, 0xe0, 0xa0, 0x03,
  73. 0x02, 0x01, 0x02, 0x02, 0x04, 0x48, 0x59, 0xf1,
  74. 0x92, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  75. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
  76. 0x30, 0x81, 0xa0, 0x31, 0x0b, 0x30, 0x09, 0x06,
  77. 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x4b, 0x52,
  78. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  79. 0x08, 0x13, 0x0a, 0x4b, 0x79, 0x75, 0x6e, 0x67,
  80. 0x67, 0x69, 0x2d, 0x64, 0x6f, 0x31, 0x14, 0x30,
  81. 0x12, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x0b,
  82. 0x53, 0x65, 0x6f, 0x6e, 0x67, 0x6e, 0x61, 0x6d,
  83. 0x2d, 0x73, 0x69, 0x31, 0x1a, 0x30, 0x18, 0x06,
  84. 0x03, 0x55, 0x04, 0x0a, 0x13, 0x11, 0x54, 0x68,
  85. 0x65, 0x20, 0x4e, 0x65, 0x74, 0x74, 0x79, 0x20,
  86. 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x31,
  87. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0b,
  88. 0x13, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c,
  89. 0x65, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72,
  90. 0x73, 0x31, 0x30, 0x30, 0x2e, 0x06, 0x03, 0x55,
  91. 0x04, 0x03, 0x13, 0x27, 0x73, 0x65, 0x63, 0x75,
  92. 0x72, 0x65, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x65,
  93. 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e,
  94. 0x65, 0x74, 0x74, 0x79, 0x2e, 0x67, 0x6c, 0x65,
  95. 0x61, 0x6d, 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
  96. 0x6e, 0x65, 0x74, 0x30, 0x20, 0x17, 0x0d, 0x30,
  97. 0x38, 0x30, 0x36, 0x31, 0x39, 0x30, 0x35, 0x34,
  98. 0x31, 0x33, 0x38, 0x5a, 0x18, 0x0f, 0x32, 0x31,
  99. 0x38, 0x37, 0x31, 0x31, 0x32, 0x34, 0x30, 0x35,
  100. 0x34, 0x31, 0x33, 0x38, 0x5a, 0x30, 0x81, 0xa0,
  101. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  102. 0x06, 0x13, 0x02, 0x4b, 0x52, 0x31, 0x13, 0x30,
  103. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a,
  104. 0x4b, 0x79, 0x75, 0x6e, 0x67, 0x67, 0x69, 0x2d,
  105. 0x64, 0x6f, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  106. 0x55, 0x04, 0x07, 0x13, 0x0b, 0x53, 0x65, 0x6f,
  107. 0x6e, 0x67, 0x6e, 0x61, 0x6d, 0x2d, 0x73, 0x69,
  108. 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04,
  109. 0x0a, 0x13, 0x11, 0x54, 0x68, 0x65, 0x20, 0x4e,
  110. 0x65, 0x74, 0x74, 0x79, 0x20, 0x50, 0x72, 0x6f,
  111. 0x6a, 0x65, 0x63, 0x74, 0x31, 0x18, 0x30, 0x16,
  112. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0f, 0x45,
  113. 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x41,
  114. 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0x31, 0x30,
  115. 0x30, 0x2e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
  116. 0x27, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x63,
  117. 0x68, 0x61, 0x74, 0x2e, 0x65, 0x78, 0x61, 0x6d,
  118. 0x70, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x74,
  119. 0x79, 0x2e, 0x67, 0x6c, 0x65, 0x61, 0x6d, 0x79,
  120. 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x6e, 0x65, 0x74,
  121. 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  122. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  123. 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41,
  124. 0x00, 0xc3, 0xe3, 0x5e, 0x41, 0xa7, 0x87, 0x11,
  125. 0x00, 0x42, 0x2a, 0xb0, 0x4b, 0xed, 0xb2, 0xe0,
  126. 0x23, 0xdb, 0xb1, 0x3d, 0x58, 0x97, 0x35, 0x60,
  127. 0x0b, 0x82, 0x59, 0xd3, 0x00, 0xea, 0xd4, 0x61,
  128. 0xb8, 0x79, 0x3f, 0xb6, 0x3c, 0x12, 0x05, 0x93,
  129. 0x2e, 0x9a, 0x59, 0x68, 0x14, 0x77, 0x3a, 0xc8,
  130. 0x50, 0x25, 0x57, 0xa4, 0x49, 0x18, 0x63, 0x41,
  131. 0xf0, 0x2d, 0x28, 0xec, 0x06, 0xfb, 0xb4, 0x9f,
  132. 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d,
  133. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  134. 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41, 0x00,
  135. 0x65, 0x6c, 0x30, 0x01, 0xc2, 0x8e, 0x3e, 0xcb,
  136. 0xb3, 0x77, 0x48, 0xe9, 0x66, 0x61, 0x9a, 0x40,
  137. 0x86, 0xaf, 0xf6, 0x03, 0xeb, 0xba, 0x6a, 0xf2,
  138. 0xfd, 0xe2, 0xaf, 0x36, 0x5e, 0x7b, 0xaa, 0x22,
  139. 0x04, 0xdd, 0x2c, 0x20, 0xc4, 0xfc, 0xdd, 0xd0,
  140. 0x82, 0x20, 0x1c, 0x3d, 0xd7, 0x9e, 0x5e, 0x5c,
  141. 0x92, 0x5a, 0x76, 0x71, 0x28, 0xf5, 0x07, 0x7d,
  142. 0xa2, 0x81, 0xba, 0x77, 0x9f, 0x2a, 0xd9, 0x44,
  143. 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x6d, 0x79,
  144. 0x6b, 0x65, 0x79, 0x00, 0x00, 0x01, 0x1a, 0x9f,
  145. 0x5b, 0x56, 0xa0, 0x00, 0x00, 0x01, 0x99, 0x30,
  146. 0x82, 0x01, 0x95, 0x30, 0x0e, 0x06, 0x0a, 0x2b,
  147. 0x06, 0x01, 0x04, 0x01, 0x2a, 0x02, 0x11, 0x01,
  148. 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, 0x81, 0x29,
  149. 0xa8, 0xb6, 0x08, 0x0c, 0x85, 0x75, 0x3e, 0xdd,
  150. 0xb5, 0xe5, 0x1a, 0x87, 0x68, 0xd1, 0x90, 0x4b,
  151. 0x29, 0x31, 0xee, 0x90, 0xbc, 0x9d, 0x73, 0xa0,
  152. 0x3f, 0xe9, 0x0b, 0xa4, 0xef, 0x30, 0x9b, 0x36,
  153. 0x9a, 0xb2, 0x54, 0x77, 0x81, 0x07, 0x4b, 0xaa,
  154. 0xa5, 0x77, 0x98, 0xe1, 0xeb, 0xb5, 0x7c, 0x4e,
  155. 0x48, 0xd5, 0x08, 0xfc, 0x2c, 0x36, 0xe2, 0x65,
  156. 0x03, 0xac, 0xe5, 0xf3, 0x96, 0xb7, 0xd0, 0xb5,
  157. 0x3b, 0x92, 0xe4, 0x14, 0x05, 0x7a, 0x6a, 0x92,
  158. 0x56, 0xfe, 0x4e, 0xab, 0xd3, 0x0e, 0x32, 0x04,
  159. 0x22, 0x22, 0x74, 0x47, 0x7d, 0xec, 0x21, 0x99,
  160. 0x30, 0x31, 0x64, 0x46, 0x64, 0x9b, 0xc7, 0x13,
  161. 0xbf, 0xbe, 0xd0, 0x31, 0x49, 0xe7, 0x3c, 0xbf,
  162. 0xba, 0xb1, 0x20, 0xf9, 0x42, 0xf4, 0xa9, 0xa9,
  163. 0xe5, 0x13, 0x65, 0x32, 0xbf, 0x7c, 0xcc, 0x91,
  164. 0xd3, 0xfd, 0x24, 0x47, 0x0b, 0xe5, 0x53, 0xad,
  165. 0x50, 0x30, 0x56, 0xd1, 0xfa, 0x9c, 0x37, 0xa8,
  166. 0xc1, 0xce, 0xf6, 0x0b, 0x18, 0xaa, 0x7c, 0xab,
  167. 0xbd, 0x1f, 0xdf, 0xe4, 0x80, 0xb8, 0xa7, 0xe0,
  168. 0xad, 0x7d, 0x50, 0x74, 0xf1, 0x98, 0x78, 0xbc,
  169. 0x58, 0xb9, 0xc2, 0x52, 0xbe, 0xd2, 0x5b, 0x81,
  170. 0x94, 0x83, 0x8f, 0xb9, 0x4c, 0xee, 0x01, 0x2b,
  171. 0x5e, 0xc9, 0x6e, 0x9b, 0xf5, 0x63, 0x69, 0xe4,
  172. 0xd8, 0x0b, 0x47, 0xd8, 0xfd, 0xd8, 0xe0, 0xed,
  173. 0xa8, 0x27, 0x03, 0x74, 0x1e, 0x5d, 0x32, 0xe6,
  174. 0x5c, 0x63, 0xc2, 0xfb, 0x3f, 0xee, 0xb4, 0x13,
  175. 0xc6, 0x0e, 0x6e, 0x74, 0xe0, 0x22, 0xac, 0xce,
  176. 0x79, 0xf9, 0x43, 0x68, 0xc1, 0x03, 0x74, 0x2b,
  177. 0xe1, 0x18, 0xf8, 0x7f, 0x76, 0x9a, 0xea, 0x82,
  178. 0x3f, 0xc2, 0xa6, 0xa7, 0x4c, 0xfe, 0xae, 0x29,
  179. 0x3b, 0xc1, 0x10, 0x7c, 0xd5, 0x77, 0x17, 0x79,
  180. 0x5f, 0xcb, 0xad, 0x1f, 0xd8, 0xa1, 0xfd, 0x90,
  181. 0xe1, 0x6b, 0xb2, 0xef, 0xb9, 0x41, 0x26, 0xa4,
  182. 0x0b, 0x4f, 0xc6, 0x83, 0x05, 0x6f, 0xf0, 0x64,
  183. 0x40, 0xe1, 0x44, 0xc4, 0xf9, 0x40, 0x2b, 0x3b,
  184. 0x40, 0xdb, 0xaf, 0x35, 0xa4, 0x9b, 0x9f, 0xc4,
  185. 0x74, 0x07, 0xe5, 0x18, 0x60, 0xc5, 0xfe, 0x15,
  186. 0x0e, 0x3a, 0x25, 0x2a, 0x11, 0xee, 0x78, 0x2f,
  187. 0xb8, 0xd1, 0x6e, 0x4e, 0x3c, 0x0a, 0xb5, 0xb9,
  188. 0x40, 0x86, 0x27, 0x6d, 0x8f, 0x53, 0xb7, 0x77,
  189. 0x36, 0xec, 0x5d, 0xed, 0x32, 0x40, 0x43, 0x82,
  190. 0xc3, 0x52, 0x58, 0xc4, 0x26, 0x39, 0xf3, 0xb3,
  191. 0xad, 0x58, 0xab, 0xb7, 0xf7, 0x8e, 0x0e, 0xba,
  192. 0x8e, 0x78, 0x9d, 0xbf, 0x58, 0x34, 0xbd, 0x77,
  193. 0x73, 0xa6, 0x50, 0x55, 0x00, 0x60, 0x26, 0xbf,
  194. 0x6d, 0xb4, 0x98, 0x8a, 0x18, 0x83, 0x89, 0xf8,
  195. 0xcd, 0x0d, 0x49, 0x06, 0xae, 0x51, 0x6e, 0xaf,
  196. 0xbd, 0xe2, 0x07, 0x13, 0xd8, 0x64, 0xcc, 0xbf,
  197. 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x58, 0x2e,
  198. 0x35, 0x30, 0x39, 0x00, 0x00, 0x02, 0x34, 0x30,
  199. 0x82, 0x02, 0x30, 0x30, 0x82, 0x01, 0xda, 0xa0,
  200. 0x03, 0x02, 0x01, 0x02, 0x02, 0x04, 0x48, 0x59,
  201. 0xf2, 0x84, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  202. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05,
  203. 0x00, 0x30, 0x81, 0x9d, 0x31, 0x0b, 0x30, 0x09,
  204. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x4b,
  205. 0x52, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  206. 0x04, 0x08, 0x13, 0x0a, 0x4b, 0x79, 0x75, 0x6e,
  207. 0x67, 0x67, 0x69, 0x2d, 0x64, 0x6f, 0x31, 0x14,
  208. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13,
  209. 0x0b, 0x53, 0x65, 0x6f, 0x6e, 0x67, 0x6e, 0x61,
  210. 0x6d, 0x2d, 0x73, 0x69, 0x31, 0x1a, 0x30, 0x18,
  211. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x11, 0x54,
  212. 0x68, 0x65, 0x20, 0x4e, 0x65, 0x74, 0x74, 0x79,
  213. 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74,
  214. 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04,
  215. 0x0b, 0x13, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x72,
  216. 0x69, 0x62, 0x75, 0x74, 0x6f, 0x72, 0x73, 0x31,
  217. 0x30, 0x30, 0x2e, 0x06, 0x03, 0x55, 0x04, 0x03,
  218. 0x13, 0x27, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65,
  219. 0x63, 0x68, 0x61, 0x74, 0x2e, 0x65, 0x78, 0x61,
  220. 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74,
  221. 0x74, 0x79, 0x2e, 0x67, 0x6c, 0x65, 0x61, 0x6d,
  222. 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x6e, 0x65,
  223. 0x74, 0x30, 0x20, 0x17, 0x0d, 0x30, 0x38, 0x30,
  224. 0x36, 0x31, 0x39, 0x30, 0x35, 0x34, 0x35, 0x34,
  225. 0x30, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x38, 0x37,
  226. 0x31, 0x31, 0x32, 0x33, 0x30, 0x35, 0x34, 0x35,
  227. 0x34, 0x30, 0x5a, 0x30, 0x81, 0x9d, 0x31, 0x0b,
  228. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  229. 0x02, 0x4b, 0x52, 0x31, 0x13, 0x30, 0x11, 0x06,
  230. 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x4b, 0x79,
  231. 0x75, 0x6e, 0x67, 0x67, 0x69, 0x2d, 0x64, 0x6f,
  232. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04,
  233. 0x07, 0x13, 0x0b, 0x53, 0x65, 0x6f, 0x6e, 0x67,
  234. 0x6e, 0x61, 0x6d, 0x2d, 0x73, 0x69, 0x31, 0x1a,
  235. 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
  236. 0x11, 0x54, 0x68, 0x65, 0x20, 0x4e, 0x65, 0x74,
  237. 0x74, 0x79, 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65,
  238. 0x63, 0x74, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03,
  239. 0x55, 0x04, 0x0b, 0x13, 0x0c, 0x43, 0x6f, 0x6e,
  240. 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x6f, 0x72,
  241. 0x73, 0x31, 0x30, 0x30, 0x2e, 0x06, 0x03, 0x55,
  242. 0x04, 0x03, 0x13, 0x27, 0x73, 0x65, 0x63, 0x75,
  243. 0x72, 0x65, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x65,
  244. 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e,
  245. 0x65, 0x74, 0x74, 0x79, 0x2e, 0x67, 0x6c, 0x65,
  246. 0x61, 0x6d, 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
  247. 0x6e, 0x65, 0x74, 0x30, 0x5c, 0x30, 0x0d, 0x06,
  248. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
  249. 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30,
  250. 0x48, 0x02, 0x41, 0x00, 0x95, 0xb3, 0x47, 0x17,
  251. 0x95, 0x0f, 0x57, 0xcf, 0x66, 0x72, 0x0a, 0x7e,
  252. 0x5b, 0x54, 0xea, 0x8c, 0x6f, 0x79, 0xde, 0x94,
  253. 0xac, 0x0b, 0x5a, 0xd4, 0xd6, 0x1b, 0x58, 0x12,
  254. 0x1a, 0x16, 0x3d, 0xfe, 0xdf, 0xa5, 0x2b, 0x86,
  255. 0xbc, 0x64, 0xd4, 0x80, 0x1e, 0x3f, 0xf9, 0xe2,
  256. 0x04, 0x03, 0x79, 0x9b, 0xc1, 0x5c, 0xf0, 0xf1,
  257. 0xf3, 0xf1, 0xe3, 0xbf, 0x3f, 0xc0, 0x1f, 0xdd,
  258. 0xdb, 0xc0, 0x5b, 0x21, 0x02, 0x03, 0x01, 0x00,
  259. 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  260. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
  261. 0x03, 0x41, 0x00, 0x02, 0xd7, 0xdd, 0xbd, 0x0c,
  262. 0x8e, 0x21, 0x20, 0xef, 0x9e, 0x4f, 0x1f, 0xf5,
  263. 0x49, 0xf1, 0xae, 0x58, 0x9b, 0x94, 0x3a, 0x1f,
  264. 0x70, 0x33, 0xf0, 0x9b, 0xbb, 0xe9, 0xc0, 0xf3,
  265. 0x72, 0xcb, 0xde, 0xb6, 0x56, 0x72, 0xcc, 0x1c,
  266. 0xf0, 0xd6, 0x5a, 0x2a, 0xbc, 0xa1, 0x7e, 0x23,
  267. 0x83, 0xe9, 0xe7, 0xcf, 0x9e, 0xa5, 0xf9, 0xcc,
  268. 0xc2, 0x61, 0xf4, 0xdb, 0x40, 0x93, 0x1d, 0x63,
  269. 0x8a, 0x50, 0x4c, 0x11, 0x39, 0xb1, 0x91, 0xc1,
  270. 0xe6, 0x9d, 0xd9, 0x1a, 0x62, 0x1b, 0xb8, 0xd3,
  271. 0xd6, 0x9a, 0x6d, 0xb9, 0x8e, 0x15, 0x51 };
  272. public static InputStream asInputStream() {
  273. byte[] data = new byte[DATA.length];
  274. for (int i = 0; i < data.length; i ++) {
  275. data[i] = (byte) DATA[i];
  276. }
  277. return new ByteArrayInputStream(data);
  278. }
  279. public static char[] getCertificatePassword() {
  280. return "secret".toCharArray();
  281. }
  282. public static char[] getKeyStorePassword() {
  283. return "secret".toCharArray();
  284. }
  285. private SecureChatKeyStore() {
  286. // Unused
  287. }
  288. }

[java] view plaincopy

  1. /*
    • Copyright 2012 The Netty Project
  2. *
    • The Netty Project licenses this file to you under the Apache License,
    • version 2.0 (the "License"); you may not use this file except in compliance
    • with the License. You may obtain a copy of the License at:
  3. *
  4. *
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    • WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    • License for the specific language governing permissions and limitations
    • under the License.
  5. */
  6. package netty.in.action.spdy;
  7. import io.netty.handler.ssl.SslHandler;
  8. import io.netty.util.internal.SystemPropertyUtil;
  9. import java.security.KeyStore;
  10. import java.security.SecureRandom;
  11. import javax.net.ssl.KeyManager;
  12. import javax.net.ssl.KeyManagerFactory;
  13. import javax.net.ssl.SSLContext;
  14. import javax.net.ssl.SSLEngine;
  15. import javax.net.ssl.TrustManager;
  16. /**
    • Creates a bogus {@link SSLContext}. A client-side context created by this
    • factory accepts any certificate even if it is invalid. A server-side context
    • created by this factory sends a bogus certificate defined in {@link SecureChatKeyStore}.
    • <p>
    • You will have to create your context differently in a real world application.
  17. *
    • <h3>Client Certificate Authentication</h3>
  18. *
    • To enable client certificate authentication:
    • <ul>
    • <li>Enable client authentication on the server side by calling
    • {@link SSLEngine#setNeedClientAuth(boolean)} before creating
    • {@link SslHandler}.</li>
    • <li>When initializing an {@link SSLContext} on the client side,
    • specify the {@link KeyManager} that contains the client certificate as
    • the first argument of {@link SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}.</li>
    • <li>When initializing an {@link SSLContext} on the server side,
    • specify the proper {@link TrustManager} as the second argument of
    • {@link SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}
    • to validate the client certificate.</li>
    • </ul>
  19. */
  20. public final class SecureChatSslContextFactory {
  21. private static final String PROTOCOL = "TLS";
  22. private static final SSLContext SERVER_CONTEXT;
  23. private static final SSLContext CLIENT_CONTEXT;
  24. static {
  25. String algorithm = SystemPropertyUtil.get("ssl.KeyManagerFactory.algorithm");
  26. if (algorithm == null) {
  27. algorithm = "SunX509";
  28. }
  29. SSLContext serverContext;
  30. SSLContext clientContext;
  31. try {
  32. KeyStore ks = KeyStore.getInstance("JKS");
  33. ks.load(SecureChatKeyStore.asInputStream(),
  34. SecureChatKeyStore.getKeyStorePassword());
  35. // Set up key manager factory to use our key store
  36. KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
  37. kmf.init(ks, SecureChatKeyStore.getCertificatePassword());
  38. // Initialize the SSLContext to work with our key managers.
  39. serverContext = SSLContext.getInstance(PROTOCOL);
  40. serverContext.init(kmf.getKeyManagers(), null, null);
  41. } catch (Exception e) {
  42. throw new Error(
  43. "Failed to initialize the server-side SSLContext", e);
  44. }
  45. try {
  46. clientContext = SSLContext.getInstance(PROTOCOL);
  47. clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);
  48. } catch (Exception e) {
  49. throw new Error(
  50. "Failed to initialize the client-side SSLContext", e);
  51. }
  52. SERVER_CONTEXT = serverContext;
  53. CLIENT_CONTEXT = clientContext;
  54. }
  55. public static SSLContext getServerContext() {
  56. return SERVER_CONTEXT;
  57. }
  58. public static SSLContext getClientContext() {
  59. return CLIENT_CONTEXT;
  60. }
  61. private SecureChatSslContextFactory() {
  62. // Unused
  63. }
  64. }

results matching ""

    No results matching ""