java - 如何更改wsse :KeyIdentifier to wsse:Reference with CXF

标签 java web-services cxf

我正在使用CXF 3.1.5,尝试将请求发送到STS服务器,即STS strong>服务器有一个策略,相关部分如下

<wsp:Policy>
    <sp:RequireThumbprintReference />
    <sp:WssX509V3Token10 />
</wsp:Policy>

因此,在发送到 STS 服务器的请求 CXF 中,签名 key 如下所示:

<wsse:SecurityTokenReference wsu:Id="...">
    <wsse:KeyIdentifier EncodingType="..."ValueType="...#ThumbprintSHA1">...</wsse:KeyIdentifier>
</wsse:SecurityTokenReference>

但是我想将SecurityTokenReference更改为

<wsse:SecurityTokenReference>
    <wsse:Reference URI="..." ValueType="...#**X509v3**"/>
</wsse:SecurityTokenReference>

它指的是BinarySecurityToken,它是X.509证书

那我该怎么办呢?我发现了一些关于 PolicyBasedWSS4JOutInterceptorPolicyBasedWSS4JInInterceptor 的信息,但不知道它们是如何工作的。

非常感谢!

最佳答案

您需要由您的服务器接受的 CA 颁发的证书来签署 SOAP 请求。您可以将 CXF 与 WSS4JOutInterceptor

结合使用

这是将 WSS4J 与 CXF 结合使用的配置。省略创建 keystore 的部分

设置WSS4J properties (根据您的政策进行调整)

outProps.put("action", "Signature");
outProps.put("user", certificateAlias);
outProps.put("passwordType", "PasswordText");
outProps.put("signaturePropFile", propertiesFile);
outProps.put("signatureKeyIdentifier","DirectReference");
outProps.put("passwordCallbackRef",clientPasswordCallback);

propertiesFile 包含属性文件的路径,其中配置了 keystore 的路径

org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=changeit
org.apache.ws.security.crypto.merlin.file=keystore.jks

ClientPasswordCallback 从 wss4j 执行回调以获取证书的密码

public class ClientPasswordCallback implements CallbackHandler {
   String password = ...; //Configure the password

   public void handle(Callback[] callbacks) {
     for (int i = 0; i < callbacks.length; i++) {
        WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
        pc.setPassword(password);
      }
   }
}

使用 WebService 端口配置 CXF 客户端

Client client = ClientProxy.getClient(port);
Endpoint cxfEndpoint = client.getEndpoint();
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssOut);

//Include LoggingOutInterceptor to log the soap message
cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());

最后调用您的服务

port.myService();   

关于java - 如何更改wsse :KeyIdentifier to wsse:Reference with CXF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987591/

相关文章:

java - 参数化类 'ABC' 的原始使用

Java方法重载参数管理

java - 如何在 Java 中循环遍历 ArrayList 并检查它是否包含另一个 ArrayList 中的值

eclipse - org.jboss.as.server.deployment.DeploymentUnitProcessingException : Apache CXF library detected in ws endpoint deployment

Java 矩阵对角差 bug

javascript - 从另一个域的服务器读取 cookie

java - 当响应迟到时,Cxf 客户端花费的时间太长

spring - 在命名空间内使用 Spring 配置文件

jax-ws - 使用 JAX-RS + JAX-WS 的 CXF 和 Google Guice

javascript - 如何使用客户端java脚本显示通过web服务接收的最后N条记录?