netty SSL尝试

1. 5.0.0.Alpha2  
    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>5.0.0.Alpha2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.bcel</groupId>
            <artifactId>bcel</artifactId>
            <version>6.2</version>
        </dependency>

        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-run</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-extras</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-bind</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-schema</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-tools</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.stream</groupId>
            <artifactId>stax-api</artifactId>
            <version>1.0-2</version>
        </dependency>


        <dependency>
            <groupId>org.ogce</groupId>
            <artifactId>xpp3</artifactId>
            <version>1.1.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
    </dependencies>

2. serverInitializer

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // Add SSL handler first to encrypt and decrypt everything.
        // In this example, we use a bogus certificate in the server side
        // and accept any invalid certificates in the client side.
        // You will need something more complicated to identify both
        // and server in the real world.
        //
        // Read SecureChatSslContextFactory
        // if you need client certificate authentication.

        SSLEngine engine = null;
        switch (SSLMODE.valueOf(tlsMode)) {
        case CA:
            URL url = this.getClass().getClassLoader().getResource("sslconf/client/sChat.jks");
            if (null == url) {
                log.error("ca jks file does not exists !!!", new Throwable());
            }
            log.info("SSL Server initializing ,wait");
            engine = SecureChatSslContextFactory.getServerContext(tlsMode, url.getFile(), null).createSSLEngine();
            if(null==engine ) {
                log.error("SSL Server create faild, please check your configs...",new Throwable());
            }
            log.info("SSL Server initialized ,OK");
            break;
        case CSA:
            URL pkUrl = this.getClass().getClassLoader().getResource("sslconf/twoway/sChat.jks");
            URL caUrl = this.getClass().getClassLoader().getResource("sslconf/twoway/sChat.jks");
            engine = SecureChatSslContextFactory.getServerContext(tlsMode, pkUrl.getFile(), caUrl.getFile())
                    .createSSLEngine();
            // Client auth
            engine.setNeedClientAuth(true);
            break;
        default:
            log.error("ERROR : " + tlsMode);
            System.exit(-1);
            break;

        }
        engine.setUseClientMode(false);

        pipeline.addLast("ssl", new SslHandler(engine));

        // On top of the SSL handler, add the text line codec.
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());

        // and then business logic.
        pipeline.addLast("handler", new SecureChatServerHandler());
    }

3. clientInitializer

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // Add SSL handler first to encrypt and decrypt everything.
        // In this example, we use a bogus certificate in the server side
        // and accept any invalid certificates in the client side.
        // You will need something more complicated to identify both
        // and server in the real world.

        SSLEngine engine = null;
        switch (SSLMODE.valueOf(tlsMode)) {
        case CA:
            URL url = this.getClass().getClassLoader().getResource("sslconf/client/cChat.jks");
            if (null == url) {
                log.error("ca jks file does not exists !!!", new Throwable());
            }
            engine = SecureChatSslContextFactory.getClientContext(tlsMode, null, url.getFile()).createSSLEngine();
            break;
        case CSA:
            URL pkUrl = this.getClass().getClassLoader().getResource("sslconf/twoway/cChat.jks");
            URL caUrl = this.getClass().getClassLoader().getResource("sslconf/twoway/cChat.jks");
            engine = SecureChatSslContextFactory.getClientContext(tlsMode, pkUrl.getFile(), caUrl.getFile()).createSSLEngine();
            break;
        default:
            log.error("ERROR : " + tlsMode);
            System.exit(-1);
            break;

        }

        engine.setUseClientMode(true);
        pipeline.addLast("ssl", new SslHandler(engine));

        // On top of the SSL handler, add the text line codec.
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());

        // and then business logic.
        pipeline.addLast("handler", new SecureChatClientHandler());
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值