我在使用Apache CXF爲Web服務請求設置HTTP授權頭時遇到了一些麻煩。我有我的客戶端安裝到春季:通過CXF攔截器不工作的HTTP基本身份驗證
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<bean id="myHTTPAuthInterceptor" class="my.app.MyHTTPAuthInterceptor" autowire="constructor" />
<bean id="webServiceFactory" class="my.app.WebServiceFactory">
<property name="wsdlLocation" value="classpath:/my/app/webservice.wsdl" />
<property name="serviceURL">
<jee:jndi-lookup jndi-name="webservice/url" />
</property>
<property name="inInterceptors">
<list>
<ref bean="loggingInInterceptor" />
</list>
</property>
<property name="outInterceptors">
<list>
<ref bean="loggingOutInterceptor" />
<ref bean="myHTTPAuthInterceptor" />
</list>
</property>
</bean>
<bean id="myWebService" factory-bean="webServiceFactory" factory-method="getInstance" />
頁眉通過MyHTTPAuthInterceptor這樣設置:
public MyHTTPAuthInterceptor(ConfigDao configDao)
{
super(Phase.POST_PROTOCOL);
this.configDao = configDao;
}
@Override
public void handleMessage(Message message) throws Fault
{
Map<String, List<?>> headers = (Map<String, List<?>>) message.get(Message.PROTOCOL_HEADERS);
String authString = configDao.getUsername() + ":" + config.getPassword();
headers.put("Authorization", Collections.singletonList("Basic " + new String(Base64.encodeBase64(authString.getBytes()))));
}
使用用戶名和都設定爲「測試」,一切似乎都在日誌中確定:
Headers: {SOAPAction=[""], Accept=[*/*], Authorization=[Basic dGVzdDp0ZXN0]}
但是,服務器返回HTTP 401:未經授權。
不知道發生了什麼問題,我通過更改我的Web服務客戶端工廠代碼採取了其他方法。我加了一個基本的授權策略,以客戶端的管道是這樣的:
HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
authorizationPolicy.setUserName("test");
authorizationPolicy.setPassword("test");
authorizationPolicy.setAuthorizationType("Basic");
httpConduit.setAuthorization(authorizationPolicy);
再次測試我的設置,相同的日誌(不同的順序雖然):
Headers: {SOAPAction=[""], Authorization=[Basic dGVzdDp0ZXN0], Accept=[*/*]}
現在服務器的響應是200 OK!
問題解決了你可能會想到的問題,但第二種方法並不適合我。我的應用程序是一個多租戶環境,全部使用不同的用戶名和密碼。採用第二種方法,我無法重用我的客戶端。
如何讓我的攔截器正常工作?我是否陷入了錯誤的階段?頭文件的順序是否重要?如果是這樣,我該如何改變它?
試了一下,但唉,沒有運氣...... – verhage 2012-11-09 10:34:05
好吧,我得在這裏糾正自己。犯了一個錯誤。它確實有用!非常感謝,您的回答在此被接受:) – verhage 2012-11-09 10:37:45