gpt4 book ai didi

java - 初始化 SSLContext 失败

转载 作者:太空宇宙 更新时间:2023-11-03 15:16:47 28 4
gpt4 key购买 nike

当我尝试使用 getServerSocket 方法初始化 SSLServerSocket 时,如下所示,并定义了 keystore :

public static ServerSocket getServerSocket(int port) {
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
ServerSocket serverSocket = null;
secureRandom = new SecureRandom();
Thread.currentThread().sleep(30000);
secureRandom.nextInt();

setupClientKeyStore();
setupServerKeystore();
setupSSLContext();

SSLServerSocketFactory sf = sslContext.getServerSocketFactory();
serverSocket = sf.createServerSocket(port);
((SSLServerSocket)serverSocket).setNeedClientAuth(true);
}
return serverSocket;
}

private static void setupClientKeyStore() throws GeneralSecurityException, IOException {
clientKeyStore = KeyStore.getInstance("JKS");

KeyStore client = KeyStore.getInstance("JKS");
client.load( new FileInputStream("client.public"), "public".toCharArray() );

KeyStore client1 = KeyStore.getInstance( "JKS" );
client1.load( new FileInputStream("client1.public"), "public".toCharArray() );

KeyStore.Entry clientpublic = client.getEntry("clientpublic", null);
KeyStore.Entry client1public = client1.getEntry("client1public", null);

clientKeyStore.load(null, null);
clientKeyStore.setEntry("clientpublic", clientpublic, null);
clientKeyStore.setEntry("client1public", client1public, null);

}

private static void setupServerKeystore() throws GeneralSecurityException, IOException {
serverKeyStore = KeyStore.getInstance( "JKS" );
serverKeyStore.load( new FileInputStream( "server.private" ),
passphrase.toCharArray() );
}

private static void setupSSLContext() throws GeneralSecurityException, IOException {
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
tmf.init( clientKeyStore );

KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init( serverKeyStore, passphrase.toCharArray() );

sslContext = SSLContext.getInstance("TLS");
sslContext.init( kmf.getKeyManagers(),
tmf.getTrustManagers(),
secureRandom);
}

它抛出异常:

trigger seeding of SecureRandom
trigger seeding of SecureRandom
done seeding SecureRandom
done seeding SecureRandom
java.lang.IllegalStateException: SSLContext is not initialized
atcom.sun.net.ssl.internal.ssl.SSLContextImpl.enginegetServerSocketFactory<SSLContextImpl.java.153>
at javax.net.ssl.SSLContext.getServerSocketFactory<SSLContext.java.272>

Allow unsafe renegotiation: false
Allow legacy hello message: true
Is initial handshake: true
Is secure renegotiation: false

我不明白为什么它会两次放置“触发 SecureRandom 播种”和“完成播种 SecureRandom”。

最佳答案

显然输出出现了两次,因为您同时在两个线程中运行此代码。我建议您也在两个线程中使用相同的 Runnable,这将完全解释此失败。

您不需要在 nextInt() 之前 hibernate 。我也不明白您为什么要将 KeyStore 条目设置为 null。

关于java - 初始化 SSLContext 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604584/

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