在加密之前修改 CXF RequestSecurityToken 的 XML

Wil*_*ren 6 java wcf ws-security web-services cxf

我正在使用 Apache CXF 从 Java 客户端调用 WCF 服务。使用另一个地址的 STS 来保护服务的安全。我已将服务客户端配置为在调用主服务之前调用安全令牌,并且它可以工作(它尝试调用 STS),但 STS 期望在元素中提供一些额外的数据RequestSecurityToken。STS 的策略指定RequestSecurityToken在发送之前对其进行加密和签名,这就是导致我出现问题的原因。加密和签名正在工作,但我似乎无法在加密之前修改 SOAP 消息。

我查看了这个问题:如何修改出站 CXF 请求的原始 XML 消息?虽然它有很大帮助,但我需要更改的 XML 部分位于已加密和签名的 SOAP 消息的一部分内。

我做了一个Interceptor并在我能找到的所有不同阶段进行了尝试,但在RequestSecurityToken创建和加密和签名之间似乎没有一个阶段被调用。

有吗?或者已经有一个可以向其中添加额外元素的工具RequestSecurityToken

为了清楚起见编辑:

这是我的 RST 现在的样子:

<wst:RequestSecurityToken xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
    <wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>
    <wsp:AppliesTo xmlns:wsp="http://www.w3.org/ns/ws-policy">
        <wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing">
            <wsa:Address>http://localhost:9085/MyService</wsa:Address>
        </wsa:EndpointReference>
    </wsp:AppliesTo>
    <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>
    <wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>
    <wst:KeySize>192</wst:KeySize>
    <wst:Entropy>
        <wst:BinarySecret Type="http://docs.oasis-open.org/ws-sx/ws-trust/200512/Nonce">OlbfbuCUf3N2lNf9mhD03gfeMk0TfPI2nLWx8edlL5w=</wst:BinarySecret>
    </wst:Entropy>
    <wst:ComputedKeyAlgorithm>http://docs.oasis-open.org/ws-sx/ws-trust/200512/CK/PSHA1</wst:ComputedKeyAlgorithm>
    <wst:Renewing/>
</wst:RequestSecurityToken>
Run Code Online (Sandbox Code Playgroud)

以下是服务提供商的文档所说的大致内容(请注意Credentials末尾附近的元素):

<t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
    <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
    <wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
        <EndpointReference xmlns="http://www.w3.org/2005/08/addressing">
            <Address>http://localhost:9085/MyService</Address>
        </EndpointReference>
    </wsp:AppliesTo>
    <t:Entropy>
        <t:BinarySecret u:Id="uuid-e2d08122-45ab-45cd-80d1-46de2306836b-1" Type="http://schemas.xmlsoap.org/ws/2005/02/trust/Nonce" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">Ssex4V/175NCIOK1j4Mmbl47GiThOQMd</t:BinarySecret>
    </t:Entropy>
    <t:KeySize>192</t:KeySize>
    <t:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</t:TokenType>
    <t:KeyType>http://schemas.xmlsoap.org/ws/2005/02/trust/SymmetricKey</t:KeyType>
    <Credentials>
        <UserName type="string">username</UserName>
        <Password type="string">password</Password>
    </Credentials>
    <t:ComputedKeyAlgorithm>http://schemas.xmlsoap.org/ws/2005/02/trust/CK/PSHA1</t:ComputedKeyAlgorithm>
</t:RequestSecurityToken>
Run Code Online (Sandbox Code Playgroud)

这或多或少是我的代码 - 我将在哪里更改 RST?:

  CXFBusFactory bf = new CXFBusFactory();
  Bus bus = bf.createBus();

  STSClient stsClient = new STSClient(bus);
  Map<String, Object> stsProperties = new HashMap<>();

  stsProperties.put(SecurityConstants.ENCRYPT_CRYPTO, stsMerlin);
  stsProperties.put(SecurityConstants.SIGNATURE_CRYPTO, stsMerlin);
  stsProperties.put(SecurityConstants.IS_BSP_COMPLIANT, "false");
  stsClient.setProperties(stsProperties);

  stsClient.setWsdlLocation("http://localhost:8090/SecurityTokenService?wsdl");
  stsClient.setServiceName("{http://tempuri.org/}Service");
  stsClient.setEndpointName("{http://tempuri.org/}Service_Port");

  stsClient.setKeySize(192);

  stsClient.getInInterceptors().add(new LoggingInInterceptor());
  stsClient.getOutInterceptors().add(new LoggingOutInterceptor());

  stsClient.setTokenType("http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1");
  stsClient.setSoap12();

  // Set the STS Client on the bus
  bus.setProperty(SecurityConstants.STS_CLIENT, stsClient);

  BusFactory.setDefaultBus(bus);
  BusFactory.setThreadDefaultBus(bus);

  MyService myService = new MyService();
  IMyService myServicePort = myService.getCustomBindingIMyService();

  Map<String, Object> ctx = ((BindingProvider)myServicePort).getRequestContext();
  ctx.put(SecurityConstants.ENCRYPT_CRYPTO, merlin);
  ctx.put(SecurityConstants.SIGNATURE_CRYPTO, merlin);
  ctx.put(SecurityConstants.IS_BSP_COMPLIANT, "false");

  myServicePort.doSomething();
Run Code Online (Sandbox Code Playgroud)

任何见解表示赞赏。

Wil*_*ren 3

所以我最终从 cxf 用户邮件列表中得到了一些帮助。

答复如下:

我建议您在这里做的是在 CXF 中子类化 STSClient:

https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=rt/ws/security/src/main/java/org/apache/cxf/ws/安全/信任/STSClient.java;h=afdaaeaa092460c5cbccd3f9723660ded9f12e2b;hb=HEAD

特别是,您想在此处重写“issue”方法:

https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=rt/ws/security/src/main/java/org/apache/cxf/ws/安全/信任/AbstractSTSClient.java;h=4b4630e9d3fe0afab4496de0c7b0dd5df2fca292;hb=HEAD

只需复制现有的方法代码+在最后添加您自己的自定义代码即可。

果然,我对 进行了子类化STSClient,并且能够复制粘贴覆盖该issue方法,该方法使您可以访问W3CDOMStreamWriter包含 的方法RequestSecurityToken,因此我能够通过writer.writeStartElement(...)在最终 之前执行更多操作等来进行我想要的任何修改writer.writeEndElement()