[英]Using WebServiceTemplate with a keystore
是否可以使用 java 密钥库配置 WebServiceTemplate?
编辑
我正在寻找一种在 spring 配置中配置密钥库位置的方法
我在六年后发布了这个答案,但说实话,没有一个帖子提供完整而简洁的解决方案。 您所需要的只是spring-ws-core(2.1.4.RELEASE +)和spring-we-security(2.2.4.RELEASE +)依赖项。 下一步是将自定义密钥库和信任库配置为bean,然后在spring配置中将它们注入Web服务模板。
<bean id="myKeyStore" class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean"> <property name="location" value="file:/tmp/config/my-keystore.jks"/> <property name="password" value="password"/> </bean> <bean id="myTrustStore" class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean"> <property name="location" value="file:/tmp/config/my-truststore.jks"/> <property name="password" value="different_password"/> </bean> <bean id="template" class="org.springframework.ws.client.core.WebServiceTemplate"> <property name="messageSender"> <bean class="org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender"> <property name="trustManagers"> <bean class="org.springframework.ws.soap.security.support.TrustManagersFactoryBean"> <property name="keyStore" ref="mytrustStore" /> </bean> </property> <property name="keyManagers"> <bean class="org.springframework.ws.soap.security.support.KeyManagersFactoryBean"> <property name="keyStore" ref="myKeyStore" /> <property name="password" value="password" /> </bean> </property> </bean> </property> </bean>
总之,不需要编写任何代码,使用spring配置可以轻松实现用例。
对于 spring 引导应用程序
我发现 Zoltan 的这篇博客非常有帮助。
https://zoltanaltfatter.com/2016/04/30/soap-over-https-with-client-certificate-authentication/
下面显示了他博客中的代码片段。
在 application.properties 中添加所有必需的属性。
@Value("${key-store}")
private Resource keyStore;
@Value("${key-store-password}")
private String keyStorePassword;
@Value("${trust-store}")
private Resource trustStore;
@Value("${trust-store-password}")
private String trustStorePassword;
并为您的 class 设置 Uri、marshaller、unmarshaller,它正在实现 WebServiceGatewaySupport set messagesender。 为此,需要遵循以下步骤。
使用您的密钥库加载密钥库管理器工厂
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(keyStore.getInputStream(), keyStorePassword.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(ks, keyStorePassword.toCharArray());
使用您的信任库加载 Trust store manager factory
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ts);
创建 HttpsUrlConnectionMessageSender 的实例并使用上面创建的密钥管理器工厂和信任管理器工厂设置密钥和信任管理器。
HttpsUrlConnectionMessageSender messageSender = new HttpsUrlConnectionMessageSender();
messageSender.setKeyManagers(keyManagerFactory.getKeyManagers());
messageSender.setTrustManagers(trustManagerFactory.getTrustManagers());
最后设置消息发件人
client.setMessageSender(messageSender);
这里的客户端是扩展 WebServiceGatewaySupport 的 class
我在这篇文章中找到的答案和问题让我一直在追逐我的尾巴。 最后,我通过将密钥库导入Weblogic服务器上的密钥库,将其部署到WebLogic 11g的应用程序:
C:\\ bea \\ jrockit_160_14_R27.6.5-32 \\ jre \\ bin> keytool -importkeystore -srckeystore \\ workspace \\ myProject \\ webservice.keystore
然后,我将WebLogic密钥库的配置更改为指向此密钥库。 您可以通过WL控制台执行此操作: Environment-> Servers-> AdminServer-> Keystores 。 将密钥库:选择更改为“自定义标识和自定义信任” ,然后将标识 (传入)和信任 (传出)部分中的路径填入密钥库位置。 在Windows XP上,我的是\\ Documents an Settings \\ my id \\ .keystore。
我没有提供密码,我相信它是可选的。
我认为您可以使用KeyStore.Builder以编程方式加载密钥库:
所以也许有一个有Web服务模板或扩展它的类,然后在你的spring配置中设置keystore的文件路径,并使它成为一个inizialing bean(Spring 3中的@PostConstruct?),然后加载密钥库。
File f = new File(keyStorePath);
KeyStore.Builder builder = KeyStore.Builder.newInstance("type",provider,file,protection);
KeyStore keystore = builder.getKeyStore();
好的 - 实际上将它与webservicetemplate一起使用我认为它必须基于这里记录的密钥库回调: http : //static.springsource.org/spring-ws/sites/1.5/reference/html/security.html#d0e4462
或者可以使用spring org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender,您可以在其上设置keystoremanager。 然后,您的webservicetemplate可以使用它。
有点像这样:
<bean id="template" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender">
<property name=""></property>
</bean>
</property>
</bean>
HTH
这个帖子的延迟回复但无论如何:请注意,一旦你拥有了你的密钥库和其他所有设置,你可能会惊讶地发现WebServiceTemplate似乎不支持HTTPS连接!
确保将messageSender
属性设置为org.springframework.ws.transport.http.CommonsHttpMessageSender
。 默认的WebServiceMessageSender
实现不支持HTTPS。
我假设你想要配置JSSE使用的密钥库,因为这是模板将使用的。 JSSE将始终始终查看javax.net.ssl.keyStore / javax.net.ssl.keyStorePassword系统属性以查找密钥库。 您可以使用InitializingBean在Spring中配置这些属性。
请注意,如果您在应用服务器中运行,则可能已在Spring初始化之前配置JSSE。 在这种情况下,您需要使用app服务器界面来设置密钥库 - 通常在命令行上使用-D params。
<bean id="jsseInitializer" lazy-init="false" class="com.blah.JsseInitializer">
<property name="trustStoreLocation" value="${pnet.batch.trustStore.location}"/>
<property name="trustStorePassword" value="${pnet.batch.trustStore.password}"/>
<property name="keyStoreLocation" value="${pnet.batch.keyStore.location}"/>
<property name="keyStorePassword" value="${pnet.batch.keyStore.password}"/>
</bean>
public class JsseInitializer implements InitializingBean {
private String trustStoreLocation;
private String trustStorePassword;
private String keyStoreLocation;
private String keyStorePassword;
public String getTrustStoreLocation() {
return trustStoreLocation;
}
public void setTrustStoreLocation(String trustStoreLocation) {
this.trustStoreLocation = trustStoreLocation;
}
public String getTrustStorePassword() {
return trustStorePassword;
}
public void setTrustStorePassword(String trustStorePassword) {
this.trustStorePassword = trustStorePassword;
}
public String getKeyStoreLocation() {
return keyStoreLocation;
}
public void setKeyStoreLocation(String keyStoreLocation) {
this.keyStoreLocation = keyStoreLocation;
}
public String getKeyStorePassword() {
return keyStorePassword;
}
public void setKeyStorePassword(String keyStorePassword) {
this.keyStorePassword = keyStorePassword;
}
public void afterPropertiesSet() throws Exception {
System.setProperty("javax.net.ssl.trustStore", trustStoreLocation);
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
System.setProperty("javax.net.ssl.keyStore", keyStoreLocation);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
}
}
您应该使用keytool命令在用于运行应用服务器的JDK的密钥库(可能是cacerts文件)中安装所需的证书。
这是一个示例命令:
keytool -import -trustcacerts -alias someAlias -file someCert.crt -keystore yourKeystore
编辑 :基于更新的问题,看起来这可能是您正在寻找的: http : //static.springsource.org/spring-ws/sites/1.5/reference/html/security.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.