webservice加入拦截器

service

1 pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jaxWsService</groupId>
  <artifactId>jaxWsService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
	<name>01_jaxws_server</name>
    <dependencies>
        <!-- 要进行jaxws 服务开发 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 内置jetty web服务器  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 日志实现 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.自定义拦截器

package util;

import java.util.List;

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;
import org.w3c.dom.NodeList;

public class ServiceInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	private static String tokenKey="gacfbgjxybtrcbugaxbdgvjtertxbjFQXDVFVEWTFBA462432";
	
	//在调用之前拦截
	public ServiceInterceptor() {
		super(Phase.PRE_INVOKE);
	}

	@Override
	public void handleMessage(SoapMessage soap) throws Fault {
		
		//获取请求头部信息
		List<Header> headers = soap.getHeaders();
		if(headers == null | headers.size()<1){
			throw new Fault(new IllegalArgumentException("找不到Header,无法验证token信息"));
		}
		
		Header header = headers.get(0);
		Element el = (Element)header.getObject();
		

		NodeList tokens = el.getElementsByTagName("token");
		
		if(tokens.getLength()<1){
			throw new Fault(new IllegalArgumentException("无token信息"));
		}
		String token = tokens.item(0).getTextContent().trim();
		//检查用户名和密码是否正确
		if(!token.equals(tokenKey)) {
			throw new Fault(new IllegalArgumentException("token验证失败"));
		}else{
			System.out.println("token验证成功");
		}
		
//		NodeList users = el.getElementsByTagName("username");
//		NodeList passwords = el.getElementsByTagName("password");		
//		//检查是否有用户名和密码元素
//		if(users.getLength()<1){
//			throw new Fault(new IllegalArgumentException("找不到用户信息"));
//		}
//		String username = users.item(0).getTextContent().trim();
//		
//		if(passwords.getLength()<1){
//			throw new Fault(new IllegalArgumentException("找不到密码信息"));
//		}
//		String password = passwords.item(0).getTextContent();
//		
//		//检查用户名和密码是否正确
//		if(!"admin".equals(username) || !"admin".equals(password)){
//			throw new Fault(new IllegalArgumentException("用户名或密码不正确"));
//		}else{
//			System.out.println("用户名密码正确允许访问");
//		}

	}
}

3.发布服务

package util;

import javax.xml.ws.Endpoint;

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

import student.StudentServiceImpl;

public class PublishService {

	
	public static void main(String[] args) {
		
		/**方法1****************************************************************/
		String address="http://localhost:8060/StudentService";
		EndpointImpl endpoint =(EndpointImpl)Endpoint.publish(address, new StudentServiceImpl());
		endpoint.getInInterceptors().add(new ServiceInterceptor());
		endpoint.getInInterceptors().add(new LoggingInInterceptor());
		endpoint.getOutFaultInterceptors().add(new LoggingOutInterceptor());
		System.out.println("发布成功!地址为:  "+address);
		
		
		/**方法2****************************************************************/
//		String address="http://localhost:8060/StudentService";
//		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//		factory.setAddress(address);
//		factory.setServiceBean(new StudentServiceImpl());
//        //  添加日志输入、输出拦截器,观察soap请求、soap响应内容
//        factory.getInInterceptors().add(new LoggingInInterceptor());
//        factory.getOutInterceptors().add(new LoggingOutInterceptor());
//		factory.create();
//		System.out.println("发布成功!地址为:  "+address);

	}

}

4.jar包
在这里插入图片描述在这里插入图片描述

client

1.pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jaxWsService</groupId>
  <artifactId>jaxWsService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
	<name>01_jaxws_server</name>
    <dependencies>
        <!-- 要进行jaxws 服务开发 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 内置jetty web服务器  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>
        
<!--         <dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>com.sun.xml.bind</groupId>
			<artifactId>jaxb-impl</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>com.sun.xml.bind</groupId>
			<artifactId>jaxb-core</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>activation</artifactId>
			<version>1.1.1</version>
		</dependency>
        
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20140107</version>
		</dependency>
		
		fastjson 
 
		<dependency>
		    <groupId>com.alibaba</groupId> 
		    <artifactId>fastjson</artifactId>
		    <version>1.2.47</version>
		</dependency>
		
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
		    <version>2.4</version>
		</dependency>
		
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.11.0</version>
		</dependency> -->
		

<!-- 		<dependency>
		    <groupId>com.sun.xml.ws</groupId>
		    <artifactId>jaxws-rt</artifactId>
		    <version>3.0.0-M2</version>
		</dependency>

		<dependency>
		    <groupId>com.sun.activation</groupId>
		    <artifactId>javax.activation</artifactId>
		    <version>1.2.0</version>
		</dependency>
		
		<dependency>
		      <groupId>com.sun.xml.bind</groupId>
		      <artifactId>jaxb-xjc</artifactId>
		      <version>2.3.2</version>
		</dependency> -->

	   <!--  <dependency>
	        <groupId>javax.xml.bind</groupId>
	        <artifactId>jaxb-api</artifactId>
	        <version>2.3.0</version>
	    </dependency>
	    <dependency>
	        <groupId>com.sun.xml.bind</groupId>
	        <artifactId>jaxb-impl</artifactId>
	        <version>2.3.0</version>
	    </dependency>
	    <dependency>
	        <groupId>com.sun.xml.bind</groupId>
	        <artifactId>jaxb-core</artifactId>
	        <version>2.3.0</version>
	    </dependency>
	    <dependency>
	        <groupId>javax.activation</groupId>
	        <artifactId>activation</artifactId>
	        <version>1.1.1</version>
	    </dependency>
 -->
        <!-- 日志实现 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.拦截器

package util;

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

	private static String tokenKey="gacfbgjxybtrcbugaxbdgvjtertxbjFQXDVFVEWTFBA462432";
	
	private String token;
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}

	//在请求之前拦截
	public ClientInterceptor() {
		super(Phase.PREPARE_SEND);
	}

	@Override
	public void handleMessage(SoapMessage soap) throws Fault {

		List<Header> headers = soap.getHeaders();
		Document doc = DOMUtils.createDocument();
		Element auth = doc.createElement("authrity");
		Element token = doc.createElement("token");
		token.setTextContent(tokenKey);
		auth.appendChild(token);
		headers.add(0, new Header(new QName("tiamaes"),auth));
	}	

}

3.客户端

package util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
//import org.json.JSONObject;
//
//import com.alibaba.fastjson.JSON;
//import com.fasterxml.jackson.databind.ObjectMapper;

import student.StudentPO;
import student.StudentService;

public class JwsClient {

	public static void main(String[] args) throws MalformedURLException {
		
		/**方法1****************************************************************/
//		URL url = new URL("http://localhost:8060/StudentService?wsdl");  
//	       
        //创建服务  
        //1.namespaceURI - 命名空间地址  2.localPart - 服务名称  
//        QName qname = new QName("http://student/", "StudentServiceImplService");
//       
//        Service service = Service.create(url, qname);  
//        StudentService studentService=service.getPort(StudentService.class);
//        List<StudentPO> studentPOs=new ArrayList<>(); 
//        studentPOs=studentService.findAllStudent();
        
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8060/StudentService?wsdl");
		client.getOutInterceptors().add(new ClientInterceptor());
		try {
			 Object[] objects= client.invoke("findAllStudent");
			 StudentPO studentPO=new StudentPO();
			 studentPO.setGender(6);
			 studentPO.setId("666");
			 studentPO.setName("张三");
			 studentPO.setSex("男");
			 client.invoke("insertStudent",studentPO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//        System.out.println(studentPOs);
        

		/**方法2****************************************************************/
        // 创建cxf代理工厂
//		JaxWsProxyFactoryBean factory = new  JaxWsProxyFactoryBean();
//        //  设置远程访问服务端地址
//        factory.setAddress("http://localhost:8060/StudentService");
//        factory.setServiceClass(StudentService.class);
//        StudentService studentService = factory.create(StudentService.class);
//	    List<StudentPO> studentPOs=new ArrayList<>(); 
//	    studentPOs=studentService.findAllStudent();
//	    System.out.println(studentPOs);


	}


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值