我正在使用 CXF 构建客户端 SOAP WS,但有一个问题,我只需要加密请求操作中的一个字段。我做了一些测试 使用 SOAPUI,我加载了 X.509 证书并创建了传出 WS-Security 配置并选择了特定字段进行加密,并且 WS 调用正常。但使用CXF,我不知道如何实现。我阅读了一些有关主题和使用 xml CXF 配置的文章,但我还没有找到如何加密 SOAP 请求的特定字段。例如我需要加密请求如下:
<soapenv:Body>
<v2:ExampleRequest>
<v2:field1>1909</v2:field1>
<v2:field2>TEST</v2:field2>
<v2:field3>22</v2:field3>
<v2:field4><xenc:EncryptedData Id="ED-B26971BCECBE85FCAB14666299360062" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><wsse:SecurityTokenReference wsse11:TokenType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKey" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"><wsse:Reference URI="#EK-B26971BCECBE85FCAB14666299359531"/></wsse:SecurityTokenReference></ds:KeyInfo><xenc:CipherData><xenc:CipherValue>1eEPbo5tRbf+c+A7eNJOONL+amAA/To87XAa6nCsM6M=</xenc:CipherValue></xenc:CipherData></xenc:EncryptedData></v2: field4>
</v2: ExampleRequest >
</soapenv:Body>
一些建议或意见,我认为可以使用拦截器,但拦截器只能加密值字段,而无需在原始 SOAP 中使用命名空间。
最佳答案
在 CXF 中,您使用 WSS4J 进行加密。对于客户端,您将使用 WSS4JOutInterceptor。如何使用它的示例可以在 here 中找到。 .
要指定应加密消息的哪一部分,有一个属性 WsHandlerConstants.ENCRYPTION_PARTS 。以下是 JavaDoc 的简短摘录:
Parameter to define which parts of the request shall be encrypted.
The value of this parameter is a list of semi-colon separated element names that identify the elements to encrypt. An encryption mode specifier and a namespace identification, each inside a pair of curly brackets, may preceed each element name.
The encryption mode specifier is either {Content} or {Element}. Please refer to the W3C XML Encryption specification about the differences between Element and Content encryption. The encryption mode defaults to Content if it is omitted. Example of a list:
<parameter name="encryptionParts"
value="{Content}{http://example.org/paymentv2}CreditCard; {Element}{}UserName" />
如果你使用基于java的配置,那就是
outProps.put(WSHandlerConstants.ENCRYPTION_PARTS, "{Content}{http://example.org/paymentv2}CreditCard;{Element}{}UserName");
关于java - 如何使用 CXF 和 X.509 对 SOAP 请求中的特定字段进行加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37978589/