CXF自定义拦截器的使用

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/48215041

首先,我们先准备好终端的接口和实现类

package com.webservice.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloWS {
	
	    @WebMethod
        public String sayHello(String name);
}


实现类:

  

package com.webservice.ws;

import javax.jws.WebService;

@WebService
public class HelloWSImple implements HelloWS {

	@Override
	public String sayHello(String name) {
		System.out.println("终端接口实现");
		return "Hello,"+name;
	}

}

终端写完后,我们开始写服务器拦截器的代码,我们通过一个用户名和密码进行校验:

package com.webservice.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;


/**
 * 服务器端拦截器进行校验
 * @author yxs
 *
 */
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	public CheckUserInterceptor() {
		super(Phase.PRE_PROTOCOL);
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
	     Header header=message.getHeader(new QName("Student"));
		  if(header!=null){
			  Element Student=(Element)header.getObject();
			  String name=Student.getElementsByTagName("name").item(0).getTextContent();
			  String password=Student.getElementsByTagName("password").item(0).getTextContent();
			  if("scott".equals(name)&&"123456".equals(password)){
				  System.out.println("服务器端拦截器通过....");
				  return;
			  }
			  
			  
		  }
		  System.out.println("服务器端拦截器没有通过....");
		  throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码!"));
		
	}

}

然后我们开始发布服务器端的代码:

package com.webservice.server;

import java.util.List;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxws22.EndpointImpl;
import org.apache.cxf.message.Message;

import com.webservice.interceptor.CheckUserInterceptor;
import com.webservice.ws.HelloWSImple;

public class WbTest {
      public static void main(String[] args) {
		String address="http://localhost:8080/WebserviceDemo/helloWorld";
    	  Endpoint endpoint = Endpoint.publish(address, new HelloWSImple());
    	  System.out.println(endpoint);
    	  EndpointImpl endpointImpl=(EndpointImpl)endpoint;
    	  
    	  List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
    	  inInterceptors.add(new CheckUserInterceptor());
    	  
    	  
    	  System.out.println("发布webservice成功");
	}
}


接下来,我们需要通过cxf的wsdl2java生成客户端代码:


客户端代码生成后,我们就开始写客户端拦截器了:

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

/**
 * 客户端拦截器
 * @author yxs
 *
 */
public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	private String name;
	private String password;
	
	public AddUserInterceptor(String name,String password) {
		super(Phase.PRE_PROTOCOL);//准备协议时拦截
		this.name=name;
		this.password=password;
	}

	/*
 	<Envelope>
 		<head>
 			<Student>
 				<name>scott</name>
 				<password>123456</password>
 			</Student>
 			<Student>
 				<name>scott</name>
 				<password>123456</password>
 			</Student>
 		<head>
 		<Body>
 			<sayHello>
 				<arg0>scott</arg0>
 			<sayHello>
 		</Body>
 	</Envelope>
 */
	@SuppressWarnings("deprecation")
	public void handleMessage(SoapMessage msg) throws Fault {
		List<Header> headers = msg.getHeaders();
		/**
		 * <Student>
 				<name>scott</name>
 				<password>123456</password>
 			</Student>
		 */
		Document docuemnt= DOMHelper.createDocument();
		Element rootEle = docuemnt.createElement("Student");
		Element nameEle= docuemnt.createElement("name");
		nameEle.setTextContent(name);
		rootEle.appendChild(nameEle);
		
		Element passwordEle= docuemnt.createElement("password");
		passwordEle.setTextContent(password);
		rootEle.appendChild(passwordEle);
		
		headers.add(new Header(new QName("Student"), rootEle));
		System.out.println("客户端拦截器............");
		
	}

}

拦截器写完后,我就开始写客户端的调用代码了:

package com.webservice.ws.test;

import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.message.Message;

import com.webservice.ws.HelloWS;
import com.webservice.ws.HelloWSImpleService;
import com.webservice.ws.interceptor.AddUserInterceptor;

public class ClientTest {
         public static void main(String[] args) {
	         HelloWSImpleService factory=new HelloWSImpleService();
		 HelloWS helloWSImplePort = factory.getHelloWSImplePort();
        	 Client client = ClientProxy.getClient(helloWSImplePort);
        	 List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
        	 outInterceptors.add(new AddUserInterceptor("scott","123456"));
        	 String info=helloWSImplePort.sayHello("mike");
        	 System.out.println("client:"+info);
		     
        	 System.out.println("client===================");
        	 
		}
}

ok,大功告成。




相关文章
|
前端开发
jfinal拦截器Interceptor解析
jfinal拦截器Interceptor解析
310 0
jfinal拦截器Interceptor解析
|
Java
SpringBoot + cxf 调用 webService
SpringBoot + cxf 调用 webService
729 0
|
Java Spring 数据安全/隐私保护
|
Web App开发 Java Apache