webService 自定义拦截器

客户端 拦截器

package com.baidu.cxf.interceptor;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AddPhoneInterceptor extends AbstractPhaseInterceptor {

	private String name;
	private String password;
	
	public AddPhoneInterceptor(String name,String password) {
		super(Phase.PRE_PROTOCOL);//准备协议化的时候拦截
		this.name = name;
		this.password = password;
	}
	/**
	 * 
	 * 		text
	 * 		text
	 * 
	 */
	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		List
headers = msg.getHeaders(); //创建document Document document = DOMUtils.createDocument(); // 创建根节点 Element 其必须是 org.w3c.dom.Element Element rootElement = document.createElement("Chuck"); //创建rootElement 的子节点 nameElement Element nameElement = document.createElement("Name"); //设置nameElement 的标签体(即:验证的用户名) nameElement.setTextContent(name); //把 子标签nameElement 放入 根标签rootElement中 rootElement.appendChild(nameElement); //创建子节点 passwordElement 其余同上 Element passwordElement = document.createElement("Password"); passwordElement.setTextContent(password); rootElement.appendChild(passwordElement); headers.add(0,new Header(new QName("Chuck"), rootElement)); System.out.println("client handleMessage().."); } }



服务端拦截器:

package com.baidu.cxf.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.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;

public class ChuckPhoneInterceptor extends AbstractPhaseInterceptor {

//	private String name;
//	private String password;
//	
	public ChuckPhoneInterceptor() {
		super(Phase.PRE_PROTOCOL);//准备协议化的时候拦截
	}
	/**
	 * 
	 * 		
	 * 			
	 * 				用户名
	 * 				密码
	 * 				 
	 * 			
	 * 				用户名
	 * 				用户名
	 * 			
	 * 		
	 * 		
	 * 			
	 * 				Bob
	 * 			
	 * 		
	 * 
	 */
	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		System.out.println("msg: " + msg);
//		 Header header = msg.getHeader(new QName("Chuck"));
		 List
headers = msg.getHeaders(); if(headers != null && headers.size() >1){ Header header = headers.get(0); Element rootElement = (Element) header.getObject(); //getElementsByTagName("Name") 获取的是一个name的集合 ,所以需要 取item 0 位上的值 String name = rootElement.getElementsByTagName("Name").item(0).getTextContent(); String password = rootElement.getElementsByTagName("Password").item(0).getTextContent(); System.out.println("Name :" + name + " Password:" + password ); if(name.equals("hhh") && password.equals("mm")){ System.out.println("Service 通过拦截器"); return; } } System.out.println("Service 没有通过拦截器"); throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码!")); } }


你可能感兴趣的:(WebService)