gpt4 book ai didi

java - 带有相互 SSL 的 SOAP - 如何发送凭据?

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

public class ResLookupGetService extends Service {
ServerServicePortType getServerServicePort();
}
public interface ServerServicePortType {
ServerServiceResponse doSoapMethod(RequestObject request, ParamObject parameters);
}

ServerServicePortType service = new ServerServiceGetService().getServerServicePort();
ServerServiceResponse response = service.doSoapMethod(request, parameters);

在需要相互 SSL 加密之前,上面的代码可以很好地调用我的 SOAP 服务。

打开后,我尝试创建一个 SSL 上下文并像这样设置它:

ServerServicePortType service = new ServerServiceGetService().getServerServicePort();

BindingProvider bindingProvider = (BindingProvider) service;
bindingProvider.getRequestContext().put(
"com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory",
getSslContext().getSocketFactory());

ServerServiceResponse response = service.doSoapMethod(request, parameters);

创建 SSLContext 的代码:

public SSLContext getSslContext(String keyStorePath, String keyStoreType, String trustStorePath) {
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
InputStream ksis = ClassLoader.getSystemResourceAsStream(keyStorePath);
keyStore.load(ksis, "mypassword".toCharArray());
ksis.close();

KeyStore trustStore = KeyStore.getInstance("JKS");
InputStream tsis = ClassLoader.getSystemResourceAsStream(trustStorePath);
trustStore.load(tsis, "mypassword".toCharArray());
tsis.close();

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

KeyManagerFactory kmf =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, "mypassword".toCharArray());

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

但它似乎没有正确传递我的凭据。我的设置是否正确?

谢谢

最佳答案

事实证明,使用 BindingProvider 没有任何作用(或者至少我无法将其使用到它产生影响的程度)。

在调用网络服务之前,我简单地设置了这些系统属性:

  private void setSystemProps() {

String keyStoreFileName = "ssl/clientKeyStore.jks";
String keyStorePath = ClassLoader.getSystemResource(keyStoreFileName).getPath();
String keyStoreType = "JKS";
String keyStorePassword = "mypassword";

String trustStoreFileName = "ssl/clientTruststore.jks";
String trustStorePath = ClassLoader.getSystemResource(trustStoreFileName).getPath();
String trustStoreType = "JKS";
String trustStorePassword = "mypassword";

Properties systemProps = System.getProperties();
systemProps.put("javax.net.ssl.keyStore", keyStorePath);
systemProps.put("javax.net.ssl.keyStorePassword", trustStorePassword);
systemProps.put("javax.net.ssl.keyStoreType", keyStoreType);

systemProps.put("javax.net.ssl.trustStore", trustStorePath);
systemProps.put("javax.net.ssl.trustStoreType", trustStoreType);
systemProps.put("javax.net.ssl.trustStorePassword", keyStorePassword);
System.setProperties(systemProps);
}

然后我可以像往常一样进行服务调用:

ServerServicePortType service = new ServerServiceGetService().getServerServicePort();
ServerServiceResponse response = service.doSoapMethod(request, parameters);

值得注意的是,当我设置系统属性时,它们接受任何 Object 作为值,而我最初错误地将其设置为 URL 对象而不是 String。

因此 trustStorePathkeyStorePath 变量被设置为 .getPath() 值,这是一个绝对文件路径,例如:

"/Users/username/path/to/directory/with/ssl/clientKeyStore.jks"

现在一切正常。

关于java - 带有相互 SSL 的 SOAP - 如何发送凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11904620/

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