问题
I'm new to Apache Camel and CXF,
I'm trying to create a route for querying a remote WS which requires Basic Authentication and to specify the Soap Action header.
I was able to achieve the same using camel HTTP component but i needed the same with camel CXF in java DSL
Can anyone guide us in fixing the same
回答1:
If you want to use camel-cxf component to setup the Basic authentication, you need do some configuration on the CxfEndpoint just like this.
CxfEndpoint cxfEndpoint = camelContext.getEndpoint(“cxf:xxx”);
// set the authentication information
Map<String, Object> properties = new HashMap<String, Object>();
org.apache.cxf.configuration.security.AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setUserName(username);
authPolicy.setPassword(password);
properties.put(AuthorizationPolicy.class.getName(), authPolicy);
cxfEndpoint.setProperties(properties);
from(“xxx”).to(cxfEndpoint);
回答2:
With @Willem's help, was able to make this working. The authentication credentials need to passed to the CXF Endpoint in the Route Builder rather than in the Processor. This is as explained by Williem on Camel forum:
If you set the cxfEndpoint property in a processor, it’s a setting of runtime. As the CxfProducer is created during the camel context start the route, the cxfEndpoint’s property is >not updated.
So, to fix this add the following code to the Route Builder:
Map<String, Object> properties = new HashMap<String, Object>();
AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_BASIC);
authPolicy.setUserName(USERNAME);
authPolicy.setPassword(PWD);
authPolicy.setAuthorization("true");
//properties.put(AuthorizationPolicy.class.getName(), authPolicy);
properties.put("org.apache.cxf.configuration.security.AuthorizationPolicy", authPolicy);
CxfEndpoint myCxfEp = (CxfEndpoint)getContext().getEndpoint("cxf://");
myCxfEp.setProperties(properties);
Also, in version 2.12.3 of Apache Camel is introducing username and password options for basic authentication.
回答3:
In current versions of camel-cxf it should be sufficient to set username and password directly on CxfEndpoint:
cxfEndpoint.setUsername("xyz");
csfEndpoint.setPassword("verySecure");
I just looked into the code of CxfEndpoint and found:
// setup the basic authentication property
if (ObjectHelper.isNotEmpty(username)) {
AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setUserName(username);
authPolicy.setPassword(password);
factoryBean.getProperties().put(AuthorizationPolicy.class.getName(), authPolicy);
}
So if you set username, basic auth will be configured as shown in other answers.
来源:https://stackoverflow.com/questions/20948182/how-to-configure-camel-cxf-with-basic-authentication