Java 如何检索SSL客户端使用的KeyManager、TrustManager和SecureRandom对象?

Java 如何检索SSL客户端使用的KeyManager、TrustManager和SecureRandom对象?,java,ssl,tls1.2,sslsocketfactory,Java,Ssl,Tls1.2,Sslsocketfactory,我正在尝试创建一个SSL套接字工厂,它使用TSLv1.2协议执行握手 到目前为止我所拥有的[更新1/18]: SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); ServerSocketFactory sf = SSLServerSocketFactory.getDefault(); KeyManager[] km = KeyManagerFactory.getInstance(KeyManagerFactory.getDefau

我正在尝试创建一个SSL套接字工厂,它使用TSLv1.2协议执行握手

到目前为止我所拥有的[更新1/18]

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
ServerSocketFactory sf = SSLServerSocketFactory.getDefault();
KeyManager[] km = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).getKeyManagers();
TrustManager[] tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).getTrustManagers();
SecureRandom random = new SecureRandom();
sslContext.init(km, tm, random);
requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());
我希望从
SSLServerSocketFactory.getDefault()
中获取KeyManager、TrustManager和SecureRandom对象,但是没有用于此的getter。
还有别的地方可以帮我拿吗?还是一种更有效的方法

我不想手动创建密钥和信任管理器,以避免需要特定于系统的配置

参考的完整方法:

    public MyOutBoundClientWSImpl(URL wsdlUrl){
        super(wsdlUrl, serviceName);
        this.wsUrl=wsdlUrl;
        this.mService = this.getMySoapHttpPort();
        Map<String, Object> requestContext = ((BindingProvider)mService).getRequestContext();
        requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, REQUEST_TIMEOUT); // Timeout in millis
        requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT); // Timeout in millis
        try {
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            ServerSocketFactory sf = SSLServerSocketFactory.getDefault();
            KeyManager[] km = ??;
            TrustManager[] tm = ??;
            SecureRandom random = ??;
            sslContext.init(km, tm, random);
            requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }
public MyOutBoundClientWSImpl(URL wsdlUrl){
超级(wsdlUrl,serviceName);
this.wsUrl=wsdlUrl;
this.mService=this.getMySoapHttpPort();
Map requestContext=((BindingProvider)mService.getRequestContext();
requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT,REQUEST_TIMEOUT);//超时(毫秒)
requestContext.put(BindingProviderProperties.CONNECT\u TIMEOUT,CONNECT\u TIMEOUT);//超时(毫秒)
试一试{
SSLContext SSLContext=SSLContext.getInstance(“TLSv1.2”);
ServerSocketFactory sf=SSLServerSocketFactory.getDefault();
密钥管理器[]公里=??;
TrustManager[]tm=??;
安全随机=??;
sslContext.init(km,tm,random);
put(BindingProviderProperties.SSL_SOCKET_工厂,sslContext.getSocketFactory());
}捕获(无算法异常){
抛出新的IllegalArgumentException(e.getMessage(),e);
}
}

查看官方文件:

初始化此上下文。前两个参数中的任何一个都可能是 null,在这种情况下,将搜索已安装的安全提供程序 以实现适当工厂的最高优先级。 同样,安全随机参数可能为null,在这种情况下 将使用默认实现

事实证明这很容易做到,只需将所有null传递给init方法

    public MyOutBoundClientWSImpl(URL wsdlUrl){
        super(wsdlUrl, serviceName);
        this.wsUrl=wsdlUrl;
        this.mService = this.getMySoapHttpPort();
        Map<String, Object> requestContext = ((BindingProvider)mService).getRequestContext();
        requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, REQUEST_TIMEOUT); // Timeout in millis
        requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT); // Timeout in millis
        try {
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            KeyManager[] km = null;
            TrustManager[] tm = null;
            SecureRandom random = null;                
            sslContext.init(km, tm, random);
            requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }
public MyOutBoundClientWSImpl(URL wsdlUrl){
超级(wsdlUrl,serviceName);
this.wsUrl=wsdlUrl;
this.mService=this.getMySoapHttpPort();
Map requestContext=((BindingProvider)mService.getRequestContext();
requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT,REQUEST_TIMEOUT);//超时(毫秒)
requestContext.put(BindingProviderProperties.CONNECT\u TIMEOUT,CONNECT\u TIMEOUT);//超时(毫秒)
试一试{
SSLContext SSLContext=SSLContext.getInstance(“TLSv1.2”);
KeyManager[]km=null;
TrustManager[]tm=null;
SecureRandom=null;
sslContext.init(km,tm,random);
put(BindingProviderProperties.SSL_SOCKET_工厂,sslContext.getSocketFactory());
}捕获(无算法异常){
抛出新的IllegalArgumentException(e.getMessage(),e);
}
}

手动创建它们不会使您的代码系统特定。不清楚你的问题是什么。我想使用默认使用的任何东西。另外,我不知道密钥库密码,也不确定是否可以获得它。此外,如果我在两个不同的环境中使用了密码或具有不同的密钥库,我将需要不同的配置来设置它。这就是我所说的系统特定性。
    public MyOutBoundClientWSImpl(URL wsdlUrl){
        super(wsdlUrl, serviceName);
        this.wsUrl=wsdlUrl;
        this.mService = this.getMySoapHttpPort();
        Map<String, Object> requestContext = ((BindingProvider)mService).getRequestContext();
        requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, REQUEST_TIMEOUT); // Timeout in millis
        requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT); // Timeout in millis
        try {
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            KeyManager[] km = null;
            TrustManager[] tm = null;
            SecureRandom random = null;                
            sslContext.init(km, tm, random);
            requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }