WebService
是一种跨编程语言、跨操作系统平台的远程调用技术,它是指一个应用程序向外界暴露一个能通过Web
调用的API
接口,我们把调用这个WebService
的应用程序称作客户端,把提供这个WebService
的应用程序称作服务端。
win10+Spring5.1+cxf3.3.2
4qp7
web
项目apache-cxf-3.3.2\lib
中的jar
包全部copy
至项目WEB-INF\lib
目录下(偷个懒,这些jar
包中包含了Spring
所需的jar
包)web.xml
中添加webService
的配置拦截
CXFService
org.apache.cxf.transport.servlet.CXFServlet
CXFService
/webservice/*
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
spring
配置文件WEB-INF
目录下新建spring
配置文件applicationContext.xml
在web.xml
中配置applicationContext.xml
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
tomcat
中启动localhost:PORT/项目名/webservice/webServiceInterface?wsdl
,如下图所示,webservice
接口发布成功SoapUI
是一个开源测试工具,通过soap/http
来检查、调用、实现Web Service
的功能/负载/符合性测试。
https://pan.baidu.com/s/1N2RTqhvrkuzx7YJvmDeY7Q
e1w3
wsdl2java工具
生成webservice
客户端代码apache-cxf-3.3.2\bin
目录下CXF_HOME
,并添加%CXF_HOME %/bin
到path
环境变量。CMD
命令行输入wsdl2java -help
,有正常提示说明环境已经正确配置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
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,"));
}
}
applicationContext.xml
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);
}
}
webservice
自定义请求头的实现,服务接口在身份认证过程中的密码字段满足SM3
(哈希函数算法标准)的加密要求SM3
加密所需jar
包:commons-lang3-3.9.jar
、bcprov-jdk15on-1.60.jar
,这两个jar
包在刚才下载的apache-cxf-3.3.2\lib
下就有
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 {
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 的消息头 中,
* 类似如下:
*
* admin P@ssw0rd
*
* 现在只需要解析其中的 标签,如果解析验证成功,则放行,否则这里直接抛出异常,
* 服务端不会再往后运行,客户端也会跟着抛出异常,得不到正确结果
*
* @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 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
添加拦截器配置
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{
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文档
*
* admin
* P@ssw0rd
*
*/
// 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 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,"));
}
}
服务端:https://github.com/Maggieq8324/spring_webservice_server.git
客户端:https://github.com/Maggieq8324/spring_webservice_client.git
.end