我收到了包含安全策略的WSDL,就目前而言,根据wsdl的判断,我发现这意味着我需要它与证书签名。不应使用令牌服务器。
我做什么: 我使用cxf wsdl2java从wsdl文件创建Java类。到目前为止,我看到CXF可以很好地自动处理所有签名,因此我在Java端STSClient上添加了Client上的ws-security.signature.crypto和ws-security.encryption.crypto。基本上什么都不做,我的Soap请求只包含主体,没有任何签名数据。结果什么也没发生,也没有错误打印出来,这真的很难解决我的问题。
问题: 如何在没有令牌服务器且没有用户名/密码的情况下签署Soap请求? 此外,提供的wsdl中的地址是http,我应该将其覆盖为https吗?
收到的wsdl政策摘要:
<wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
wsu:Id="SigOnly">
<wsp:ExactlyOne>
<wsp:All>
<sp:AsymmetricBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:InitiatorToken>
<wsp:Policy>
<sp:X509Token
sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Always">
<wsp:Policy>
<sp:WssX509V3Token10/>
</wsp:Policy>
</sp:X509Token>
</wsp:Policy>
</sp:InitiatorToken>
<sp:RecipientToken>
<wsp:Policy>
<sp:X509Token
sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Always">
<wsp:Policy>
<sp:WssX509V3Token10/>
</wsp:Policy>
</sp:X509Token>
</wsp:Policy>
</sp:RecipientToken>
<sp:AlgorithmSuite>
<wsp:Policy>
<sp:TripleDesRsa15/>
</wsp:Policy>
</sp:AlgorithmSuite>
<sp:Layout>
<wsp:Policy>
<sp:Strict/>
</wsp:Policy>
</sp:Layout>
<sp:IncludeTimestamp/>
<sp:OnlySignEntireHeadersAndBody/>
</wsp:Policy>
</sp:AsymmetricBinding>
<sp:Wss10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:MustSupportRefKeyIdentifier/>
<sp:MustSupportRefIssuerSerial/>
</wsp:Policy>
</sp:Wss10>
<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<sp:Body/>
</sp:SignedParts>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
收到的邮件标题示例:
<soap:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
soap:mustUnderstand="1">
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SIG-****">
<ds:SignedInfo>
<ds:Canonicalizationmethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="soap"/>
</ds:Canonicalizationmethod>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#id-***">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:Digestvalue>value=</ds:Digestvalue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
signatureValue==
</ds:SignatureValue>
<ds:KeyInfo Id="KI-***">
<wsse:SecurityTokenReference wsu:Id="STR-***">
<ds:X509Data>
<ds:X509IssuerSerial>
<ds:X509IssuerName>issuer name
</ds:X509IssuerName>
<ds:X509SerialNumber>nmber</ds:X509SerialNumber>
</ds:X509IssuerSerial>
</ds:X509Data>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
</wsse:Security>
</soap:Header>
我的Java代码段:
Client client = ClientProxy.getclient(generatedService);
Init.init();
Properties properties = new Properties();
properties.setProperty("org.apache.ws.security.crypto.provider","org.apache.ws.security.components.crypto.Merlin");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.type","jks");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password",keystorePassword);
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias",signedCertificateAliasInTheKeystore);
properties.setProperty("org.apache.ws.security.crypto.merlin.file",new ClasspathResource(keyStore).getPath());
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.private.password",keystorePassword);
Merlin crypto = null;
try {
crypto = (Merlin) CryptoFactory.getInstance(properties);
}
catch (WSSecurityException e) {
e.printStackTrace();//temp
}
STSClient stsClient = new STSClient(((EndpointImpl) client.getEndpoint()).getBus());
Map<String,Object> stsClientProperties = new HashMap<>();
stsClient.setWsdlLocation(new ClasspathResource("MyWsdl.wsdl").getPath());
stsClientProperties.put("ws-security.signature.crypto",crypto);
stsClientProperties.put("ws-security.encryption.crypto",crypto);
stsClient.setProperties(stsClientProperties);
BindingProvider bindingProvider = (BindingProvider) generatedService;
bindingProvider.getRequestContext().put("ws-security.sts.client",stsClient);
generatedService.callToTheOuterWorld(data);
第二次尝试,基本上是 link:
org.apache.cxf.endpoint.Client client = ClientProxy.getclient(generatedService);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
Map<String,Object> outProps = new HashMap<String,Object>();
outProps.put(WSHandlerConstants.USER,"keystoreAlias");
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,ClientPasswordCallback.class.getName()); //callback has keystore password
outProps.put(WSHandlerConstants.actION,WSHandlerConstants.TIMESTAMP + " " + WSHandlerConstants.SIGNATURE + " " + WSHandlerConstants.ENCRYPT);
outProps.put(WSHandlerConstants.SIG_PROP_FILE,new ClasspathResource("client_sign.properties").getPath());
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssOut);
generatedService.callToTheOuterWorld(data)
引发的:原因:org.apache.wss4j.common.ext.WSSecurityException:EncryptionCrypto实例化失败 如果我删除了WSHandlerConstants.ENCRYPT,我没有任何异常,但请求中仍然缺少标头。