`

[简略记录]Netty+SSL+Protobuf的配置

阅读更多
好吧,今天继续简略的记录一下。记记几周之前写的一小段代码,关于在Netty中同时使用SSL和Google Protobuf的配置。

首先是Netty的initChannel相关代码:
public void initChannel(SocketChannel ch) throws Exception {
 ChannelPipeline p = ch.pipeline();
 
 // SSL相关,可以参考netty example中的io.netty.example.securechat.*的代码
 p.addLast("ssl", new SslHandler(YourServerSslContextFactory.createSSLEngine()));
 
 // Protobuf相关,可以参考netty example中的io.netty.example.worldclock.*的代码
 ExtensionRegistry registry = ExtensionRegistry.newInstance(); 
 YourProtobufClass.registerAllExtensions(registry);
 p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
 p.addLast("protobufDecoder", new ProtobufDecoder(YourProtobufClass.YourClass.getDefaultInstance(), registry));
 p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
 p.addLast("protobufEncoder", new ProtobufEncoder());
 
 p.addLast(new LoggingHandler(LogLevel.INFO));
 p.addLast(handler);
}


顺便提提用java的keytool来生成SSL证书的命令:
keytool -genkey -alias YOUR_KEY_NAME -keysize 2048 -validity 365  -keyalg RSA -dname "CN=127.0.0.1,OU=YOUR_KEY_NAME" -keypass YOUR_KEY_PASSWORD -storepass changeit -keystore YOUR_FILE_NAME.jks

这里的-storepass changeit,因为jdk默认的密码是changeit,我的jdk没做过修改,还是默认的。

上面有很多YourClassXX或YOUR_XX之类的,表示需要根据自己的情况来做修改的~

发布在:http://auzll.iteye.com
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics