我正在尝试加密SOAP请求。问题是请求是正确的,但是在加密时却出现错误提示:
这在spring-ws文档(https://docs.spring.io/spring-ws/site/reference/html/security.html)中:
7.2.4.2。加密要加密传出的SOAP消息,安全策略文件应包含一个Encrypt元素。该元素可以进一步 携带一个EncryptionTarget元素,该元素指示 消息应加密,并使用SymmetricKey指示 应该使用共享机密而非常规公钥来 加密消息。您可以阅读其他元素的描述 在这里。
XwsSecurityInterceptor会将EncryptionKeyCallback触发到 注册处理程序,以检索加密信息。 在Spring-WS中,有一个类处理这一特定问题 回调:KeyStoreCallbackHandler。
我的错误:
2019-10-16 19:56:52.482 ERROR 5264 --- [nio-8080-exec-1] j.e.resource.xml.webservices.security : WSS0221: Unable to locate matching certificate for Key Encryption using Callback Handler.
2019-10-16 19:56:52.494 ERROR 5264 --- [nio-8080-exec-1] com.sun.xml.wss.logging.impl.filter : WSS1413: Error extracting certificate
com.sun.xml.wss.XWSSecurityException: Unable to locate certificate for the alias ''
at com.sun.xml.wss.impl.misc.DefaultSecurityEnvironmentImpl.getCertificate(DefaultSecurityEnvironmentImpl.java:365) ~[xws-security-3.0.jar:3.0-FCS]
我的代码:
@Bean
public XwsSecurityInterceptor securityInterceptor() {
XwsSecurityInterceptor securityInterceptor = new XwsSecurityInterceptor();
securityInterceptor.setPolicyConfiguration(new ClassPathResource("securityPolicy.xml"));
try{
securityInterceptor.setCallbackHandler(callback());
securityInterceptor.afterPropertiesSet();
}
catch (Exception e) {
System.out.println("display Expensionm: " + e);
}
return securityInterceptor;
}
@Bean
public KeyStoreCallbackHandler callback() throws Exception{
KeyStoreCallbackHandler callbackHandler = new KeyStoreCallbackHandler();
callbackHandler.setPrivateKeyPassword("sopasswordo");
callbackHandler.setDefaultAlias("test");
callbackHandler.setKeyStore(keyStoreFactoryBean());
callbackHandler.setTrustStore(TrustFactoryBean());
return callbackHandler;
}
@Bean
public KeyStore keyStoreFactoryBean(){
KeyStoreFactoryBean keyStoreFactoryBean = new KeyStoreFactoryBean();
keyStoreFactoryBean.setPassword("sotore_passwordo");
//keyStoreFactoryBean.setType("JKS");
System.out.println("1");
keyStoreFactoryBean.setLocation(new FileSystemResource("C:\\Users\\miha_\\OneDrive\\Dokumenti\\Job\\Lj\\Spring\\Porting\\target\\classes\\softnet.jks"));
try{
keyStoreFactoryBean.afterPropertiesSet();
}catch (Exception e){
System.out.println("e: "+e );
}
return keyStoreFactoryBean.getObject();
}
@Bean
public KeyStore TrustFactoryBean(){
KeyStoreFactoryBean trustFactory = new KeyStoreFactoryBean();
trustFactory.setPassword("sostore_passwordo");
//keyStoreFactoryBean.setType("JKS");
System.out.println("1");
trustFactory.setLocation(new FileSystemResource("C:\\Users\\miha_\\OneDrive\\Dokumenti\\Job\\Lj\\Spring\\Porting\\target\\classes\\trust.jks"));
try{
trustFactory.afterPropertiesSet();
}catch (Exception e){
System.out.println("e: "+e );
}
return trustFactory.getObject();
}
@Override
public void addInterceptors(List interceptors) {
interceptors.add(securityInterceptor());
}
我不知道未设置加密证书的别名,并且未检索到证书。我设置了默认别名,但我想我还缺少其他东西。
我的政策
<xwss:SecurityConfiguration xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
<xwss:Sign includeTimestamp="false" />
<xwss:Encrypt />
</xwss:SecurityConfiguration>
答案 0 :(得分:0)
客户端将xwss:Encrypt元素与服务器的公共密钥(证书)一起使用,以初始化共享密钥(对称密钥)的交换。
您需要在客户端密钥库中提供服务器证书的别名(服务器的公共密钥)。
示例:
<xwss:Encrypt>
<xwss:X509Token certificateAlias="myServerPubCert"/>
</xwss:Encrypt>
还必须将客户端私钥的别名提供给xwss:sign元素。
示例:
<xwss:Sign includeTimestamp="false">
<xwss:X509Token certificateAlias="myClientPrivKey"/>
</xwss:Sign>