java - Netty 5.0.0Alpha1 SSLHandler 在一个请求后崩溃

标签 java ssl netty

我正在努力解决 Netty 5.0.0Alpha1 的问题。目前我正在努力升级我们的一个 API 以使用 SSL。当我按照示例中所示设置所有内容时,服务器处理一个请求并崩溃。基本上,我能够在 firefox 上收到不受信任的证书警告,并且服务器会在每个后续请求上重置连接。并且日志中没有更多有用的信息。我已将 io.netty 设置为 DEBUG 级别。

以下是我的服务器初始化的代码示例:

Bootstrap :

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 添加到管道

最佳答案

经过几个小时的努力,终于解决了我的问题。问题是 ssl 警报使 SSLHandler 中的 SSLEngine 崩溃。如果只有一个 SSLHandler 附加在管道的开头并且它的 SSLEngine 崩溃,则 SSLHandler 不再能够处理 SSL/TLS 连接。

因此我已将此覆盖添加到我的入站 channel 处理程序中:

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        SSLEngine sslEngine = ApiServer.this.sslContext.createSSLEngine();
        sslEngine.setNeedClientAuth(false);
        sslEngine.setUseClientMode(false);
        SslHandler sslHandler = new SslHandler(sslEngine);
        ctx.pipeline().addFirst(sslHandler);
    }

关于java - Netty 5.0.0Alpha1 SSLHandler 在一个请求后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24931463/

相关文章:

java - 初级 Java Applet — 无法让 Swing 计时器运行

java - 如何获取 Netty 4 应用程序的直接内存快照

session - Netty https ( TLS ) session 持续时间 : why is renegotiation needed?

java - 如何将 File 对象转换为 Netty ByteBuffer

Java (Bukkit) - 在执行下一行之前等待

java - 电报机器人 java : inline url button does not create an update and thus no callback data to respond on

java - OpenNLP 保存训练好的模型

ssl - 为 IIS7 使用 BAT 文件注册 SSL 绑定(bind)

apache - 由于 SSLProtocol : Illegal protocol 'TLSv1.1' error,httpd 未启动

python - 在Python中获取或构建PEM证书链