【问题标题】:com.sun.xml.wss.XWSSecurityException: Unable to locate certificate for the alias ''com.sun.xml.wss.XWSSecurityException:找不到别名“”的证书
【发布时间】: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


    【解决方案1】:

    客户端使用 xwss:Encrypt 元素和服务器的公钥(证书)来初始化共享密钥(对称密钥)的交换。

    您需要在客户端密钥库中提供服务器证书的别名(服务器的公钥)。

    例子:

    <xwss:Encrypt>
        <xwss:X509Token certificateAlias="myServerPubCert"/>
    </xwss:Encrypt>
    

    您还必须将客户私钥的别名提供给 xwss:sign 元素。

    例子:

    <xwss:Sign includeTimestamp="false">
        <xwss:X509Token certificateAlias="myClientPrivKey"/>
    </xwss:Sign>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2011-07-12
      • 2018-09-28
      相关资源
      最近更新 更多