2014-01-06 71 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); 
} 

所以如果您設置了用戶名,則基本身份驗證將按照其他答案中的顯示進行配置。