java - 如何使用 CXF 和 X.509 对 SOAP 请求中的特定字段进行加密

标签 java soap cxf

我正在使用 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/

相关文章:

java - 通过拆分正则表达式从 Java 中的字符串中提取数字

c# - 在服务器之间快速传输大数据

web-services - 如何从 Equinox OSGi 应用程序公开和使用 Web 服务?

grails - WSClient Soap客户端在grails/groovy中

java - 运行 junit 测试失败。无法创建新的浏览器 session

java - Spring-MVC : serving JSP that was already statically included in another loaded jsp

php - 如何使用 Web 服务将 Joomla 登录 session 从一个网站共享到另一个网站?

wcf - BizTalk WCF 超时问题

java - 在 Java 中的 SoapUI API 中向 TestStep 添加断言

java - 尝试调用 CXF/SOAP web 服务时出现 LinkageError