我正在使用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证书
那我该怎么办呢?我发现了一些关于 PolicyBasedWSS4JOutInterceptor 和 PolicyBasedWSS4JInInterceptor 的信息,但不知道它们是如何工作的。
非常感谢!
最佳答案
您需要由您的服务器接受的 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/