Netty-Tcp using openssl/boringssl for ssl

Netty-Tcp using openssl/boringssl for ssl

Pom.xml: 若发现找不到对应classifier变量,可以尝试重新编译

 <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty-tcnative-boringssl-static</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-tcnative-boringssl-static</artifactId>
            <version>2.0.20.Final</version>
            <classifier>${os.detected.classifier}</classifier>
        </dependency>
  1. 设定sslProvide属性为sslProvide.OPENSSL
  2. openssl version >= 1.02g
  3. 如果因为openssl升级造成调用openssl library,无法找到library, netty 会自动生成.so到tomcat的tmp目录下,查看tomcatlog时可以看调用生成的xxxxxxx.so successful.
 private boolean initSocketChannel(SocketChannel socketChannel) {
        SslContext sslContext = initSSLContext();
        if (sslContext == null){
            logger.info("sslContext init fail, return.");
            return false;
        }

        SSLEngine sslEngine = sslContext.newEngine(socketChannel.alloc());
        sslEngine.setUseClientMode(false);
        sslEngine.setNeedClientAuth(false);

        SslHandler sslHandler = new SslHandler(sslEngine);
        sslHandler.setHandshakeTimeoutMillis(60 * 1000);

        socketChannel.pipeline().addFirst("ssl", new SslHandler(sslEngine));
        socketChannel.pipeline().addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
        ByteBuf delimiter = Unpooled.copiedBuffer("\r\n".getBytes());
        socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(2048, delimiter));
        socketChannel.pipeline().addLast("decode", new StringDecoder());
        socketChannel.pipeline().addLast("encode", new StringEncoder());
        socketChannel.pipeline().addLast(eventExecutorGroup, new HeartBeatServerHandler());
        socketChannel.pipeline().addLast(idleStateTrigger);
        return true;
    }

    private SslContext initSSLContext() {
        SslContext sslContext = null;
        try {
            final Decoder decoder = Base64.getDecoder();
//            String pass = new String(decoder.decodeBuffer("MTExMTEx"), "UTF-8");
            String pass = new String(decoder.decode("aWRzYmcxMjMu"), "UTF-8");
            KeyStore ks = KeyStore.getInstance(Util.SSL_JKS);
            InputStream ksInputStream = HeartBeatServer.class.getResourceAsStream("/server.jks");
            ks.load(ksInputStream, pass.toCharArray());
            OpenSslCachingX509KeyManagerFactory kmf = new OpenSslCachingX509KeyManagerFactory(KeyManagerFactory.getInstance(Util.SSL_MANAGER_X509));
            kmf.init(ks, pass.toCharArray());
            sslContext = SslContextBuilder.forServer(kmf).sslProvider(SslProvider.OPENSSL).build();
            //SSLContext.getInstance(Util.SSL_VERSION);
            //sslContext.init(kmf.getKeyManagers(), null, null);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (sslContext != null) {
            logger.debug("sslContext初始化成功");
        }
        return sslContext;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值