gpt4 book ai didi

java - 添加用于客户端身份验证的 trustStore

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:55 26 4
gpt4 key购买 nike

<分区>

A server和相应的client支持客户端身份验证,但如此处所述:SSLHandshakeException: no cipher suites in common ,没有 trustStore 引用,即它们使用默认的 trustStore。如何指定 trustStore?

类文件服务器:

private static ServerSocketFactory getServerSocketFactory(String type) {
if (type.equals("TLS")) {
SSLServerSocketFactory ssf = null;

Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks");
systemProps.put( "javax.net.ssl.trustStorePassword", "p@ssw0rd");
System.setProperties(systemProps);

try {
// set up key manager to do server authentication
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
char[] passphrase = "p@ssw0rd".toCharArray();

ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("keystore.jks"), passphrase);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);

ssf = ctx.getServerSocketFactory();
return ssf;
} catch (Exception e) {
e.printStackTrace();
}
}

SSLSocketClientWithClientAuth:

    try {

/*
* Set up a key manager for client authentication
* if asked by the server. Use the implementation's
* default TrustStore and secureRandom routines.
*/
Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks");
systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");
System.setProperties(systemProps);

SSLSocketFactory factory = null;
try {
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
char[] passphrase = "changeit".toCharArray();

ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("JKS");

ks.load(new FileInputStream("keystore.jks"), passphrase);

kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);

factory = ctx.getSocketFactory();
} catch (Exception e) {
throw new IOException(e.getMessage());
}

SSLSocket socket = (SSLSocket)factory.createSocket(host, port);

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com