2014-01-06 74 views
1

我是新来的Apache骆驼和CXF,如何配置骆驼CXF使用基本身份验证

我试图创建一个路由查询这需要基本身份验证,并指定SOAP动作头远程WS。

我能够用骆驼HTTP组件来实现相同的,但我需要用骆驼CXF 同样在Java DSL

任何人都可以指导我们在固定的同一

+2

您可以简单地创建一个POJO来调用该服务并在您的路由中将其用作bean。 POJO本身可能包含f.e. [CXF WebClient](http://cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/client/WebClient.html),它提供了[基本认证的工厂方法](http:// cxf .apache.org /的Javadoc /最新/组织/阿帕奇/ CXF/JAXRS /客户/ WebClient.html#创建%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang。 String%29) –

+0

感谢您的回复,您能否给我们提供一些示例代码来实现相同的例如http组件,我们拥有authMethod = Basic ---> from(“direct:routes”)。 (“http:// localhost/whatever?authMethod = Basic&authUsername = me&authPassword = secret”); CXF webclient – Akshat

回答

2

如果你想使用camel- cxf组件来设置基本身份验证,您需要像这样在CxfEndpoint上进行一些配置。

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); 
1

在@ Willem的帮助下,能够使这项工作。身份验证凭据需要传递到路由构建器中的CXF端点,而不是处理器中。这正如Williem在Camel论坛上所解释的:

如果在处理器中设置cxfEndpoint属性,它是运行时设置。 由于在骆驼上下文中创建CxfProducer启动路由,因此cxfEndpoint的属性不会更新。

因此,要解决这个问题下面的代码添加到路由生成器:

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); 

此外,在版本的Apache骆驼的2.12.3推出了用户名和密码选项基本身份验证。

+0

是的,您可以在CXF端点uri上设置用户名,密码选项,就像在Camel 2.13.x中的“cxf:// bean:myservice?username = user1&password = pass”一样。 。 –

0

在骆驼CXF的当前版本中,它应该足以直接CxfEndpoint设置用户名和密码:

cxfEndpoint.setUsername("xyz"); 
csfEndpoint.setPassword("verySecure"); 

我只是看着CxfEndpoint的代码,发现:

// 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); 
} 

所以如果您设置了用户名,则基本身份验证将按照其他答案中的显示进行配置。