CXF WebService 授权&动态调用
SpringBoot WebService 源代码:https://gitee.com/VipSoft/VipWebService
服务端添加拦截器
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 java.util.List;
import org.apache.cxf.binding.soap.SoapHeader;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import javax.xml.soap.SOAPException; @Component
public class WsAuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private static final String USERNAME = "root";
private static final String PASSWORD = "admin"; public WsAuthInterceptor() {
// 定义在哪个阶段进行拦截
super(Phase.PRE_PROTOCOL);
} @Override
public void handleMessage(SoapMessage soapMessage) throws Fault {
List<Header> headers = soapMessage.getHeaders();
String username = null;
String password = null;
if (headers == null || headers.isEmpty()) {
throw new Fault(new IllegalArgumentException("找不到Header,无法验证用户信息"));
}
// 获取用户名,密码
for (Header header : headers) {
SoapHeader soapHeader = (SoapHeader) header;
Element e = (Element) soapHeader.getObject();
username = e.getAttribute("username");
password = e.getAttribute("password");
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
throw new Fault(new IllegalArgumentException("用户信息为空"));
}
}
// 校验用户名密码
if (!(USERNAME.equals(username) && PASSWORD.equals(password))) {
SOAPException soapExc = new SOAPException("认证失败");
System.err.println("用户认证信息错误");
throw new Fault(soapExc);
}
}
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService()); //绑定要发布的服务
endpoint.publish("/api"); // 暴露webService api,用在资源访问
endpoint.getInInterceptors().add(wsAuthInterceptor);
return endpoint;
}
--------
客户端调用
SOAP UI
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.his.iron.com">
<soapenv:Header>
<Author username="root" password="admin"></Author>
</soapenv:Header>
<soapenv:Body>
<ser:sayHello>
<!--Optional:-->
<arg>Test</arg>
</ser:sayHello>
</soapenv:Body>
</soapenv:Envelope>
WebService 动态调用
@Override
public String soapDynamicTest(String ip) {
//创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
// Client client = factory.createClient("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
Client client = factory.createClient("http://192.168.1.216:9090/ws/api?wsdl");
// 需要密码的情况需要加上用户名和密码
//client.getOutInterceptors().add(new ClientAuthInterceptor("USER_NAME","PASS_WORD"));
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); //连接超时
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(120000); //响应超时
conduit.setClient(httpClientPolicy);
client.getOutInterceptors().add(new ClientCheckInterceptor()); try{
Object[] objects = new Object[0];
// invoke("方法名",参数1,参数2,参数3....);
// objects = client.invoke("getCountryCityByIp", ip);
objects = client.invoke("sayHello", ip);
System.out.println("返回数据:" + objects[0]);
// return ((ArrayOfString) objects[0]).getString().toString();
return objects[0].toString();
}catch (Exception e){
e.printStackTrace();
}
return "";
}
ClientCheckInterceptor.java
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; public class ClientAuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private static final String USERNAME = "root";
private static final String PASSWORD = "admin";
public ClientCheckInterceptor() {
super(Phase.PREPARE_SEND);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers = message.getHeaders();
Document document = DOMUtils.createDocument();
Element authorEle = document.createElement("author");
authorEle.setAttribute("username", USERNAME);
authorEle.setAttribute("password", PASSWORD);
headers.add(new Header(new QName(""), authorEle));
}
}
CXF WebService 授权&动态调用的更多相关文章
- C#调用WebService服务(动态调用)
原文:C#调用WebService服务(动态调用) 1 创建WebService using System; using System.Web.Services; namespace WebServi ...
- WebService – 2.动态调用WebService
在本节课程中,将演示如何通过程序动态添加.调用.编译.执行WebService并返回结果. WebService动态调用示意图 WebService相关知识 代码文档对象模型CodeDom的使用 编程 ...
- Webservice客户端动态调用服务端功能方法
一.发布WebService服务 方式一:在服务端生成wsdl文件,下方客户端直接引用即可 优点:针对要发布的方法生成一个wsdl文件即可,无需多余配置. 缺点:每次服务端方法发生改变都需 ...
- cxf Webservice 使用httpClient 调用
package com.wistron.wh.swpc.portal.uitl; import java.io.BufferedInputStream;import java.io.File;impo ...
- C# .NET 动态调用webservice的三种方式
转载自 百度文库 http://wenku.baidu.com/link?url=Q2q50wohf5W6UX44zqotXFEe_XOMaib4UtI3BigaNwipOHKNETloMF4ax4W ...
- Java动态调用webService,axis2动态调用webService
Java动态调用webService axis2动态调用webService >>>>>>>>>>>>>>>& ...
- Web Service学习笔记:动态调用WebService
原文:Web Service学习笔记:动态调用WebService 多数时候我们通过 "添加 Web 引用..." 创建客户端代理类的方式调用WebService,但在某些情况下我 ...
- SOA 下实现分布式 调用 cxf+ webService +动态调用
近期项目间隙 自学了 webservice 一下 是我写的 一个demo 首先我们在web.xml 里配置如下 <servlet> <servlet-name>CXFS ...
- 转载 CXF动态调用webservice
/** * * @param wsdlUrl wsdl的地址:http://localhost:8001/demo/HelloServiceDemoUrl?wsdl * @param methodNa ...
- cxf动态调用webservice设置超时,测试线程安全
Java代码 import java.util.Random; import java.util.concurrent.ArrayBlockingQueue; import java.util.con ...
随机推荐
- Mybatis和其他主流框架的整合使用
Mybatis简介 MyBatis历史 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google ...
- 牛客小白月赛43 F 全体集合
题目链接 F 全体集合 题目大意 给出\(n\)个点\(m\)条边的无向图,给出\(k\)个点上分别有一个人,每个人一次只能走到一个相邻的节点,问有没有一种可能让这些人都走到一个点. 思路 考虑使用二 ...
- 基于Python下MySQL数据库驱动
由于MySQL服务器以独立的进程运行,并通过网络对外服务,所以,需要支持Python的MySQL驱动来连接到MySQL服务器. 1.mysql-connector-python mysql-con ...
- ASP.NET Core Web API设置响应输出的Json数据格式的两种方式
前言 在ASP.NET Core Web API中设置响应输出Json数据格式有两种方式,可以通过添加System.Text.Json或Newtonsoft.JsonJSON序列化和反序列化库在应用程 ...
- 将mysql的输出文本写回mysql
1 准备工作 1.1 环境准备 操作系统:Microsoft Windows 10 专业工作站版 软件版本:Python 3.9.6 第三方包: pip install pandas2.1.0 pip ...
- Object.defineProperty用法
1.能干啥? Object.defineProperty()可以给传入的对象动态的添加或修改属性 2.怎么玩? Object.defineProperty(obj,prop,desc)它有三个参数: ...
- LeetCode224:基本计算器(栈)
解题思路: 1.双栈模拟,一个用来存数,一个用来存操作符.需要考虑 '('后面紧跟'+'.'-'这种情况 2.递归:遇到左括号开始递归,遇到右括号结束递归,返回值. 1 class Solution: ...
- NetSuite 开发日记:创建 Transfer(转账单)
经测试,截止到 2022.12.26,Transfer 只能使用 Client 脚本创建,使用服务端脚本创建报错:ReferenceError: "document" is not ...
- 【Python微信机器人】第六七篇: 封装32位和64位Python hook框架实战打印微信日志
目录修整 目前的系列目录(后面会根据实际情况变动): 在windows11上编译python 将python注入到其他进程并运行 注入Python并使用ctypes主动调用进程内的函数和读取内存结构体 ...
- MinIO客户端之head
MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc head 查看对象的前N行内容,N默认为10,命令如下: ./mc head local1/bkt1/doc ...