繁体   English   中英

Apache CXF附件安全性

[英]Apache cxf attachment 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);

我正在按照此示例制作代码。

{}cid:Attachments部分来自此apache页面

问题在于,Apache CXF出于某种原因在将附件添加到消息的拦截器之前运行符号/加密拦截器。

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

   @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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM