SpringBoot整合CXF完成webservice服務例項(包括使用者驗證)
阿新 • • 發佈:2018-12-20
最近接到了一個對外發布介面的需求,所以找到CXF寫了一個簡單例子僅供參考。考慮到方便呼叫,服務端資料採用Map封裝,這樣不需要客戶端建立相關實體類就可以拿到目標資料。
一、專案目錄
二、pom.xml依賴引入
注意:
依賴版本不同,程式碼也會有不同之處:springboot版本2.0.1,CXF版本3.2.4,json-lib版本2.4
gson是客戶端用來將接收到的JSONArray形式的String類轉成List的依賴包
特別的,<classifier>jdk15</classifier>必須有,不然報錯。
<!-- 整合cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</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>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
三、程式碼介紹
(1)需要兩個專案分別部署在服務端和客戶端,這裡使用server包和client包代替,在本機中進行測試。
① 首先新建springboot專案,然後application.properties中自定義介面指定相關資訊:
訪問使用者名稱、密碼、訪問路徑字首(有預設值,此處指定/mySoap)、站點(終端路徑)
#####server and client###### webservices.username=admin webservices.password=root webservices.service.prefix=/mySoap webservices.service.endpoint=/user
② 提供讀取類Properties
package com.ccl.webservice.server.service; 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; } }
(2)server包(對應服務端)
① 業務介面及實現類
package com.ccl.webservice.server.service;
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);
}
package com.ccl.webservice.server.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import net.sf.json.JSONArray;
// name:暴露的服務名稱;targetNamespace:名稱空間,預設為本類包名倒寫;endpointInterface:介面地址
@WebService(name="testCXF", targetNamespace="http://service.server.webservice.ccl.com/",
endpointInterface="com.ccl.webservice.server.service.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", "20");
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();
}
}
② CXF釋出介面配置類CXFConfig,此處先編寫攔截器AuthInterceptor用於訪問使用者驗證:
package com.ccl.webservice.server.interceptor;
import java.util.List;
import javax.xml.soap.SOAPException;
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;
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 = 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;
}
}
(3)client包(對應客戶端)
① 編寫攔截器ClientInterceptor用於在請求頭新增使用者資訊用於驗證
package com.ccl.webservice.client;
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 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<Header> 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));
}
}
② 客戶端進行訪問
這裡採用動態呼叫方式訪問,並通過JSONArray傳遞資料,不需要客戶端編寫業務程式碼和相關實體類,比較方便。
package com.ccl.webservice.client;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import com.google.gson.Gson;
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<Map<String, String>> list = Arrays.asList(maps);
System.out.println("返回的資料:");
System.out.println(list);
} catch (Exception e) {
System.out.println("客戶端介面訪問失敗!!");
e.printStackTrace();
}
}
}