WebService之Spring+CXF整合示例

Java框架

浏览数:14

2020-6-11

AD:资源代下载服务

一、Spring+CXF整合示例

WebService是一种跨编程语言、跨操作系统平台的远程调用技术,它是指一个应用程序向外界暴露一个能通过Web调用的API接口,我们把调用这个WebService的应用程序称作客户端,把提供这个WebService的应用程序称作服务端。

环境

win10+Spring5.1+cxf3.3.2

下载

服务端

  • 新建web项目
  • 放入依赖
    apache-cxf-3.3.2\lib中的jar包全部copy至项目WEB-INF\lib目录下(偷个懒,这些jar包中包含了Spring所需的jar包)
  • web.xml中添加webService的配置拦截
<!--webService  -->
<servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
  • webservice服务接口
    在项目src目录下新建pms.inface.WebServiceInterface
package pms.inface;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(targetNamespace = "http://spring.webservice.server", name = "WebServiceInterface")
public interface WebServiceInterface {

	@WebMethod
    @WebResult(name = "result", targetNamespace = "http://spring.webservice.server")
	public String sayBye(@WebParam(name = "word", targetNamespace = "http://spring.webservice.server") String word);

}

  • 接口实现类
    在项目src目录下新建pms.impl.WebServiceImpl
package pms.impl;

import javax.jws.WebService;

import pms.inface.WebServiceInterface;

@WebService
public class WebServiceImpl implements WebServiceInterface{

	@Override
	public String sayBye(String word) {
		return word + "当和这个真实的世界迎面撞上时,你是否找到办法和自己身上的欲望讲和,又该如何理解这个铺面而来的人生?";
	}

}

  • webservice配置文件
    WEB-INF目录下新建webservice配置文件cxf-webService.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"
	xmlns:cxf="http://cxf.apache.org/core"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
       http://cxf.apache.org/core
	   http://cxf.apache.org/schemas/core.xsd
	   http://cxf.apache.org/transports/http/configuration
	   http://cxf.apache.org/schemas/configuration/http-conf.xsd
	   ">
	   
	<import resource="classpath:META-INF/cxf/cxf.xml" />

	<!-- 使用jaxws:server标签发布WebService服务 ,设置address为访问地址, 和web.xml文件中配置的CXF配合为一个完整的路径 -->
	<!-- serviceClass为实现类的接口 serviceBean引用配置好的WebService实现类 -->
	<jaxws:server address="/webServiceInterface"
		serviceClass="pms.inface.WebServiceInterface">
		<jaxws:serviceBean>
			<ref bean="WebServiceImpl" />
		</jaxws:serviceBean>
	</jaxws:server>
	
	<!-- 为所有的WS设置超时时间 ,此时为默认值 连接时间30s,等待回复时间为60s-->	
	<http-conf:conduit name="*.http-conduit">
		<http-conf:client ConnectionTimeout="60000" ReceiveTimeout="120000"/>
	</http-conf:conduit>

</beans>
  • spring配置文件
    WEB-INF目录下新建spring配置文件applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="WebServiceImpl" class="pms.impl.WebServiceImpl"></bean>
	
	<import resource="cxf-webService.xml" />

</beans>

      在web.xml中配置applicationContext.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
		    /WEB-INF/applicationContext.xml
		</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  • 将项目放至tomcat中启动
    启动后访问地址:localhost:PORT/项目名/webservice/webServiceInterface?wsdl,如下图所示,webservice接口发布成功

二、SoapUI测试

SoapUI是一个开源测试工具,通过soap/http来检查、调用、实现Web Service的功能/负载/符合性测试。

下载

  • 百度网盘
    链接:https://pan.baidu.com/s/1N2RTqhvrkuzx7YJvmDeY7Q
    提取码:e1w3

测试

  • 打开SoapUI,新建一个SOAP项目,将刚才的发布地址copyInitial WSDL栏,点击OK按钮
  • 发起接口请求

三、客户端

使用wsdl2java工具生成webservice客户端代码

  • 该工具在刚才下载的apache-cxf-3.3.2\bin目录下
  • 配置环境变量
    设置CXF_HOME,并添加%CXF_HOME %/binpath环境变量。

  • CMD命令行输入wsdl2java -help,有正常提示说明环境已经正确配置
  • wsdl2java.bat用法:
wsdl2java –p 包名 –d 存放目录 -all wsdl地址

-p 指定wsdl的命名空间,也就是要生成代码的包名

-d 指令要生成代码所在目录

-client 生成客户端测试web service的代码

-server 生成服务器启动web service代码

-impl 生成web service的实现代码,我们在方式一用的就是这个

-ant 生成build.xml文件

-all 生成所有开始端点代码
  • 生成客户端代码
wsdl2java -p pms.inface -d ./ -all http://localhost:8080/spring_webservice_server/webservice/webServiceInterface?wsdl

客户端调用

  • 新建web项目
  • 放入依赖
    apache-cxf-3.3.2\lib中的jar包全部copy至项目WEB-INF\lib目录下
  • wsdl2java生成的代码放至src.pms.inface目录下
调用方法一:
  • 新建webServiceClientMain测试
package pms;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import pms.inface.WebServiceInterface;

public class webServiceClientMain {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
		svr.setServiceClass(WebServiceInterface.class);
		svr.setAddress("http://localhost:8080/spring_webservice_server/webservice/webServiceInterface?wsdl");
		WebServiceInterface webServiceInterface = (WebServiceInterface) svr.create();

		System.out.println(webServiceInterface.sayBye("honey,"));
	}
}
  • 运行webServiceClientMain
调用方法二:
  • 在src目录下新建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://cxf.apache.org/jaxws
		http://cxf.apache.org/schemas/jaxws.xsd">

	<jaxws:client id="webServiceInterface"
		serviceClass="pms.inface.WebServiceInterface"
		address="http://localhost:8080/spring_webservice_server/webservice/webServiceInterface?wsdl" >
	</jaxws:client>	
</beans>
  • 新建webServiceClientTest测试
package pms;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pms.inface.WebServiceInterface;

public class webServiceClientTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		WebServiceInterface webServiceInterface = context.getBean(WebServiceInterface.class);
		String result = webServiceInterface.sayBye("honey,");
		System.out.println(result);
	}
	
}
  • 运行webServiceClientTest

四、服务端拦截器

  • 需求场景:服务提供方安全验证,也就是webservice自定义请求头的实现,服务接口在身份认证过程中的密码字段满足SM3(哈希函数算法标准)的加密要求
  • SM3加密所需jar包:commons-lang3-3.9.jarbcprov-jdk15on-1.60.jar,这两个jar包在刚才下载的apache-cxf-3.3.2\lib下就有
  • 请求头格式
<security>
	<username></username>
	<password></password>
</auth>
  • src.pms.interceptor下新建WebServiceInInterceptor拦截器拦截请求,解析头部
package pms.interceptor;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
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.cxf.transport.http.AbstractHTTPDestination;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import pms.support.Sm3Utils;
import pms.support.StringUtils;

/**
 * WebService的输入拦截器
 * @author coisini
 * @date May 2020, 13
 *
 */
public class WebServiceInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "P@ssw0rd";
    
    /**
     * 允许访问的IP
     */
    private static final String ALLOWIP = "127.0.0.1;XXX.XXX.XXX.XXX";

	public WebServiceInInterceptor() {
		/*
		 * 拦截器链有多个阶段,每个阶段都有多个拦截器,拦截器在拦截器链的哪个阶段起作用,可以在拦截器的构造函数中声明
		 * RECEIVE 接收阶段,传输层处理
		 * (PRE/USER/POST)_STREAM 流处理/转换阶段
		 * READ SOAPHeader读取 
		 * (PRE/USER/POST)_PROTOCOL 协议处理阶段,例如JAX-WS的Handler处理 
		 * UNMARSHAL SOAP请求解码阶段 
		 * (PRE/USER/POST)_LOGICAL SOAP请求解码处理阶段 
		 * PRE_INVOKE 调用业务处理之前进入该阶段 
		 * INVOKE 调用业务阶段 
		 * POST_INVOKE 提交业务处理结果,并触发输入连接器
		 */
		super(Phase.PRE_INVOKE);
	}

	/**
	  * 客户端传来的 soap 消息先进入拦截器这里进行处理,客户端的账目与密码消息放在 soap 的消息头<security></security>中,
	  * 类似如下:
     * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
     * <soap:Header><security><username>admin</username><password>P@ssw0rd</password></security></soap:Header>
     * <soap:Body></soap:Body></soap:Envelope>
     * 现在只需要解析其中的 <head></head>标签,如果解析验证成功,则放行,否则这里直接抛出异常,
     * 服务端不会再往后运行,客户端也会跟着抛出异常,得不到正确结果
     *
     * @param message
     * @throws Fault
     */
	@Override
    public void handleMessage(SoapMessage message) throws Fault {
		System.out.println("PRE_INVOKE");
		
		HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
	    String ipAddr=request.getRemoteAddr();
	    System.out.println("客户端访问IP----"+ipAddr);
	    
	    if(!ALLOWIP.contains(ipAddr)) {
			throw new Fault(new IllegalArgumentException("非法IP地址"), new QName("0009"));
		}
		
		/**
		 * org.apache.cxf.headers.Header
         * QName :xml 限定名称,客户端设置头信息时,必须与服务器保持一致,否则这里返回的 header 为null,则永远通不过的
         */
		Header authHeader = null;
		//获取验证头
		List<Header> headers = message.getHeaders();
		for(Header h:headers){
			if(h.getName().toString().contains("security")){
				authHeader=h;
				break;
			}
		}
		System.out.println("authHeader");
		System.out.println(authHeader);
		
		if(authHeader !=null) {
			Element auth = (Element) authHeader.getObject();
			NodeList childNodes = auth.getChildNodes();
			String username = null,password = null;
			for(int i = 0, len = childNodes.getLength(); i < len; i++){
					Node item = childNodes.item(i);
					if(item.getNodeName().contains("username")){
						username = item.getTextContent();
						System.out.println(username);
					}
					if(item.getNodeName().contains("password")){
						password = item.getTextContent();
						System.out.println(password);
					}
			}
			
			if(StringUtils.isBlank(username) || StringUtils.isBlank(password)) { 
		    	throw new Fault(new IllegalArgumentException("用户名或密码不能为空"), new QName("0001")); 
		    }
			
			if(!Sm3Utils.verify(USERNAME, username) || !Sm3Utils.verify(PASSWORD,password)) { 
		    	throw new Fault(new IllegalArgumentException("用户名或密码错误"), new QName("0008")); 
		    }
		  
		    if (Sm3Utils.verify(USERNAME, username) && Sm3Utils.verify(PASSWORD,password)) { 
		    	System.out.println("webService 服务端自定义拦截器验证通过...."); 
		    	return;//放行
		    } 
		}else {
			throw new Fault(new IllegalArgumentException("请求头security不合法"), new QName("0010"));
		}
	}

	// 出现错误输出错误信息和栈信息
	public void handleFault(SoapMessage message) {
		Exception exeption = message.getContent(Exception.class);
		System.out.println(exeption.getMessage());
	}
	
}
  • src.pms.support下新建Sm3Utils加密类
package pms.support;

import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Arrays;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

/**
 * SM3加密
 * @author coisini
 * @date May 2020, 13
 */
public class Sm3Utils {
	 private static final String ENCODING = "UTF-8";
     static {
         Security.addProvider(new BouncyCastleProvider());
     }
	    
    /**
     * sm3算法加密
     * @explain
     * @param paramStr
     * 待加密字符串
     * @return 返回加密后,固定长度=32的16进制字符串
     */
    public static String encrypt(String paramStr){
        // 将返回的hash值转换成16进制字符串
        String resultHexString = "";
        try {
            // 将字符串转换成byte数组
            byte[] srcData = paramStr.getBytes(ENCODING);
            // 调用hash()
            byte[] resultHash = hash(srcData);
            // 将返回的hash值转换成16进制字符串
            resultHexString = ByteUtils.toHexString(resultHash);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return resultHexString;
    }
    
    /**
     * 返回长度=32的byte数组
     * @explain 生成对应的hash值
     * @param srcData
     * @return
     */
    public static byte[] hash(byte[] srcData) {
        SM3Digest digest = new SM3Digest();
        digest.update(srcData, 0, srcData.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return hash;
    }
    
    /**
     * 通过密钥进行加密
     * @explain 指定密钥进行加密
     * @param key
     *            密钥
     * @param srcData
     *            被加密的byte数组
     * @return
     */
    public static byte[] hmac(byte[] key, byte[] srcData) {
        KeyParameter keyParameter = new KeyParameter(key);
        SM3Digest digest = new SM3Digest();
        HMac mac = new HMac(digest);
        mac.init(keyParameter);
        mac.update(srcData, 0, srcData.length);
        byte[] result = new byte[mac.getMacSize()];
        mac.doFinal(result, 0);
        return result;
    }
    
    /**
     * 判断源数据与加密数据是否一致
     * @explain 通过验证原数组和生成的hash数组是否为同一数组,验证2者是否为同一数据
     * @param srcStr
     *            原字符串
     * @param sm3HexString
     *            16进制字符串
     * @return 校验结果
     */
    public static boolean verify(String srcStr, String sm3HexString) {
        boolean flag = false;
        try {
            byte[] srcData = srcStr.getBytes(ENCODING);
            byte[] sm3Hash = ByteUtils.fromHexString(sm3HexString);
            byte[] newHash = hash(srcData);
            if (Arrays.equals(newHash, sm3Hash))
                flag = true;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return flag;
    }
    
    public static void main(String[] args) {
        // 测试二:account
        String account = "admin";
        String passoword = "P@ssw0rd";
        String hex = Sm3Utils.encrypt(account);
        System.out.println(hex);//dc1fd00e3eeeb940ff46f457bf97d66ba7fcc36e0b20802383de142860e76ae6
        System.out.println(Sm3Utils.encrypt(passoword));//c2de40449a2019db9936381fa9810c22c8548a8635ed2b7fb3c7ec362e37429d
        //验证加密后的16进制字符串与加密前的字符串是否相同
        boolean flag =  Sm3Utils.verify(account, hex);
        System.out.println(flag);// true
    }
}
  • StringUtils工具类
package pms.support;

/**
 * 字符串工具类
 * @author coisini
 * @date Nov 27, 2019
 */
public class StringUtils {

	/**
	 * 判空操作
	 * @param value
	 * @return
	 */
	public static boolean isBlank(String value) {
		return value == null || "".equals(value) || "null".equals(value) || "undefined".equals(value);
	}

}
  • cxf-webService.xml添加拦截器配置
<!-- 在此处引用拦截器 -->
<bean id="InInterceptor"
	class="pms.interceptor.WebServiceInInterceptor" >
</bean>

<cxf:bus>
	<cxf:inInterceptors>
		<ref bean="InInterceptor" />
	</cxf:inInterceptors>
</cxf:bus> 
  • SoapUI调用

  • java调用

    服务端拦截器到此结束,由上图可以看出拦截器配置生效

五、客户端拦截器

  • src.pms.support下新建AddHeaderInterceptor拦截器拦截请求,添加自定义认证头部
package pms.support;

import java.util.List;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapHeader;
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 userName; 
    private String password; 
       
    public AddHeaderInterceptor(String userName, String password) { 
        super(Phase.PREPARE_SEND); 
        this.userName = userName; 
        this.password = password;  
    } 
   
    @Override 
    public void handleMessage(SoapMessage msg) throws Fault { 
    	   System.out.println("拦截...");
        
           /**
            * 生成的XML文档
            * <authHeader>
            *      <userName>admin</userName>
            *      <password>P@ssw0rd</password>
            * </authHeader>
            */ 
        
        	// SoapHeader部分待添加的节点
     		QName qName = new QName("security");
     		Document doc = DOMUtils.createDocument();

     		Element pwdEl = doc.createElement("password");
     		pwdEl.setTextContent(password);
     		Element userEl = doc.createElement("username");
     		userEl.setTextContent(userName);
     		Element root = doc.createElement("security");
     		root.appendChild(userEl);
     		root.appendChild(pwdEl);
     		// 创建SoapHeader内容
     		SoapHeader header = new SoapHeader(qName, root);
     		// 添加SoapHeader内容
     		List<Header> headers = msg.getHeaders();
     		headers.add(header); 
    } 
}
  • java调用,修改webServiceClientMain调用代码如下
public class webServiceClientMain {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
		svr.setServiceClass(WebServiceInterface.class);
		svr.setAddress("http://localhost:8081/spring_webservice_server/webservice/webServiceInterface?wsdl");
		WebServiceInterface webServiceInterface = (WebServiceInterface) svr.create();
		
		// jaxws API 转到 cxf API 添加日志拦截器
		org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy
				.getClient(webServiceInterface);
		org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
		//添加自定义的拦截器
		cxfEndpoint.getOutInterceptors().add(new AddHeaderInterceptor("dc1fd00e3eeeb940ff46f457bf97d66ba7fcc36e0b20802383de142860e76ae6", "c2de40449a2019db9936381fa9810c22c8548a8635ed2b7fb3c7ec362e37429d"));
		
		System.out.println(webServiceInterface.sayBye("honey,"));
	}
}

  • SoapUI调用

六、代码示例

服务端:https://github.com/Maggieq8324/spring_webservice_server.git
客户端:https://github.com/Maggieq8324/spring_webservice_client.git

.end

作者:95.8℃