【发布时间】:2019-10-17 07:23:39
【问题描述】:
我正在尝试加密 SOAP 请求。问题是 reuest 是正确的,但是当涉及到加密时,我得到以下错误:
这是在 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>
【问题讨论】:
标签: spring spring-boot spring-security spring-ws