webService(⼆springboot的wsdl、soap协议)
⾸先,⽬前springboot的版本最⾼为2.2.6,在集成cxf的3.2.4或3.2.5 时候会出现启动报错,这个太折磨⼈。。。。必须把springboot 版本换到2.1.2以下,否者不兼容。
这是模拟企业⽤户的开发,在远程调⽤的时候客户端必须传⼊密码,这⾥通过cxf框架 的⾃定义实现jax-ws,jax-rs下次在学习下。创建springboot项⽬:
导⼊起步依赖以及junit
pom⽂件如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 集成cxf -->
<dependency>
<groupId>f</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<!-- 服务端:⽤来⽣成JSONArray -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
<version>2.4</version>
</dependency>
<!-- 客户端:⽤来解析JSONArray -->
<dependency>
<groupId&le.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>springboot框架的作用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
(⼀、配置⽂件)
application.properties中⾃定义接⼝指定相关信息:访问⽤户名、密码、访问路径前缀(有默认值,此处指定/mySoap)、站点(终端路径)
>server and client># 访问⽤户名、密码、访问路径前缀(有默认值,此处指定/mySoap)、站点(终端路径)
webservices.username=admin
webservices.password=root
webservices.service.prefix=/mySoap
dpoint=/user
(⼆、提供读取类Properties)
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、实现
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="server.webservice.cj/",
endpointInterface="cn.cj.webservice.server.IMessageService")
public class MessageServiceImpl implements IMessageService {
@Override
public String getMessary(String param) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> messaryMap1 = new HashMap<String, String>();
messaryMap1.put("param", param);
messaryMap1.put("name", "华闻");
messaryMap1.put("sex", "男");
messaryMap1.put("age", "18");
Map<String, String> messaryMap2 = new HashMap<String, String>();
messaryMap2.put("param", param);
messaryMap2.put("name", "程勇");
messaryMap2.put("sex", "⼥");
messaryMap2.put("age", "18");
list.add(messaryMap1);
list.add(messaryMap2);
// 返回JSON数组字符串
return JSONArray.fromObject(list).toString();
}
}
3、编写AuthInterceptor⽤于访问⽤户验证:
import f.binding.soap.SoapHeader;
import f.binding.soap.SoapMessage;
import f.headers.Header;
import f.interceptor.Fault;
import f.phase.AbstractPhaseInterceptor;
import f.phase.Phase;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
l.soap.SOAPException;
import java.util.List;
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
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<Header> headers = Headers();
if(headers == null) {
throw new Fault(new IllegalArgumentException("headers未取到,⽆法验证⽤户信息"));  }
// 获取客户端传递的⽤户名和密码
for (Header header : headers) {
SoapHeader soapHeader = (SoapHeader) header;
Element e = (Element) Object();
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("⽤户信息认证失败!"));
}
}
}
(四、配置中⼼发布接⼝)
l.fig;
l.ws.Endpoint;
import f.Bus;
import f.bus.spring.SpringBus;
import f.jaxws.EndpointImpl;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论