栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用2向SSL握手设置Netty(客户端和服务器证书)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用2向SSL握手设置Netty(客户端和服务器证书)

这是基于netty项目的HttpSnoop服务器示例的解决方案。

设置客户端管道时,必须如下设置ssl引擎:

public ChannelPipeline getPipeline() throws Exception {    // Create a default pipeline implementation.    ChannelPipeline pipeline = pipeline();    // Uncomment the following line if you want HTTPS    SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();    engine.setUseClientMode(false);    engine.setNeedClientAuth(true);    pipeline.addLast("ssl", new SslHandler(engine));    pipeline.addLast("deprer", new HttpRequestDeprer());    pipeline.addLast("logger", new RequestAuditLogger());    // Uncomment the following line if you don't want to handle HttpChunks.    pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));    pipeline.addLast("outputLogger", new ResponseAuditLogger());    pipeline.addLast("enprer", new HttpResponseEnprer());    // Remove the following line if you don't want automatic content compression.    pipeline.addLast("deflater", new HttpContentCompressor());    pipeline.addLast("handler", new HttpSnoopServerHandler());    return pipeline;}}

然后,必须对SSLContext进行如下修改,以建立除密钥库(SecureChatSslContextFactory)之外的信任库:

public final class SecureChatSslContextFactory {private static Logger logger = LoggerFactory.getLogger(SecureChatSslContextFactory.class);private static final String PROTOCOL = "TLS";private static final SSLContext SERVER_CONTEXT;private static final SSLContext CLIENT_CONTEXT;static {    SSLContext serverContext = null;    SSLContext clientContext = null;        // get keystore and trustore locations and passwords    String keyStoreLocation = System.getProperty("javax.net.ssl.keyStore");    String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");    String trustStoreLocation = System.getProperty("javax.net.ssl.trustStore");    String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");    try {        KeyStore ks = KeyStore.getInstance("JKS");        ks.load(KeyStoreStreamManager.asInputStream(keyStoreLocation),     keyStorePassword.toCharArray());        // Set up key manager factory to use our key store        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());        kmf.init(ks, keyStorePassword.toCharArray());          // truststore        KeyStore ts = KeyStore.getInstance("JKS");        ts.load(KeyStoreStreamManager.asInputStream(trustStoreLocation),     trustStorePassword.toCharArray());        // set up trust manager factory to use our trust store        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());        tmf.init(ts);        // Initialize the SSLContext to work with our key managers.        serverContext = SSLContext.getInstance(PROTOCOL);        serverContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);    } catch (Exception e) {        throw new Error(     "Failed to initialize the server-side SSLContext", e);    }    try {        clientContext = SSLContext.getInstance(PROTOCOL);        clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);    } catch (Exception e) {        throw new Error(     "Failed to initialize the client-side SSLContext", e);    }    SERVER_ConTEXT = serverContext;    CLIENT_ConTEXT = clientContext;}public static SSLContext getServerContext() {    return SERVER_CONTEXT;}public static SSLContext getClientContext() {    return CLIENT_CONTEXT;}private SecureChatSslContextFactory() {    // Unused}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430858.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号