java - Apache CXF 附件安全

标签 java cxf ws-security

我尝试让我的 apache cxf 客户端对附件进行签名和加密。正如我现在有了我的解决方案,它确实对消息正文进行签名和加密,但它忽略附件。

我有以下代码:

    Map<String, Object> props = new HashMap<>();
    props.put("action", "Signature Encrypt");
    props.put("signaturePropFile", "client.properties");
    props.put("passwordCallbackClass", "******.KeystorePasswordCallback");
    props.put("user", "node1");
    props.put("signatureKeyIdentifier", "DirectReference");
    props.put("signatureParts",
            "{Element}{http://www.w3.org/2003/05/soap-envelope}Body;" +
                    "{}cid:Attachments;");
    props.put("encryptionParts",
            "{Content}{http://www.w3.org/2003/05/soap-envelope}Body;" +
                    "{Element}cid:Attachments;" );
    props.put("encryptionPropFile", "client.properties");
    props.put("encryptionKeyIdentifier", "IssuerSerial");
    props.put("encryptionKeyTransportAlgorithm",
            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");

    WSS4JOutInterceptor wss4jOut = new WSS4JOutInterceptor(props);

    client.getOutInterceptors().add(wss4jOut);

我正在关注this example制作我的代码。

{}cid:Attachments 部分来自 this apache page .

最佳答案

问题是 Apache CXF 由于某种原因在向消息添加附件的拦截器之前运行签名/加密拦截器。

简单的解决方法是添加您自己的 WSS4J 出/入拦截器(问题出在两种方式上 - 传入/传出消息),该拦截器在加密/解密/签名(检查)完成之前添加附件。 基本上,您可以打开添加附件的SAAJ拦截器,并将部分代码从handleMessage方法复制粘贴到您的拦截器。 对于out incerceptor:

   @Override
    public void handleMessage(SoapMessage mc) throws Fault {
        super.handleMessage(mc);
        SOAPMessage soapMessage = mc.getContent(SOAPMessage.class);
        if (soapMessage != null) {
            if (soapMessage.countAttachments() > 0) {
                if (mc.getAttachments() == null) {
                    mc.setAttachments(new ArrayList<Attachment>(soapMessage
                            .countAttachments()));
                }
                Iterator<AttachmentPart> it = CastUtils.cast(soapMessage.getAttachments());
                while (it.hasNext()) {
                    AttachmentPart part = it.next();
                    String id = AttachmentUtil.cleanContentId(part.getContentId());
                    AttachmentImpl att = new AttachmentImpl(id);
                    try {
                        att.setDataHandler(part.getDataHandler());
                    } catch (SOAPException e) {
                        throw new Fault(e);
                    }
                    Iterator<MimeHeader> it2 = CastUtils.cast(part.getAllMimeHeaders());
                    while (it2.hasNext()) {
                        MimeHeader header = it2.next();
                        att.setHeader(header.getName(), header.getValue());
                    }
                    mc.getAttachments().add(att);
                    it.remove();
                }
            }
        }
    }

对于拦截器:

 @Override
    public void handleMessage(SoapMessage msg) throws Fault {
        super.handleMessage(msg);
        SOAPMessage soapMessage = msg.getContent(SOAPMessage.class);
        soapMessage.removeAllAttachments();
        Collection<Attachment> atts = msg.getAttachments();
        if (atts != null) {
            for (Attachment a : atts) {
                if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
                    try {
                        ((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(msg);
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                }
                AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
                Iterator<String> i = a.getHeaderNames();
                while (i != null && i.hasNext()) {
                    String h = i.next();
                    String val = a.getHeader(h);
                    ap.addMimeHeader(h, val);
                }
                if (StringUtils.isEmpty(ap.getContentId())) {
                    ap.setContentId(a.getId());
                }
                soapMessage.addAttachmentPart(ap);
            }
        }
        msg.setAttachments(Collections.<Attachment>emptyList());
        msg.setContent(SOAPMessage.class, soapMessage);
    }

关于java - Apache CXF 附件安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45279836/

相关文章:

java - HashSet 在此实例中是否为性能提供任何附加值?

java - 将文件保存到Java中的特定目录?

java - 在 WebServices 插件设置中设置有效的 CXF 路径

java - Apache CXF 策略异常(WS 安全)- 无法检测到安全配置

java - 如何通过 WSS4J 使用 Eclipse 生成的代理( Axis 1.4)对 SOAP 请求进行数字签名?

java - 寻找包含 JuiCEProviderOpenSSL 的 jar 文件

java - 如何在 java 流中添加我的方法?

java - 数据核空间映射误差

java - 如何获取Cxfrs :server input and output types

java - jackson JsonMappingException : Invalid type id