Webservice交互經(jīng)常需要驗(yàn)證用戶,用戶名和密碼的傳遞采用SOAPHeader傳遞不失為一種好辦法。在Axis1中設(shè)置很簡單:
客戶端:
((org.apache.axis.client.Call) call).addHeader(new SOAPHeaderElement("Authorization","username",username));
((org.apache.axis.client.Call) call).addHeader(new SOAPHeaderElement("Authorization","password",password));
經(jīng)包裝后傳遞的內(nèi)容如下
<soapenv:Header>
<ns1:username
soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next"
soapenv:mustUnderstand="0" xsi:type="soapenc:string"
xmlns:ns1="Authorization"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
admin
</ns1:username>
<ns2:password
soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next"
soapenv:mustUnderstand="0" xsi:type="soapenc:string"
xmlns:ns2="Authorization"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
1
</ns2:password>
</soapenv:Header>
服務(wù)端通過Handler取得用戶名和密碼進(jìn)行驗(yàn)證:
username = (String) messageContext.getRequestMessage().getSOAPEnvelope()
.getHeaderByName("Authorization","username").getValue();
password = (String) messageContext.getRequestMessage().getSOAPEnvelope()
.getHeaderByName("Authorization","password").getValue();
如果覺得這樣不安全,可雙方約定一種加密解密規(guī)則,將用戶名和密碼加密后進(jìn)行傳輸。
我曾試過使用如下方法,
客戶端:
((org.apache.axis.client.Call) call).setUsername(username);
((org.apache.axis.client.Call) call).setPassword(password);
包裝后傳遞內(nèi)容(多了最后一句:Authorization: Basic emphZG1pbjox。Axis將用戶名和密碼經(jīng)Base64加密后傳遞):
POST /web/services/GenericServer HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.4
Host: localhost:8083
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 807
Authorization: Basic emphZG1pbjox
服務(wù)端的Handle:
username =messageContext.getUsername();
password = messageContext.getPassword();
這樣是沒問題,看起來更簡單。可惜調(diào)用部署在weblogic上的ws時(shí),會被weblogic攔截,必須在weblogic安全域中配置相應(yīng)的用戶才能通過驗(yàn)證,這不是我們所需要的,通常我們有自己的用戶管理機(jī)制,調(diào)用WS的用戶也作為系統(tǒng)中的一個(gè)用戶納入我們的管理,而不是跟weblogic安全域用戶綁在一起。
posted on 2008-07-18 13:18
jinn 閱讀(5935)
評論(1) 編輯 收藏 所屬分類:
Jave/Webservice