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

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

发soap消息格式如下:



abcd
1234




214484f8-8a79-464a-9fcb-27491d219ce3
1
10





(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 {
private String _userName;
private String _password;

@Override
public Set 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 getHandlerChain(PortInfo portInfo) {
List handlerChain = new ArrayList();

//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

 

你可能感兴趣的:(客户端调用有安全认证的Webservice,可用messageHander 修改 SoapHeader)