不复制完整密钥库客户端使用自签名证书的Java SSL连接(Java SSL connection

2019-08-07 05:59发布

我在Java中一起与客户端应用程序,将发布新的许可证请求,并在该服务器上验证现有许可设立发牌的servlet。 该servlet在Tomcat中运行。 我Tomcat配置,使其只允许连接到Servlet通过https,这工作得很好。

我一直在使用创建的自签名证书'keytool -genkey -alias www.mysite.com -keyalg RSA -keystore license.store'这将创建一个文件license.store尖tomcat的这个keystoreFile连同密码asdf1234

当我尝试从客户端连接到Servlet在Java中HTTPS,我收到了熟悉PKIX path building failed ,因为该证书不在信任。 我试图固定此使用此所得的在下面的代码建议:

private SSLSocketFactory getSSLFactory() throws Exception {

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream is = this.getClass().getResourceAsStream("license.store");

    if(is ==null) {
        return null;
    }

    keyStore.load(is, "asdf1234".toCharArray());
    is.close();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(keyStore);

    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, tmf.getTrustManagers(), null);

    return ctx.getSocketFactory();
}

这之后,我打电话:

HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setSSLSocketFactory(getSSLFactory());

这导致succesfull连接。

现在的问题是,我只能得到这个当我复制工作license.store到客户端,并且加载到KeyStore.load() 它不罢工我很安全复制私钥和服务器使用的客户端密码。 有没有什么方法提取只能从license.store的公钥和使用? 我一直在寻找这个论坛和其他人现在一天似乎仍不能得到它。

Answer 1:

你不应该产生一个公私密钥对,而是服务器的证书导入到你(客户)的Java信任。 该证书是不是秘密,因此不提供在客户端安全风险。 查看-import的密钥工具选项。 下面是一个例子 。



文章来源: Java SSL connection with self-signed certificate without copying complete keystore to client