WebService之XFire和Jax实现身份验证

只列出部分核心代码, 其它代码都是myeclipse生成, 详见附件.

 

1.服务器代码:

HelloWorld.java代码:

 

 

package test;

public class HelloWorld implements IHelloWorld {

	public Float add(float a, float b) {
		return a + b;
	}

	public String sayHello(String name) {
		return "hello, " + name + "...";
	}

}

 IHelloWorld.java代码:

 

package test;

import java.rmi.Remote;

public interface IHelloWorld extends Remote{
	
	public String sayHello(String name);
	
	public Float add(float a, float b);
}

 ServerHandler.java代码:

 

package test;

import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;

public class ServerHandler extends AbstractHandler {
	public void invoke(MessageContext message) throws Exception {
		Element header = message.getInMessage().getHeader();
		if (header == null) {
			throw new XFireRuntimeException("Missing SOAP header...");
		}
		Element element = header.getChild("AuthenticationToken");
		String userName = element.getChild("userName").getValue();
		String password = element.getChild("password").getValue();
		System.out.println("userName:" + userName + ",password" + password);
		if ("wander".equals(userName) && "wander".equals(password)) {
			System.out.println("yes, you passed...");
		} else {
			throw new XFireRuntimeException("error password and username...");
		}
	}

}

 services.xml内容:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

	<service>
		<name>HelloWorldSimple</name>
		<namespace>http://simple.xfire.webservice/HelloWorld</namespace>
		<serviceClass>test.IHelloWorld</serviceClass>
		<implementationClass>test.HelloWorld</implementationClass>
		<scope>request</scope>
		<inHandlers>
			<handler handlerClass="test.ServerHandler">
			</handler>
		</inHandlers>
	</service>
</beans>

 

2.XFire实现身份验证

ClientMain2.java代码:

 

package test;

import java.util.List;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxy;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.jdom.Element;

public class ClientMain2 
extends AbstractHandler{
	
	public static void main(String[] args) throws MalformedURLException {
		ClientMain2 
auth = new ClientMain2
();
		String url = "http://localhost:8080/msg/services/HelloWorldSimple";
		Service service = new ObjectServiceFactory().create(IHelloWorld.class);
		IHelloWorld helloWorld = (IHelloWorld) new XFireProxyFactory().create(service, url);
		
		XFireProxy xfireProxy = (XFireProxy) Proxy.getInvocationHandler(helloWorld);
		Client client = xfireProxy.getClient();
		client.addOutHandler(auth);
		
		System.out.println(helloWorld.sayHello("wander"));
	}

	public void invoke(MessageContext message) throws Exception {
		Element header = new Element("header");
		Element authentication = new Element("AuthenticationToken");
		
		Element userName = new Element("userName");
		userName.setText("wander");
		
		Element password = new Element("password");
		password.setText("wander");
		
		authentication.addContent(userName);
		authentication.addContent(password);
		header.addContent(authentication);
		message.getOutMessage().setHeader(header);
	}

}

 

3.Jax-ws 实现身份验证

ClientMain.java代码

 

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.PortInfo;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class ClientMain {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		HelloWorldSimple simple = new HelloWorldSimple();

		simple.setHandlerResolver(new HandlerResolver() {
			public List<Handler> getHandlerChain(PortInfo portInfo) {
				List<Handler> list = new ArrayList<Handler>();
				list.add(new MySOAPHandler());
				return list;
			}
		});
		HelloWorldSimplePortType port = simple.getHelloWorldSimpleHttpPort();
		System.out.println(port.sayHello("cat1"));
	}
}

class MySOAPHandler implements SOAPHandler<SOAPMessageContext> {

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

	public void close(MessageContext context) {
	}

	public boolean handleFault(SOAPMessageContext context) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public boolean handleMessage(SOAPMessageContext context) {
		Boolean outboundProperty = (Boolean) context
				.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if (outboundProperty.booleanValue()) {
			SOAPMessage message = context.getMessage();
			try {
				SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
				SOAPFactory factory = SOAPFactory.newInstance();
				SOAPElement authenticationToken = factory
						.createElement("AuthenticationToken");
				SOAPElement userName = factory.createElement("userName");
				userName.setValue("wander");
				SOAPElement password = factory.createElement("password");
				password.setValue("wander");
				authenticationToken.addChildElement(userName);
				authenticationToken.addChildElement(password);
				
				SOAPHeader header = envelope.addHeader();
				header.addChildElement(authenticationToken);
				
			} catch (SOAPException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值