首先,目前springboot的版本最高为2.2.6,在集成cxf的3.2.4或3.2.5 时候会出现启动报错,这个太折磨人。。。。必须把springboot版本换到2.1.2以下,否者不兼容。
这是模拟企业用户的开发,在远程调用的时候客户端必须传入密码,这里通过cxf框架 的自定义拦截器实现jax-ws,jax-rs下次在学习下。
创建springboot项目:
导入起步依赖以及junit
pom文件如下:
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.apache.cxf
cxf-spring-boot-starter-jaxws
3.2.5
net.sf.json-lib
json-lib
jdk15
2.4
com.google.code.gson
gson
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
(一、配置文件)
application.properties中自定义接口指定相关信息:访问用户名、密码、访问路径前缀(有默认值,此处指定/mySoap)、站点(终端路径)
#####server and client###### 访问用户名、密码、访问路径前缀(有默认值,此处指定/mySoap)、站点(终端路径)
webservices.username=admin
webservices.password=root
webservices.service.prefix=/mySoap
webservices.service.endpoint=/user
(二、提供读取类Properties)
package cn.cj.webservice.server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Properties {
@Value("${webservices.username}")
private String USERNAME;
@Value("${webservices.password}")
private String PASSWORD;
public String getUSERNAME() {
return USERNAME;
}
public String getPASSWORD() {
return PASSWORD;
}
}
(三、服务端代码):
1、业务接口及实现类
package cn.cj.webservice.server;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
//声明一个访问接口
@WebService
public interface IMessageService {
@WebMethod
public String getMessary(@WebParam(name = "param") String param);
}
2、实现
package cn.cj.webservice.server;
import net.sf.json.JSONArray;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// name:暴露的服务名称;targetNamespace:命名空间,默认为本类包名倒写;endpointInterface:接口地址
@WebService(name="testCXF", targetNamespace="http://server.webservice.cj.cn/",
endpointInterface="cn.cj.webservice.server.IMessageService")
public class MessageServiceImpl implements IMessageService {
@Override
public String getMessary(String param) {
List
3、编写拦截器AuthInterceptor用于访问用户验证:
package cn.cj.webservice.server;
import org.apache.cxf.binding.soap.SoapHeader;
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.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.soap.SOAPException;
import java.util.List;
public class AuthInterceptor extends AbstractPhaseInterceptor {
private String USERNAME;
private String PASSWORD;
public AuthInterceptor(String username, String password) {
// 定义在什么阶段进行拦截
super(Phase.PRE_PROTOCOL);
this.USERNAME = username;
this.PASSWORD = password;
}
@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {
String username = null;
String password = null;
List headers = soapMessage.getHeaders();
if(headers == null) {
throw new Fault(new IllegalArgumentException("headers未取到,无法验证用户信息"));
}
// 获取客户端传递的用户名和密码
for (Header header : headers) {
SoapHeader soapHeader = (SoapHeader) header;
Element e = (Element) soapHeader.getObject();
NodeList usernameNode = e.getElementsByTagName("username");
NodeList passwordNode = e.getElementsByTagName("password");
username = usernameNode.item(0).getTextContent();
password = passwordNode.item(0).getTextContent();
if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
throw new Fault(new IllegalArgumentException("用户信息为空!"));
}
}
// 校验客户端用户名密码是否和服务端一致
if(!(username.equals(USERNAME) && password.equals(PASSWORD))) {
throw new Fault(new SOAPException("用户信息认证失败!"));
}
}
}
(四、配置中心发布接口)
package com.ccl.webservice.server.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ccl.webservice.server.interceptor.AuthInterceptor;
import com.ccl.webservice.server.service.IMessageService;
import com.ccl.webservice.server.service.MessageServiceImpl;
@Configuration
public class CXFConfig {
@Value("${webservices.service.prefix}")
private String prefix;
@Value("${webservices.service.endpoint}")
private String endpoint;
@Value("${webservices.username}")
private String username;
@Value("${webservices.password}")
private String password;
/**
* 作用:改变服务名的前缀名
* 此方法被注释后:wsdl访问(默认)地址为http://127.0.0.1:8080/services/user?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/mySoap/user?wsdl
* @return
*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), prefix + "/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springbus() {
return new SpringBus();
}
@Bean
public IMessageService messageServiceImpl() {
return new MessageServiceImpl();
}
/**
* JAX-WS:EndpointImpl
* 站点服务:终端路径
* @return
*/
@Bean
public Endpoint endpoint() {
EndpointImpl endpointImpl = new EndpointImpl(springbus(), messageServiceImpl());
// 服务端添加自定义拦截器:用户密码
endpointImpl.getInInterceptors().add(new AuthInterceptor(username, password));
endpointImpl.publish(endpoint);
return endpointImpl;
}
}
现在可以访问了,地址为http://127.0.0.1:8080/mySo/user?wsdl
(五、客户端)
package cn.cj.webservice.client;
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;
import javax.xml.namespace.QName;
import java.util.List;
/**
* 用于拼接xml,密码和个人信息
*/
public class ClientInterceptor extends AbstractPhaseInterceptor {
private String username;
private String password;
public ClientInterceptor(String username, String password) {
// 发送请求之前进行拦截
super(Phase.PREPARE_SEND);
this.username = username;
this.password = password;
}
@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {
List headers = soapMessage.getHeaders();
Document doc = DOMUtils.createDocument();
Element auth = doc.createElement("authrity");
Element username = doc.createElement("username");
Element password = doc.createElement("password");
username.setTextContent(this.username);
password.setTextContent(this.password);
auth.appendChild(username);
auth.appendChild(password);
headers.add(0, new Header(new QName("tiamaes"), auth));
}
}
远程调用
package cn.cj.webservice.client;
import com.google.gson.Gson;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class ClientMain {
// 客户端访问服务端接口地址
private static String address = “http://127.0.0.1:8080/mySoap/user?wsdl”;
/**
* 动态调用方式
*/
@SuppressWarnings("all")
public static void main(String[] args) {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(address);
// 添加用户信息验证
client.getOutInterceptors().add(new ClientInterceptor("admin", "root"));
// 取返回值
Object[] objects = new Object[0];
try {
// 接口方法、参数
objects = client.invoke("getMessary", "clientParam");
Map[] maps = new Gson().fromJson(objects[0].toString(),Map[].class);
List> list = Arrays.asList(maps);
System.out.println("返回的数据:");
System.out.println(list);
} catch (Exception e) {
System.out.println("客户端接口访问失败!!");
e.printStackTrace();
}
}
}