Webservice交互经常需要验证用户,用户名和密码的传递采用SOAPHeader传递不失为一种好办法。在Axis1中设置很简单:
客户端:
((org.apache.axis.client.Call) call).addHeader(new SOAPHeaderElement("Authorization","username",username));
((org.apache.axis.client.Call) call).addHeader(new SOAPHeaderElement("Authorization","password",password));
经包装后传递的内容如下
<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>
服务端通过Handler取得用户名和密码进行验证:
username = (String) messageContext.getRequestMessage().getSOAPEnvelope()
.getHeaderByName("Authorization","username").getValue();
password = (String) messageContext.getRequestMessage().getSOAPEnvelope()
.getHeaderByName("Authorization","password").getValue();
如果觉得这样不安全,可双方约定一种加密解密规则,将用户名和密码加密后进行传输。
我曾试过使用如下方法,
客户端:
((org.apache.axis.client.Call) call).setUsername(username);
((org.apache.axis.client.Call) call).setPassword(password);
包装后传递内容(多了最后一句:Authorization: Basic emphZG1pbjox。Axis将用户名和密码经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
服务端的Handle:
username =messageContext.getUsername();
password = messageContext.getPassword();
这样是没问题,看起来更简单。可惜调用部署在weblogic上的ws时,会被weblogic拦截,必须在weblogic安全域中配置相应的用户才能通过验证,这不是我们所需要的,通常我们有自己的用户管理机制,调用WS的用户也作为系统中的一个用户纳入我们的管理,而不是跟weblogic安全域用户绑在一起。
posted on 2008-07-18 13:18
jinn 阅读(5911)
评论(1) 编辑 收藏 所属分类:
Jave/Webservice