【发布时间】:2014-09-15 21:01:09
【问题描述】:
我正在努力解决 Netty 5.0.0Alpha1 的问题。目前我正在升级我们的一个 API 以使用 SSL。当我按照示例中所示设置所有内容时,服务器会处理一个请求并崩溃。基本上,我能够在 Firefox 上获得不受信任的证书警告,并且服务器会在每个后续请求中重置连接。并且日志中没有更多有用的信息。我已将 io.netty 设置为 DEBUG 级别。
这是我的服务器初始化的代码示例:
引导程序:
String cfgKsLocation = ; // .....
String cfgKsPassword = ; // .....
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(new FileInputStream(new File(cfgKsLocation)), cfgKsPassword.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keystore, cfgKsPassword.toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), null, null);
this.sslEngine = sslContext.createSSLEngine();
this.sslEngine.setUseClientMode(false);
EventLoopGroup nettyBossGroup = new NioEventLoopGroup(1);
EventLoopGroup nettyWorkerGroup = new NioEventLoopGroup(2);
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(nettyBossGroup, nettyWorkerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ApiChannelInitializer());
bootstrap.option(ChannelOption.SO_BACKLOG, cfgServerBacklog);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
Channel channel = bootstrap.bind(cfgServerIpAddress, cfgServerPort).sync().channel();
channel.closeFuture().sync();
} finally {
nettyBossGroup.shutdownGracefully();
nettyWorkerGroup.shutdownGracefully();
}
ApiChannelInitializer:
private class ApiChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel c) throws Exception {
c.pipeline().addLast(new SslHandler(ApiServer.this.sslEngine));
c.pipeline().addLast(new HttpRequestDecoder());
c.pipeline().addLast(new HttpResponseEncoder());
c.pipeline().addLast(new HttpContentCompressor());
c.pipeline().addLast(new ApiChannelInboundHandler());
}
}
到目前为止我所做的尝试:
- 从 EventLoopGroup 构造中移除参数
- 使用与 Java SecureSocketServer 配合良好的不同证书
- 更改为 netty 版本 4.0.X
- 在 SslHandler 之后将 DelimiterBasedFrameDecoder 添加到管道
【问题讨论】: