2016-12-30 37 views
0

对于几天,我尝试解决以下问题:的Apache CXF策略:安全配置无法检测(外部政策)

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Security configuration could not be detected. Potential cause: Make sure jaxws:client element with name attribute value matching endpoint port is defined as well as a ws-security.signature.properties element within it. 
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:158) 
    at com.sun.proxy.$Proxy38.getSignedDocument(Unknown Source) 
    at pl.mycompany.epuap.TPSigning_TPSigning_Client.main(TPSigning_TPSigning_Client.java:55) 
Caused by: org.apache.cxf.ws.policy.PolicyException: Security configuration could not be detected. Potential cause: Make sure jaxws:client element with name attribute value matching endpoint port is defined as well as a ws-security.signature.properties element within it. 
    at org.apache.cxf.ws.security.wss4j.policyhandlers.AbstractBindingBuilder.policyNotAsserted(AbstractBindingBuilder.java:315) 
    at org.apache.cxf.ws.security.wss4j.policyhandlers.AbstractBindingBuilder.getSignatureBuilder(AbstractBindingBuilder.java:1851) 
    at org.apache.cxf.ws.security.wss4j.policyhandlers.AsymmetricBindingHandler.doSignature(AsymmetricBindingHandler.java:570) 
    at org.apache.cxf.ws.security.wss4j.policyhandlers.AsymmetricBindingHandler.doSignBeforeEncrypt(AsymmetricBindingHandler.java:149) 
    at org.apache.cxf.ws.security.wss4j.policyhandlers.AsymmetricBindingHandler.handleBinding(AsymmetricBindingHandler.java:98) 
    at org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JOutInterceptor$PolicyBasedWSS4JOutInterceptorInternal.handleMessage(PolicyBasedWSS4JOutInterceptor.java:176) 
    at org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JOutInterceptor$PolicyBasedWSS4JOutInterceptorInternal.handleMessage(PolicyBasedWSS4JOutInterceptor.java:90) 
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272) 
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:572) 
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:481) 
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:382) 
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:335) 
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96) 
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:136) 
    ... 2 more 

发生这种情况时,我尝试调用Web服务:https://pz.gov.pl/pz-services/tpSigning?wsdl与外部(仅供参考)政策:https://pz.gov.pl/pz-services/tpSigning?wsdl=wssec-policies.wsdl

这里是我的代码:

Properties properties = new Properties(); 
properties.put("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin"); 
properties.put("org.apache.ws.security.crypto.merlin.keystore.type",config.getKeystoreType()); 
properties.put("org.apache.ws.security.crypto.merlin.keystore.password",config.getKeystorePass()); 
properties.put("org.apache.ws.security.crypto.merlin.keystore.alias",config.getKeystoreAlias()); 
properties.put("org.apache.ws.security.crypto.merlin.file", config.getKeystoreFile()); 

outProps.put("cryptoProperties",properties); 
outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP + " " + WSHandlerConstants.SIGNATURE); 
//outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE); 
outProps.put(WSHandlerConstants.USER, config.getKeystoreAlias()); 
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordCallbackHandler.class.getName()); 
outProps.put(WSHandlerConstants.SIG_PROP_REF_ID,"cryptoProperties"); 
outProps.put(WSHandlerConstants.SIG_KEY_ID, "DirectReference"); 
outProps.put(WSHandlerConstants.SIGNATURE_PARTS, "{}{http://schemas.xmlsoap.org/soap/envelope/}Body"); 

WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); 
cxfEndpoint.getOutInterceptors().add(wssOut); 

try{ 
    String s = signing.addDocumentToSigning(doc, succesUrl, failureUrl, additionalInfo); 
    return s; 
} 

正如我注意到,该政策并不受CXF引擎加载。我试图通过拦截器加载策略,但效果是一样的。

该问题也出现在2.7.18中,如3.x版本。

任何帮助将不胜感激。

问候 的Mariusz

回答

0

的问题是,你是混合 “行动” 为基础的方法的WS-Security和WS-SECURITYPOLICY驱动方式。您引用的WSDL包含一个安全策略,并且CXF PolicyBasedWSS4JOutInterceptor将自动负责基于此配置安全性。您只需指定一些安全配置选项,例如密钥库。详情请看这里:https://cxf.apache.org/docs/ws-securitypolicy.html