首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

客户端调用有安全认证的Webservice,可用messageHander 批改 SoapHeader

2012-08-31 
客户端调用有安全认证的Webservice,可用messageHander 修改 SoapHeader描述:最近做一个项目,需要调用webse

客户端调用有安全认证的Webservice,可用messageHander 修改 SoapHeader

描述:
最近做一个项目,需要调用webservice,服务端用xfire发布,客户端我打算用jdk的wsimport生成,xfire在发布服务时在SoapHeader里增加了安全认证, 客户端在调用时必须将认证参数封装到SoapHeader中才能通过认证。

发soap消息格式如下:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<AuthenticationToken>
<Username>abcd</Username>
<Password>1234</Password>
</AuthenticationToken>
</S:Header>
<S:Body>
<ns2:getUserList xmlns:ns2="http://www.application.org/jsr181"?xmlns:ns3="http://bean.chinamobile.cn.com">
<requestId>214484f8-8a79-464a-9fcb-27491d219ce3</requestId>
<pn>1</pn>
<ps>10</ps>
</ns2:getUserList>
</S:Body>
</S:Envelope>


(1)环境准备
jdk: 1.6.25(没有用任何框架)

(2)创建RequesterCredentials.java(通过实现SoapHander往SoapMessage加SoapHeader)

package com.chinamobile.webservice.omae;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

/**
*
* @author mark
* VanceInfo
*/
public class RequesterCredentials implements SOAPHandler<SOAPMessageContext> {
private String _userName;
private String _password;

@Override
public Set<QName> getHeaders() {
return null;
}

@Override
public void close(MessageContext context) {
}

@Override
public boolean handleFault(SOAPMessageContext context) {
//TODO return true
return true;
}

@Override
public boolean handleMessage(SOAPMessageContext context) {
addRequesterCredentials(context);
//TODO return true
return true;
}

private void addRequesterCredentials(SOAPMessageContext context) {
Boolean outboundProperty = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if (outboundProperty.booleanValue()) {
SOAPMessage message = context.getMessage();
try {
SOAPHeader header = message.getSOAPHeader();
if (header == null) {
message.getSOAPPart().getEnvelope().addHeader();
header = message.getSOAPHeader();
}

SOAPElement authenticationToken = header.addChildElement("AuthenticationToken","","");
SOAPElement userName = authenticationToken.addChildElement("Username").addTextNode(_userName);
SOAPElement password = authenticationToken.addChildElement("Password").addTextNode(_password);

//message.writeTo(System.out);

} catch (Exception e) {
e.printStackTrace();
}
}
}

RequesterCredentials(){
_userName = "abcd";
_password = "1234";
}

RequesterCredentials(String userName,String password){
_userName = userName;
_password = password;
}
}

(3)创建HeaderHandlerResolver.java(调用RequesterCredentials)

package com.chinamobile.webservice.omae;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;

public class HeaderHandlerResolver implements HandlerResolver {

@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();

//add header auth
handlerChain.add(new RequesterCredentials());

return handlerChain;
}
}

(3)webserivce client 端调用

//ClientFactory
public class ClientFactory {
??? public static UserService getUserServiceClient(){
try {
UserService_Service userService_Service =new UserService_Service();
userService_Service.setHandlerResolver(new HeaderHandlerResolver());

UserService userServiceClient = userService_Service.getUserServiceHttpPort();

return userServiceClient;

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

? ? }
}

参照资料:
(1)http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
(2)${jdk_1.6.25}\sample\webservices

?

热点排行