cxf+Spring的webservice应用--拦截器

接上篇:cxf+Spring的webservice应用



为客户端添加出拦截器:

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 MyInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	private String name;
	private String password;
	
	
	public MyInterceptor() {
		super(Phase.PRE_PROTOCOL);
		System.out.println("client..MyInterceptor()_1......");
	}


	public MyInterceptor(String name,String password) {
		/**
		 * 此處中有 Phase.PRE_PROTOCOL 時才調用  handleMessage(SoapMessage arg0)
		 * 如果是  Phase.PRE_INVOKE 則不調用  handleMessage(SoapMessage arg0)
		 */
		super(Phase.PRE_PROTOCOL);
		this.name=name;
		this.password=password;
		System.out.println("client..MyInterceptor()_2......");
	}
	
	
	@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().....");
		
		
	}

}

配置拦截器:bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://cxf.apache.org/jaxws 
		http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="UserClient" serviceClass="com.xh.ws.sei.SayHello" address="http://localhost:8080/ws_spring_s/userProcess">
        <jaxws:outInterceptors>
        	<bean class="com.xh.ws.interceptor.MyInterceptor">
        		<constructor-arg name="name" value="lina"></constructor-arg>
        		<constructor-arg name="password" value="123123"></constructor-arg>
        	</bean>
        </jaxws:outInterceptors>          
                  
                  
</jaxws:client>
</beans>



为服务端添加拦截器:

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 MyInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	private String name;
	private String password;
	
	public MyInterceptor() {
		super(Phase.PRE_PROTOCOL);
		System.out.println("server..MyInterceptor()......");
	}


	@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("lina".equals(name_1)&&"123123".equals(password_1))
			{
				return;
			}else
			{
				throw new RuntimeException("用户名或密码不正确!");
			}
			
			
		}
		
	}

}


配置拦截器:bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://cxf.apache.org/jaxws 
		http://cxf.apache.org/schemas/jaxws.xsd">
   <import resource="classpath:META-INF/cxf/cxf.xml" /> 
   
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
   <jaxws:endpoint 
     id="UserProcess" 
     implementor="com.xh.ws.sei.SayHelloImpl" 
     address="/userProcess" >
     	<jaxws:inInterceptors>
     		<bean class="com.xh.ws.interceptor.MyInterceptor">
     		
     		</bean>
     	
     	</jaxws:inInterceptors>
     
     
     </jaxws:endpoint>
     
</beans>




上一篇:IT巨头争抢云计算市场 A股公司“分羹”上下游


下一篇:CentOS 6.3 + Subversion + Usvn 搭建版本管理服务器