Am trying to call third party web service using java apache CXF. I created the proxy using CXF apache plugin. The service is protected using X509 Authentication, Signature and Encryption.
When I call the service, am getting the below exception.
Apache CXF Policy Exception Reference to policy "X509 Authentication, Signature and Encryption" could not be resolved
This is what I tried sofar..
ServiceEnq service=new ServiceEnq(new
URL("https://.....Inquiry?wsdl"));
System.out.println("Line2 scuccess!");
InquiryPortType port=service.getInquiryPort();
Client client = ClientProxy.getClient(port);
org.apache.cxf.endpoint.Endpoint endpoint = client.getEndpoint();
HashMap<String, Object> outProps = new HashMap<String, Object>();
outProps.put(WSHandlerConstants.ACTION,
"UsernameToken Timestamp Signature Encryption");
outProps.put(WSHandlerConstants.USER, "username1");
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordCallbackHandler.class.getName());
outProps.put(WSHandlerConstants.ENCRYPTION_USER, "public1");
outProps.put(WSHandlerConstants.ENC_PROP_FILE, "publicProp.properties");
outProps.put(WSHandlerConstants.SIGNATURE_USER, "pk");
outProps.put(WSHandlerConstants.SIG_PROP_FILE, "pkProp.properties");
outProps.put("timeToLive", "30");
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
endpoint.getOutInterceptors().add(wssOut);
HashMap<String, Object> inProps = new HashMap<>();
inProps.put(WSHandlerConstants.ACTION, "Encryption Signature Timestamp");
inProps.put(WSHandlerConstants.DEC_PROP_FILE, "publicProp.properties");
inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordCallbackHandler.class.getName());
inProps.put(WSHandlerConstants.SIG_PROP_FILE, "pkProp.properties");
WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
endpoint.getInInterceptors().add(wssIn);
ObjectFactory fact=new ObjectFactory();
InquiryRequest request=fact.createInquiryRequest();
MessageHeaderIn headerIn=fact.createMessageHeaderIn();
// removed input parameters
// getting error in this line...
InquiryResponse2 res= port.Inquiry(request);
I have manually created the XML Document
and used wss4j
to encrypt and sign XML document. Then uploaded the generated SOAP envelope to HttpURLConnection
using POST method.
Here is the java code to sign and encryption. I have avoided other code as this is more tricky part..
public void addSignature(org.w3c.dom.Document doc) {
try {
Properties properties = new Properties();
properties.put("org.apache.ws.security.crypto.merlin.keystore.password", SecurityConstants.JKSPassword);
properties.put("org.apache.ws.security.crypto.merlin.keystore.file",
SecurityConstants.ClientCert);
properties.put("org.apache.ws.security.crypto.merlin.keystore.alias",
SecurityConstants.ClientKeyAliasName);
Merlin keyMaterialCrypto = new Merlin(properties);
WSSecSignature wssSign = new WSSecSignature();
wssSign.setUserInfo(SecurityConstants.ClientKeyAliasName, SecurityConstants.JKSPassword);
wssSign.setKeyIdentifierType(WSConstants.X509_KEY_IDENTIFIER);
wssSign.setUseSingleCertificate(false);
org.apache.ws.security.message.WSSecHeader secHeader = new WSSecHeader();
secHeader.setMustUnderstand(false);
secHeader.insertSecurityHeader(doc);
wssSign.build(doc, keyMaterialCrypto, secHeader);
} catch (Exception e) {
e.printStackTrace();
System.out.print(e.toString());
}
}
public void addEnccryption(Document doc) {
StringWriter writer = null;
try {
Properties properties = new Properties();
properties.put("org.apache.ws.security.crypto.merlin.keystore.password", SecurityConstants.JKSPassword);
properties.put("org.apache.ws.security.crypto.merlin.keystore.file",
SecurityConstants.ServerCer);
properties.put("org.apache.ws.security.crypto.merlin.keystore.alias",
SecurityConstants.ServerAliasName);
Merlin crypto = new Merlin(properties);
WSSecEncrypt wsEncryption = new WSSecEncrypt();
WSSConfig wssConfig = WSSConfig.getNewInstance();
wsEncryption.setWsConfig(wssConfig);
wsEncryption.setUserInfo(SecurityConstants.ServerAliasName);
wsEncryption.setKeyIdentifierType(WSConstants.X509_KEY_IDENTIFIER);
wsEncryption.setEncryptSymmKey(true);
org.apache.ws.security.message.WSSecHeader secHeader = new WSSecHeader();
secHeader.setMustUnderstand(false);
secHeader.insertSecurityHeader(doc);
wsEncryption.build(doc, crypto, secHeader);
} catch (Exception e) {
e.printStackTrace();
}
}