Cxf拦截器

服务器端

拦截器类:

package ws.interceptor;

import java.util.List;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

import org.apache.cxf.aegis.xml.stax.ElementWriter;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
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;
import org.w3c.dom.NodeList;

/**
 * 继承AbstractPhaseInterceptor拦截类
 * @author Administrator
 *
 */
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	private SAAJInInterceptor saa = new SAAJInInterceptor();

	public AuthInterceptor() {
		//显示调用父类有参数构造
		super(Phase.PRE_INVOKE); //该拦截器将会在调用之前拦截soap消息
	}

	/* 
	 * 实现自己的拦截器时 需要实现handleMessage方法
	 * handleMessage中的形参就是拦截到的soap消息
	 * 一旦获取到soap消息,剩下的事就是解析soap消息或者修改soap消息
	 */
	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		System.out.println("--------------拦截到"+message);
		
		//得到soap消息的所有header
		List<Header> headers=message.getHeaders();
		
		//没有header
		if(headers==null || headers.size()<1)
		{
			SOAPException soapExc = new SOAPException("异常,没有Hearder消息");
			throw new Fault(soapExc);
		}
		
		//如果要求第一个hearder携带了用户名和密码
		Header firstHeader=headers.get(0);
		Element ele=(Element)firstHeader.getObject();
		
		NodeList userIdNode = ele.getElementsByTagName("userid");
		NodeList userPassNode = ele.getElementsByTagName("userpass");
		
		if(userIdNode.getLength()!=1)
		{
			throw new Fault(new SOAPException("异常,用户名格式不对"));
		}
		if(userPassNode.getLength()!=1)
		{
			throw new Fault(new SOAPException("异常,密码格式不对"));
		}
		
		String userId=userIdNode.item(0).getTextContent();
		String userPass=userPassNode.item(0).getTextContent();
		System.out.println(userId+":"+userPass);
		
		if (userId.equals("gt") && userPass.equals("123456")) {
			System.out.println("认证成功");
		} else {
			throw new Fault(new SOAPException("认证错误"));
		}

	}

}

集成spring配置:

	<bean id="interceptor1" class="ws.interceptor.AuthInterceptor"></bean>

	<jaxws:endpoint id="hello" implementor="#service1" address="/HelloWorldService" >
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
			<ref bean="interceptor1"/>
		</jaxws:inInterceptors>
	</jaxws:endpoint>


客户端

拦截器类:

package 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 AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	private String userId;
	private String userPass;
	
	
	public AddHeaderInterceptor(String userId,String userPass) {
		super(Phase.PREPARE_SEND);
		this.userId=userId;
		this.userPass=userPass;

	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		List<Header> headers=message.getHeaders();
		//创建doc对象
		Document doc=DOMUtils.createDocument();
		Element ele=doc.createElement("authorHeader");
		
		//此处元素应该按照服务器那边的要求
		Element idEle=doc.createElement("userid");
		idEle.setTextContent(userId);
		Element passEle=doc.createElement("userpass");
		passEle.setTextContent(userPass);
		
		ele.appendChild(idEle);
		ele.appendChild(passEle);
		
		//把ele元素包装成Header,并添加到Soap的Headers列表中
		headers.add(new Header(new QName("gt-gt"),ele));
		
	}
	
}

调用类:

		HelloGT gt=new HelloGT();
		HelloWorld hello=gt.getHelloWorldImplPort();
		
		Client client=ClientProxy.getClient(hello);
		client.getOutInterceptors().add(new AddHeaderInterceptor("gt","123456"));
		client.getOutInterceptors().add(new LoggingOutInterceptor());
		
		User user=new User();


你可能感兴趣的:(Web,Web,Web,webservice,webservice,webservice,webservice)