http://huiseyiyu.iteye.com/blog/1176306
服务端修改
1.新建一个拦截器(Interceptor)
package hs.cxf.soapHeader;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.NodeList;
/**
*
* @Title:获取soap头信息
*
* @Description:
*
* @Copyright:
*
* @author zz
* @version 1.00.000
*
*/
public class ReadSoapHeader extends AbstractPhaseInterceptor<SoapMessage> {
private SAAJInInterceptor saa = new SAAJInInterceptor();
public ReadSoapHeader() {
super(Phase.PRE_PROTOCOL);
getAfter().add(SAAJInInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage mess = message.getContent(SOAPMessage.class);
if (mess == null) {
saa.handleMessage(message);
mess = message.getContent(SOAPMessage.class);
}
SOAPHeader head = null;
try {
head = mess.getSOAPHeader();
} catch (SOAPException e) {
e.printStackTrace();
}
if (head == null) {
return;
}
try {
//读取自定义的节点
NodeList nodes = head.getElementsByTagName("tns:spId");
NodeList nodepass = head.getElementsByTagName("tns:spPassword");
//获取节点值,简单认证
if (nodes.item(0).getTextContent().equals("wdw")) {
if (nodepass.item(0).getTextContent().equals("wdwsb")) {
System.out.println("认证成功");
}
} else {
SOAPException soapExc = new SOAPException("认证错误");
throw new Fault(soapExc);
}
} catch (Exception e) {
SOAPException soapExc = new SOAPException("认证错误");
throw new Fault(soapExc);
}
}
}
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.NodeList;
/**
*
* @Title:获取soap头信息
*
* @Description:
*
* @Copyright:
*
* @author zz
* @version 1.00.000
*
*/
public class ReadSoapHeader extends AbstractPhaseInterceptor<SoapMessage> {
private SAAJInInterceptor saa = new SAAJInInterceptor();
public ReadSoapHeader() {
super(Phase.PRE_PROTOCOL);
getAfter().add(SAAJInInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage mess = message.getContent(SOAPMessage.class);
if (mess == null) {
saa.handleMessage(message);
mess = message.getContent(SOAPMessage.class);
}
SOAPHeader head = null;
try {
head = mess.getSOAPHeader();
} catch (SOAPException e) {
e.printStackTrace();
}
if (head == null) {
return;
}
try {
//读取自定义的节点
NodeList nodes = head.getElementsByTagName("tns:spId");
NodeList nodepass = head.getElementsByTagName("tns:spPassword");
//获取节点值,简单认证
if (nodes.item(0).getTextContent().equals("wdw")) {
if (nodepass.item(0).getTextContent().equals("wdwsb")) {
System.out.println("认证成功");
}
} else {
SOAPException soapExc = new SOAPException("认证错误");
throw new Fault(soapExc);
}
} catch (Exception e) {
SOAPException soapExc = new SOAPException("认证错误");
throw new Fault(soapExc);
}
}
}
2.配置文件中新增拦截器配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jaxWsServiceFactoryBean"
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
<property name="dataBinding" ref="aegisBean" />
</bean>
<bean id="aegisBean"
class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<jaxws:endpoint id="CollectiveServices"
implementor="hs.cxf.server.WebServiceSampleImpl" address="/HelloWorld">
<jaxws:inInterceptors>
<!-- 日志拦截器 -->
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 自定义拦截器 -->
<bean class="hs.cxf.soapHeader.ReadSoapHeader"/>
</jaxws:inInterceptors>
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean"/>
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jaxWsServiceFactoryBean"
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
<property name="dataBinding" ref="aegisBean" />
</bean>
<bean id="aegisBean"
class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<jaxws:endpoint id="CollectiveServices"
implementor="hs.cxf.server.WebServiceSampleImpl" address="/HelloWorld">
<jaxws:inInterceptors>
<!-- 日志拦截器 -->
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 自定义拦截器 -->
<bean class="hs.cxf.soapHeader.ReadSoapHeader"/>
</jaxws:inInterceptors>
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean"/>
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
服务端的配置就告一段落了,接下来是客户端的修改
客户端
1.同样新增一个Interceptor
package hs.cxf.client.SoapHeader;
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.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* @Title:在发送消息前,封装Soap Header 信息
*
* @Description:
*
* @Copyright:
*
* @author zz
* @version 1.00.000
*
*/
public class AddSoapHeader extends AbstractSoapInterceptor {
private static String nameURI="http://127.0.0.1:8080/cxfTest/ws/HelloWorld";
public AddSoapHeader(){
super(Phase.WRITE);
}
public void handleMessage(SoapMessage message) throws Fault {
String spPassword="wdwsb";
String spName="wdw";
QName qname=new QName("RequestSOAPHeader");
Document doc=DOMUtils.createDocument();
//自定义节点
Element spId=doc.createElement("tns:spId");
spId.setTextContent(spName);
//自定义节点
Element spPass=doc.createElement("tns:spPassword");
spPass.setTextContent(spPassword);
Element root=doc.createElementNS(nameURI, "tns:RequestSOAPHeader");
root.appendChild(spId);
root.appendChild(spPass);
SoapHeader head=new SoapHeader(qname,root);
List<Header> headers=message.getHeaders();
headers.add(head);
System.out.println(">>>>>添加header<<<<<<<");
}
}
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.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* @Title:在发送消息前,封装Soap Header 信息
*
* @Description:
*
* @Copyright:
*
* @author zz
* @version 1.00.000
*
*/
public class AddSoapHeader extends AbstractSoapInterceptor {
private static String nameURI="http://127.0.0.1:8080/cxfTest/ws/HelloWorld";
public AddSoapHeader(){
super(Phase.WRITE);
}
public void handleMessage(SoapMessage message) throws Fault {
String spPassword="wdwsb";
String spName="wdw";
QName qname=new QName("RequestSOAPHeader");
Document doc=DOMUtils.createDocument();
//自定义节点
Element spId=doc.createElement("tns:spId");
spId.setTextContent(spName);
//自定义节点
Element spPass=doc.createElement("tns:spPassword");
spPass.setTextContent(spPassword);
Element root=doc.createElementNS(nameURI, "tns:RequestSOAPHeader");
root.appendChild(spId);
root.appendChild(spPass);
SoapHeader head=new SoapHeader(qname,root);
List<Header> headers=message.getHeaders();
headers.add(head);
System.out.println(">>>>>添加header<<<<<<<");
}
}
2.客户端调用程序修改
package hs.cxf.client;
import hs.cxf.client.SoapHeader.AddSoapHeader;
import java.util.ArrayList;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
/**
* @Title:
*
* @Description:
*
* @Copyright:
*
* @author zz
* @version 1.00.000
*
*/
public class TestClient {
/**
* 测试1
*/
@SuppressWarnings("unchecked")
public void testSend1() {
try {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
ArrayList<Interceptor> list = new ArrayList<Interceptor>();
// 添加soap header
list.add(new AddSoapHeader());
// 添加soap消息日志打印
list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
factory.setOutInterceptors(list);
factory.setServiceClass(WebServiceSample.class);
factory.setAddress("http://127.0.0.1:8080/cxfTest/ws/HelloWorld");
Object obj = factory.create();
System.out.println(obj == null ? "NULL" : obj.getClass().getName());
if (obj != null) {
WebServiceSample ws = (WebServiceSample) obj;
String str = ws.say("test");
System.out.println(str);
str = ws.say("1111");
System.out.println(str);
User u = new User();
JAXBElement<String> je = new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "name"), String.class, "张三");
u.setName(je);
str = ws.sayUserName(u);
System.out.println(str);
// 通过对象来交互
ReqBean req = new ReqBean();
req.setExp(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "exp"), String.class,
"<exp>111<exp>"));
req.setSeqId(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "seqId"), String.class,
"12345678"));
RespBean resp = ws.action(req);
System.out.println("resp_id:" + resp.getRespId().getValue());
System.out.println("resp_exp:" + resp.getExp().getValue());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 测试2
*/
@SuppressWarnings("unchecked")
public void testSend2() {
String webServiceUrl = "http://127.0.0.1:8080/cxfTest/ws/HelloWorld";
String webServiceConTimeout = "60000";
String webServiceRevTimeout = "60000";
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
ArrayList<Interceptor> list = new ArrayList<Interceptor>();
// 添加soap header 信息
list.add(new AddSoapHeader());
// 添加soap消息日志打印
list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
factory.setOutInterceptors(list);
factory.setServiceClass(WebServiceSample.class);
factory.setAddress(webServiceUrl);
WebServiceSample service = (WebServiceSample) factory.create();
//超时时间设置
Client clientP = ClientProxy.getClient(service);
HTTPConduit http = (HTTPConduit) clientP.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(Integer
.valueOf(webServiceConTimeout));
httpClientPolicy.setReceiveTimeout(Integer
.valueOf(webServiceRevTimeout));
httpClientPolicy.setAllowChunking(false);
http.setClient(httpClientPolicy);
// 通过对象来交互
ReqBean req = new ReqBean();
req.setExp(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "exp"), String.class,
"<exp>111<exp>"));
req.setSeqId(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "seqId"), String.class,
"12345678"));
System.out.println(">>>>>>发送消息<<<<<<<<<");
RespBean resp = service.action(req);
System.out.println("resp_id:" + resp.getRespId().getValue());
System.out.println("resp_exp:" + resp.getExp().getValue());
}
/**
* @param args
*/
public static void main(String[] args) {
TestClient tc = new TestClient();
tc.testSend1();
System.out.println(">>>>>>>>>>>>2<<<<<<<<<<<<<");
tc.testSend2();
System.out.println(">>>>>>>>>>>>END<<<<<<<<<<<<<");
}
}
import hs.cxf.client.SoapHeader.AddSoapHeader;
import java.util.ArrayList;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
/**
* @Title:
*
* @Description:
*
* @Copyright:
*
* @author zz
* @version 1.00.000
*
*/
public class TestClient {
/**
* 测试1
*/
@SuppressWarnings("unchecked")
public void testSend1() {
try {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
ArrayList<Interceptor> list = new ArrayList<Interceptor>();
// 添加soap header
list.add(new AddSoapHeader());
// 添加soap消息日志打印
list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
factory.setOutInterceptors(list);
factory.setServiceClass(WebServiceSample.class);
factory.setAddress("http://127.0.0.1:8080/cxfTest/ws/HelloWorld");
Object obj = factory.create();
System.out.println(obj == null ? "NULL" : obj.getClass().getName());
if (obj != null) {
WebServiceSample ws = (WebServiceSample) obj;
String str = ws.say("test");
System.out.println(str);
str = ws.say("1111");
System.out.println(str);
User u = new User();
JAXBElement<String> je = new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "name"), String.class, "张三");
u.setName(je);
str = ws.sayUserName(u);
System.out.println(str);
// 通过对象来交互
ReqBean req = new ReqBean();
req.setExp(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "exp"), String.class,
"<exp>111<exp>"));
req.setSeqId(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "seqId"), String.class,
"12345678"));
RespBean resp = ws.action(req);
System.out.println("resp_id:" + resp.getRespId().getValue());
System.out.println("resp_exp:" + resp.getExp().getValue());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 测试2
*/
@SuppressWarnings("unchecked")
public void testSend2() {
String webServiceUrl = "http://127.0.0.1:8080/cxfTest/ws/HelloWorld";
String webServiceConTimeout = "60000";
String webServiceRevTimeout = "60000";
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
ArrayList<Interceptor> list = new ArrayList<Interceptor>();
// 添加soap header 信息
list.add(new AddSoapHeader());
// 添加soap消息日志打印
list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
factory.setOutInterceptors(list);
factory.setServiceClass(WebServiceSample.class);
factory.setAddress(webServiceUrl);
WebServiceSample service = (WebServiceSample) factory.create();
//超时时间设置
Client clientP = ClientProxy.getClient(service);
HTTPConduit http = (HTTPConduit) clientP.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(Integer
.valueOf(webServiceConTimeout));
httpClientPolicy.setReceiveTimeout(Integer
.valueOf(webServiceRevTimeout));
httpClientPolicy.setAllowChunking(false);
http.setClient(httpClientPolicy);
// 通过对象来交互
ReqBean req = new ReqBean();
req.setExp(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "exp"), String.class,
"<exp>111<exp>"));
req.setSeqId(new JAXBElement<String>(new QName(
"http://bean.cxf.hs", "seqId"), String.class,
"12345678"));
System.out.println(">>>>>>发送消息<<<<<<<<<");
RespBean resp = service.action(req);
System.out.println("resp_id:" + resp.getRespId().getValue());
System.out.println("resp_exp:" + resp.getExp().getValue());
}
/**
* @param args
*/
public static void main(String[] args) {
TestClient tc = new TestClient();
tc.testSend1();
System.out.println(">>>>>>>>>>>>2<<<<<<<<<<<<<");
tc.testSend2();
System.out.println(">>>>>>>>>>>>END<<<<<<<<<<<<<");
}
}
相关推荐
以上步骤展示了如何在CXF客户端中添加SOAP Header,这个过程涉及到CXF客户端的配置、自定义Header处理以及服务调用。通过这样的方式,我们可以灵活地控制SOAP请求中的Header信息,满足各种服务交互的需求。记得根据...
根据提供的标题、描述、标签及部分内容,我们可以详细探讨CXF框架中SOAP Header的使用方法。 ### CXF SOAP Header 使用详解 #### 一、引言 Apache CXF 是一个开源项目,提供了一套全面的框架来构建和服务 SOA 架构...
描述中提到的博客链接可能提供了一个详细的实践案例,展示了如何在CXF中配置和处理带有自定义header的SOAP请求。通常,这涉及到以下步骤: 1. **创建服务接口**:首先,你需要定义一个SOAP服务接口,这个接口将暴露...
将自定义的Interceptor添加到CXF服务端或客户端的配置中。如果是Spring配置,可以在`cxf.xml`文件中添加以下代码: ```xml <cxf:bus> <cxf:outInterceptors> </cxf:outInterceptors> </cxf:bus> ``` 4. *...
CXF提供了一套全面的工具和服务,包括代码生成器、WSDL到Java绑定、Java到WSDL绑定、SOAP消息处理和安全支持等。它的优势在于对WS-I兼容性、强大的错误处理和调试能力。 3. **创建SOAP服务** 使用CXF,我们可以从...
标题 "cxf-soap内容解析、更改(助记)" 指向的是Apache CXF框架下关于SOAP消息的处理,特别是如何解析和修改SOAP内容的主题。Apache CXF是一个开源服务框架,它允许开发人员创建和消费Web服务,支持SOAP和RESTful ...
在IT行业中,CXF是一个广泛使用的开源框架,它支持服务导向架构(SOA)并通过SOAP协议提供Web服务。Spring框架则是Java应用开发的核心框架,它提供了丰富的功能来管理bean、处理事务、AOP(面向切面编程)等。将CXF...
在这个"CXF soap例子源码"中,我们可以深入理解如何使用CXF框架来实现基于SOAP的Web服务。 首先,SOAP是一种基于XML的协议,允许应用程序通过HTTP或其他传输协议进行通信。在CXF中,你可以使用Java API for RESTful...
【标题】"CXF-SOAP搭建WebService服务端demo"主要涵盖了使用Apache CXF框架、Spring框架、Maven构建工具以及SOAP协议来创建一个Web服务端的实例。这个过程涉及了多个关键知识点,下面将详细阐述。 【SOAP】:简单...
CXF框架是Apache组织提供的一款开源工具,它简化了在Java环境中创建和消费SOAP Web服务的过程。本篇文章将深入探讨如何使用CXF框架在SOAP协议下开发Web服务。 首先,我们需要理解SOAP的基本概念。SOAP是一种轻量级...
Header中通常包含身份验证或路由信息,而Body则包含实际的业务数据。Envelope是SOAP消息的顶级元素,定义了整个消息的结构。 在"拼接SOAP"过程中,开发者需要根据服务接口的要求构造SOAP请求的XML字符串。这通常...
【SOAP简介】 SOAP(Simple Object Access ...同时,也提到了CXF这个强大的工具,它在SOAP Web服务开发中的重要作用。理解SOAP和CXF可以帮助开发者更好地构建和使用分布式应用程序,实现不同系统之间的互操作性。
在本文中,我们将深入探讨如何在Apache CXF框架中实现客户端和服务器端的权限验证。Apache CXF是一个开源的Java框架,主要用于构建和开发服务导向架构(SOA)和服务级应用程序。它支持多种Web服务标准,包括SOAP、...
然而,“soap-build”更专注于代码生成,特别是在持续集成(CI)和自动化构建流程中,它可以无缝集成到构建脚本中,自动生成测试用例所需的SOAP请求。 在使用“soap-build”时,你需要有SOAP服务的WSDL(Web Service ...
3. **WS-Security**:为了在SOAP级别提供安全,业界制定了WS-Security标准,它定义了如何在SOAP Header中添加安全信息,如数字签名、用户名令牌、X.509证书等,用于身份验证和消息加密。 4. **XML加密**:在SOAP中...
在Eclipse中使用CXF,你可以创建一个CXF Web服务项目,然后添加服务接口和实现。 CXF提供了WSImport工具,可以从WSDL文件生成Java代码,也可以使用JAX-WS注解来定义服务接口。在完成服务的编写后,可以使用CXF的内置...
1. 添加 CXF 相关依赖到项目构建配置(例如 Maven 或 Gradle)。 2. 创建服务接口和其实现类。 3. 使用 CXF 提供的工具生成 WSDL 文件,或者手动编写 WSDL 描述服务接口。 4. 配置 CXF 组件,如 Servlet 或 JAX-WS ...
在Web服务开发中,服务器端接收到SOAP请求后,首先解析SOAP消息,提取Body内容,然后根据Body中的信息执行相应的业务逻辑。同样,客户端在发送请求时,也需要构建SOAP消息的Body部分,确保包含正确的请求参数。 6....