国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Netty使用JSSE實現SSLSocket通信

DTeam / 1068人閱讀

摘要:上文講了如何使用生成的簽名證書進行加密通信,結果客戶端告訴我他們用的版本沒有類,并且由于一些交易的原因還不能更新沒有你總有吧,來吧。

上文講了netty如何使用openssl生成的簽名證書進行加密通信,結果客戶端告訴我他們用的netty版本沒有SslContextBuilder類,并且由于一些PY交易的原因還不能更新netty....

SslContextBuilder沒有java你總有吧,來吧。

一、老規矩,創建key 1.創建客戶端和服務端的keystore文件

隨便找個文件夾就行,打開命令行輸入

keytool -genkey -alias sslserver -keystore sslserverkeys
keytool -genkey -alias sslclient -keystore sslclientkeys
2.將keystore導出證書格式
keytool -export -alias sslserver -keystore sslserverkeys -file sslserver.cer
keytool -export -alias sslclient -keystore sslclientkeys -file sslclient.cer
3.將客戶端證書導入到服務器端信任的 keystore 里,將服務器端證書導入到客戶端信任的 keystore 里。
keytool -import -alias sslclient -keystore sslservertrust -file sslclient.cer
keytool -import -alias sslserver -keystore sslclienttrust -file sslserver.cer

最后得到有用的文件分別為
服務端:sslserverkeys、sslservertrust
客戶端:sslclientkeys、sslclienttrust

二、服務器端代碼 (git) 服務器key工廠類 SecureChatSslContextFactory.java
public class SecureChatSslContextFactory {
    private static final String PROTOCOL = "SSL";
    private static final SSLContext SERVER_CONTEXT;
    private static String SERVER_KEY_STORE = ".sslsslserverkeys";
    private static String SERVER_TRUST_KEY_STORE = ".sslsslservertrust";
    private static String SERVER_KEY_STORE_PASSWORD = "123123123";
    private static String SERVER_TRUST_KEY_STORE_PASSWORD = "123123123";

    static {
        String algorithm = SystemPropertyUtil.get("ssl.KeyManagerFactory.algorithm");
        if (algorithm == null) {
            algorithm = "SunX509";
        }
        SSLContext serverContext;
        try {
            KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(new FileInputStream(SERVER_KEY_STORE), SERVER_KEY_STORE_PASSWORD.toCharArray());
            KeyStore tks = KeyStore.getInstance("JKS");
            tks.load(new FileInputStream(SERVER_TRUST_KEY_STORE), SERVER_TRUST_KEY_STORE_PASSWORD.toCharArray());
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            kmf.init(ks, SERVER_KEY_STORE_PASSWORD.toCharArray());
            tmf.init(tks);
            serverContext = SSLContext.getInstance(PROTOCOL);
            serverContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        } catch (Exception e) {
            throw new Error(e);
        }
        SERVER_CONTEXT = serverContext;
    }
    public static SSLContext getServerContext() {
        return SERVER_CONTEXT;
    }
}
Main.java
public class Main {
    private static final int m_port = 23333;
    public void run() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new Initializer());
            System.out.println("啟動了,端口:" + m_port);
            ChannelFuture f = b.bind(m_port).sync();
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            System.out.println("關閉了");
        }
    }
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
}
Initializer.java
public class Initializer extends ChannelInitializer {
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
        engine.setUseClientMode(false);
        engine.setNeedClientAuth(true);
        pipeline.addLast("ssl", new SslHandler(engine));
        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast(new StringDecoder());
        pipeline.addLast(new StringEncoder());
        pipeline.addLast(new Handler());
    }
}
Handler.java
public class Handler extends SimpleChannelInboundHandler {
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        ctx.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 加入
");
    }
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("收到" + incoming.id() + "消息:" + s);
    }
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
       Channel incoming = ctx.channel();
       System.out.println("SimpleChatClient:" + incoming.remoteAddress() + "在線");
    }
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        ctx.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 離開
");
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println(incoming.remoteAddress() + "掉線");
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println(incoming.remoteAddress() + "異常");
        // 當出現異常就關閉連接
        cause.printStackTrace();
        ctx.close();
    }
}
三、客戶端代碼(git) SecureChatSslContextFactory.java
public class SecureChatSslContextFactory {
    private static final String PROTOCOL = "SSL";
    private static final SSLContext CLIENT_CONTEXT;
    private static String CLIENT_KEY_STORE = ".sslsslclientkeys";
    private static String CLIENT_TRUST_KEY_STORE = ".sslsslclienttrust";
    private static String CLIENT_KEY_STORE_PASSWORD = "321321321";
    private static String CLIENT_TRUST_KEY_STORE_PASSWORD = "321321321";
    static {
        String algorithm = SystemPropertyUtil.get("ssl.KeyManagerFactory.algorithm");
        if (algorithm == null) {
            algorithm = "SunX509";
        }
        SSLContext clientContext;
        try {
            KeyStore ks2 = KeyStore.getInstance("JKS");
            ks2.load(new FileInputStream(CLIENT_KEY_STORE), CLIENT_KEY_STORE_PASSWORD.toCharArray());
            KeyStore tks2 = KeyStore.getInstance("JKS");
            tks2.load(new FileInputStream(CLIENT_TRUST_KEY_STORE), CLIENT_TRUST_KEY_STORE_PASSWORD.toCharArray());
            KeyManagerFactory kmf2 = KeyManagerFactory.getInstance(algorithm);
            TrustManagerFactory tmf2 = TrustManagerFactory.getInstance("SunX509");
            kmf2.init(ks2, CLIENT_KEY_STORE_PASSWORD.toCharArray());
            tmf2.init(tks2);
            clientContext = SSLContext.getInstance(PROTOCOL);
            clientContext.init(kmf2.getKeyManagers(), tmf2.getTrustManagers(), null);
        } catch (Exception e) {
            throw new Error(e);
        }
        CLIENT_CONTEXT = clientContext;
    }
    public static SSLContext getClientContext() {
        return CLIENT_CONTEXT;
    }
Main.java
public class Main {
    private static String m_host = "127.0.0.1";
    private static int m_prot = 23333;
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
    public void run() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bt = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new Initializer());
            Channel channel = bt.connect(m_host, m_prot).sync().channel();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                channel.writeAndFlush(in.readLine() + "
");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}
Initializer.java
public class Initializer extends ChannelInitializer{
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        SSLEngine engine = SecureChatSslContextFactory.getClientContext().createSSLEngine();
        engine.setUseClientMode(true);
        pipeline.addLast("ssl", new SslHandler(engine));
        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast(new StringDecoder());
        pipeline.addLast(new StringEncoder());
        pipeline.addLast(new Handler());
    }
}
Handler.java
public class Handler extends SimpleChannelInboundHandler{
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        System.out.println("收到:" + s);
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67276.html

相關文章

  • SSLSocket

    摘要:定義擴展并提供使用或協議的安全套接字。它也是基于正常的流套接字,但是在網絡傳輸協議如上添加了安全保護層。 SSLSocket定義 SSLSocket擴展Socket并提供使用SSL或TLS協議的安全套接字。它也是基于正常的流套接字,但是在網絡傳輸協議(如TCP)上添加了安全保護層。 SSLSocket相關類 類 功能描述 SSLContext 該類的實例表示安全套接字協議的實...

    ConardLi 評論0 收藏0
  • Apple APNS http2 Provider 開發 使用 okHttp

    摘要:前言升級了后臺推送接口,使用協議,提高了的最大大小,本文介紹新版實現方法基于框架框架不要使用的類直接發送請求,因為底層雖然使用了,可以設置和,但是超過,鏈接還是會斷開,而官方建議保持長鏈接所以最好自建長鏈接,使用底層的類來直接發送請求,并通 前言 Apple 升級了后臺推送接口,使用 http2 協議,提高了 payload 的最大大小(4k),本文介紹新版 APNS 實現方法 基于 ...

    widuu 評論0 收藏0
  • 慕課網_《Netty入門之WebSocket初體驗》學習總結

    時間:2018年04月11日星期三 說明:本文部分內容均來自慕課網。@慕課網:https://www.imooc.com 教學源碼:https://github.com/zccodere/s... 學習源碼:https://github.com/zccodere/s... 第一章:課程介紹 1-1 課程介紹 什么是Netty 高性能、事件驅動、異步非阻塞的IO Java開源框架 基于NIO的客戶...

    Noodles 評論0 收藏0
  • 【Java貓說】SSM整合Netty5.0詳細說明

    摘要:而我們項目在實測時也是將項目發布到測試服務器,通過模擬工具進行測試連接,當數據格式正常,且業務數據正常,服務器就會對指令執行對應的操作。 閱讀本文約5.5分鐘 最近又有粉絲加Q群討論netty整合SSM項目的方式等,我在這里抽了休息日的時候整理一下,一步一步的記錄,注意的是,本案例僅實現了用netty整合SSM后與單片機等類TCP應用通信。 SSM + Netty項目結合思路 對于N...

    dingding199389 評論0 收藏0

發表評論

0條評論

DTeam

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<