【问题标题】:EncryptedData was not recognized, apache cxf with springEncryptedData 无法识别,带有 spring 的 apache cxf
【发布时间】:2020-10-28 19:32:39
【问题描述】:

我的错误是这样的:

org.apache.cxf.interceptor.Fault:消息部分 {http://www.w3.org/2001/04/xmlenc#}EncryptedData 未被识别。 (是否存在于服务 WSDL 中?)

这是由于设置了用于解码加密数据的属性。我的问题是我在使用 apache cxf 时遇到问题(时间戳和签名工作正常)。

这是我的部分代码:

public WSS4JStaxInInterceptor wss4JStaxInInterceptor() 抛出异常 {

   WSSSecurityProperties inProperties = new WSSSecurityProperties();
   //inProperties.addAction(WSSConstants.USERNAMETOKEN);
   inProperties.addAction(WSSConstants.TIMESTAMP);
   inProperties.addAction(WSSConstants.SIGNATURE);
   inProperties.addAction(WSSConstants.ENCRYPTION);
   inProperties.setEncryptionUser("xxx");
   inProperties.loadDecryptionKeystore(this.getClass().getClassLoader().getResource("\"C:\\\\Users\\\\miha_\\\\OneDrive\\\\Dokumenti\\\\Job\\\\Lj\\\\Spring\\\\demo\\\\src\\\\main\\\\resources\\\\xxxx.jks"),"xxx".toCharArray());;
   inProperties.setMustUnderstand(false);
   inProperties.loadSignatureKeyStore(this.getClass().getClassLoader().getResource("\"C:\\\\Users\\\\miha_\\\\OneDrive\\\\Dokumenti\\\\Job\\\\Lj\\\\Spring\\\\demo\\\\src\\\\main\\\\resources\\\\xxxx.jks"),"xxx".toCharArray());
   inProperties.setSignatureUser("cbd");
   //inProperties.setSignatureVerificationCryptoProperties(wss4jInProperties());

   //inProperties.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_DIGEST);
   inProperties.setCallbackHandler(new ClientKeystorePasswordCallback());

   WSS4JStaxInInterceptor wss4JStaxInInterceptor = new WSS4JStaxInInterceptor(inProperties);

   return  wss4JStaxInInterceptor;

}

所以我定义了“loadDecryptionKeystore”,我在其中获得了密钥库。但是我在哪里定义要使用哪个证书(使用 setEncryptionUser("xxx"); ?)以及访问证书中私钥的密码在哪里? 我是否还应该定义其他东西,如何定义?

ps.:这是接收请求时服务器部分的配置

谢谢

【问题讨论】:

    标签: spring-boot web-services spring-security cxf jax-ws


    【解决方案1】:

    您可以通过调用 setEncryptionUser 来定义要获取的证书。

    私钥的密码应由您通过调用setCallbackHandler 定义的CallbackHandler 提供。
    当需要私钥的密码时,框架将通过调用带有WSPasswordCallback 实例的回调处理程序来请求它(有关详细信息,请参阅有关WSPasswordCallback identifiers 的文档部分)。

    回调处理程序的一个简单示例:

    /**
     * @see <a href="https://github.com/gmazza/blog-samples/blob/master/cxf_x509_profile/client/src/main/java/client/ClientKeystorePasswordCallback.java">ClientKeystorePasswordCallback</a>
     */
    public class ClientKeystorePasswordCallback implements CallbackHandler {
    
        private Map<String, String> passwords =
                new HashMap<String, String>();
    
        public ClientKeystorePasswordCallback() {
            passwords.put("myclientkey", "ckpass");
        }
    
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (int i = 0; i < callbacks.length; i++) {
                WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
    
                String pass = passwords.get(pc.getIdentifier());
                if (pass != null) {
                    pc.setPassword(pass);
                    return;
                }
            }
        }
    }
    

    【讨论】:

    • 我已按照您的建议进行设置,但主要问题是未触发回调(我已将一些日志记录在其中),为什么我无法找出。我已经把所有东西都放在了 pastebin 里,如果有什么问题,我也会把它放在 pom 部分。这是我把所有东西都放在这里的 pastebin:pastebin.com/uEA3PWvK 非常感谢!
    • 另一件事,如果我将“public WSS4JStaxInInterceptor wss4JStaxInInterceptor() throws Exception {}”定义为@Bean,我会得到错误:pastebin.com/TrHgVsqj
    • 关于错误 - 错误日志表明在调用方法 wss4JStaxInInterceptor 时发生了 NullPointerException。由于错误日志不包含更多详细信息,因此我无法告诉您更多信息。您必须调试该方法以找到导致它的行并修复它。
    • 谢谢。是的,主要问题是密钥库的路径,我已经修复了它,现在触发了回调。主要问题是现在我收到错误:“读取 XMLStreamReader 时出错:org.apache.wss4j.common.ext.WSSecurityException:验证消息时遇到安全错误”这里也是过去bin:pastebin.com/kBDT3XB6 有什么办法可以调试这个。我在 spring 上使用与 wss4j 相同的证书并且它正在工作(但我对肥皂操作有问题)。
    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2019-01-23
    • 2014-02-09
    • 2019-08-06
    • 2020-02-02
    相关资源
    最近更新 更多