How to load cxf wss4j crypto properties file from external location other than classpath
I am trying to elicit the crypto.properties files required to sign SOAP CXF request messages. According to the structure, it should have a properties file on the classpath. I cannot load it from external. Please help me, I have tried many methods.
I am getting below exception
org.apache.ws.security.WSSecurityException: general security error (unable to load resource file:
it is very necessary to externalize the file as we have synchronized our development code base and production environment
The CXF structure used is 2.6.10
source to share
As noted in Colm O hEigeartaigh's answer, it is possible to load the config settings from an external file using the latest CXF and WSS4J. However, this still means that you need to write the properties to a file and load them again.
You can also create an in-memory Properties object and use CXF. This also works for older versions of CXF. This is done by extending WSS4JInInterceptor
and WSS4JOutInterceptor
, then overriding the method Crypto loadCryptoFromPropertiesFile(String propFilename, RequestData reqData)
and simply returning your own object Crypto
that you can create with CryptoFactory.getInstance(properties)
.
So something like:
Properties cxfProps = new Properties();
cxfProps.setProperty("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
cxfProps.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "jks");
cxfProps.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", "client");
cxfProps.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", PASSWORD);
cxfProps.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", "keystore.j2");
Crypto crypto = CryptoFactory.getInstance(cxfProps);
Map<String, Object> inProps = new HashMap<String, Object>();
Map<String, Object> outProps = new HashMap<String, Object>();
inProps.put(WSHandlerConstants.ACTION, "Signature");
inProps.put(WSHandlerConstants.SIG_PROP_FILE, "dummy_value"); // Only necessary to avoid NPE
outProps.put(WSHandlerConstants.ACTION, "Signature");
outProps.put(WSHandlerConstants.USER, "client");
outProps.put(WSHandlerConstants.SIG_PROP_FILE, "dummy_value"); // Only necessary to avoid NPE
WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps) {
@Override
protected Crypto loadCryptoFromPropertiesFile(String propFilename, RequestData reqData)
throws WSSecurityException {
return crypto;
}
};
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps) {
@Override
protected Crypto loadCryptoFromPropertiesFile(String propFilename, RequestData reqData)
throws WSSecurityException {
return crypto;
}
};
source to share