web service 自定义拦截器-阿里云开发者社区

开发者社区> 大数据> 正文

web service 自定义拦截器

简介:

客户端拦截器:

package com.xh.ws.interceptor;

import java.util.List;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;



public class LoginInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	private String name;
	private String password;
	
	
	public LoginInterceptor() {
		super(Phase.PRE_PROTOCOL);
	}
	
	public LoginInterceptor(String name,String password) {
		super(Phase.PRE_PROTOCOL);
		this.name=name;
		this.password=password;
	
	}

	@Override
	public void handleMessage(SoapMessage arg0) throws Fault {
		/**
		 * 客户端的head
		 * <user>
		 * 		<name>zhangsan</name>
		 * 		<password>zhangsan</password>
		 * </user>
		 * 	
		 */
			List<Header> heads=arg0.getHeaders();
			
			Document document=DOMUtils.createDocument();
			Element rootEle=document.createElement("user");
			Element nameEle=document.createElement("name");
			nameEle.setTextContent(name);
			Element passwordEle=document.createElement("password");
			passwordEle.setTextContent(password);
			rootEle.appendChild(nameEle);
			rootEle.appendChild(passwordEle);
			heads.add(new Header(new QName("user"), rootEle));
			System.out.println("Client:handleMessage().....");
			
	}

}

客户端测试代码:

package com.xh.ws.test;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

import com.xh.ws.interceptor.LoginInterceptor;
import com.xh.ws.sei.SayHello;
import com.xh.ws.sei.SayHelloImplService;

public class ClientTest {

	public static void main(String[] args) {
		SayHelloImplService factory=new SayHelloImplService();
		SayHello hello=factory.getSayHelloImplPort();
		Client client=ClientProxy.getClient(hello);
		//添加自定义拦截器
		client.getOutInterceptors().add(new LoginInterceptor("zhangsan","123456"));
		//添加日志拦截器
		client.getOutInterceptors().add(new LoggingOutInterceptor());
		System.out.println(hello.sayHello("lili"));
	}
}

服务端拦截器:

package com.xh.ws.interceptor;

import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;



public class LoginInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	public LoginInterceptor() {
		super(Phase.PRE_INVOKE);
	}

	@Override
	public void handleMessage(SoapMessage arg0) throws Fault {
		
		Header header=arg0.getHeader(new QName("user"));
		if(header!=null)
		{
			/**
			 * 解析header
			 */
			Element rootEle=(Element) header.getObject();
			Element name=(Element) rootEle.getElementsByTagName("name").item(0);
			Element password=(Element) rootEle.getElementsByTagName("password").item(0);
			String name_1=name.getTextContent();
			String password_1=password.getTextContent();
			
			if("zhangsan".equals(name_1)&&"123456".equals(password_1))
			{
				return;
			}else
			{
				throw new RuntimeException("用户名或密码不正确!");
			}
			
			
			
		}
		
	}

}

服务端测试代码:

package com.xh.ws.test;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;

import com.xh.ws.interceptor.LoginInterceptor;
import com.xh.ws.sei.SayHelloImpl;

public class ServerTest {

	public static void main(String[] args) {
		Endpoint endpoint=Endpoint.publish("http://127.0.0.1:54321", new SayHelloImpl());
		EndpointImpl endpointImpl=(EndpointImpl) endpoint;
		//添加自定义拦截器
		endpointImpl.getInInterceptors().add(new LoginInterceptor());
		//添加日志拦截器
		endpointImpl.getInInterceptors().add(new LoggingInInterceptor());
		System.out.println("发布成功");
	}
}


结果:

当正确时:

Client:handleMessage().....
三月 01, 2016 4:14:11 下午 org.apache.cxf.services.SayHelloImplService.SayHelloImplPort.SayHello
信息: Outbound Message
---------------------------
ID: 1
Address: http://127.0.0.1:54321/
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><user><name>zhangsan</name><password>123456</password></user></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://sei.ws.xh.com/"><arg0>lili</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
lili

错误时:

Client:handleMessage().....
三月 01, 2016 4:37:17 下午 org.apache.cxf.services.SayHelloImplService.SayHelloImplPort.SayHello
信息: Outbound Message
---------------------------
ID: 1
Address: http://127.0.0.1:54321/
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><user><name>zhangsan</name><password>1234561</password></user></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://sei.ws.xh.com/"><arg0>lili</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: 用户名或密码不正确!
	at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:161)
	at com.sun.proxy.$Proxy34.sayHello(Unknown Source)
	at com.xh.ws.test.ClientTest.main(ClientTest.java:21)
Caused by: org.apache.cxf.binding.soap.SoapFault: 用户名或密码不正确!


相关参考资料:

http://huiseyiyu.iteye.com/blog/1172160




版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
大数据
使用钉钉扫一扫加入圈子
+ 订阅

大数据计算实践乐园,近距离学习前沿技术

其他文章