首先使用Spring方式发布成功WebService,再在客户端和服务器端引入出拦截器和入拦截器,引入的方式是写在配置文件中的。
客户端:
自定义的拦截器
package ws.client.interceptor;
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.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.xml.utils.DOMHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class MyClientInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
//自定义的校验字段,姓名和密码
private String name;
private String pwd;
public MyClientInterceptor(String name,String pwd) {
super(Phase.PRE_PROTOCOL);//配置拦截时机,一定要有,准备协议化得时候拦截
this.name=name;
this.pwd=pwd;
}
//封装数据到消息里,格式如下
/* <Envelop>
* <head> //里面的标签可以有多个
* <atguigu> //自定义标签,随意
* <name>zhangsan</name>
* <pwd>123123</pwd>
* </atguigu>
* <atguigu1> //自定义标签,随意
* <name>zhangsan</name>
* <pwd>123123</pwd>
* </atguigu1>
* </head>
* <body>
* <sayHello>
* <arg0>BOB</arg0>
* </sayHello>
* </body>
* </Envelop>
*
*
*
*
*
*
*
*
*/
@Override
public void handleMessage(SoapMessage msg) throws Fault {
List<Header> headers=msg.getHeaders(); //创建Head
//创建head里的元素,添加到head
Document document=DOMHelper.createDocument();
Element atguigu=document.createElement("atguigu");
Element name=document.createElement("name");
name.setTextContent(this.name);
atguigu.appendChild(name);
Element pwd=document.createElement("pwd");
pwd.setTextContent(this.pwd);
atguigu.appendChild(pwd);
headers.add(new Header(new QName("atguigu"), atguigu));//QName里的参数要和标签名一样
System.out.println("client:handleMessage");
}
}
wsimport生成的客户端代码:
GetUserById.java
GetUserByIdResponse.java
ObjectFactory.java
package-info.java
UserBean.java
UserWS.java
UserWSImplService.java
客户端的配置:
<?xml version="1.0" encoding="UTF-8"?>
<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/jaxws">
<jaxws:client id="userClient"
serviceClass= "ws.spring.server.UserWS"
address= "http://localhost:8080/ws_spring_interceptor_server/userws">
<!-- 添加客户端出拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<bean class="ws.client.interceptor.MyClientInterceptor">
<constructor-arg name="name" value="zhangsan"/>
<constructor-arg name="pwd" value="123"/>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
</beans>
客户端测试类:
package ws.spring.server;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(
new String[]{"client-beans.xml"});
UserWS userWS=(UserWS) context.getBean("userClient");
UserBean userBean=userWS.getUserById(1);
System.out.println("Client:"+userBean);
}
}
服务器端:
自定义的拦截器:
package ws.server.interceptor;
import javax.xml.namespace.QName;
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.w3c.dom.Element;
/**
* 检查用户名称和密码
* @author
*
*/
public class MyServerInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public MyServerInterceptor() {
super(Phase.PRE_PROTOCOL);
// TODO Auto-generated constructor stub
}
@Override
public void handleMessage(SoapMessage msg) throws Fault {
// 读header里的数据
Header header=msg.getHeader(new QName("atguigu"));//参数和客户端传的一样
if(header!=null){
Element atguigu=(Element) header.getObject();
String name=atguigu.getElementsByTagName("name").item(0).getTextContent();
String pwd=atguigu.getElementsByTagName("pwd").item(0).getTextContent();
if(name.equals("zhangsan")&&pwd.equals("123")){
System.out.println("server:check ok");
return;
}
}
//没通过校验,抛出异常
System.out.println("没有通过校验");
throw new Fault(new RuntimeException("用户名或密码不对"));
}
}
bean:
package ws.spring.server;
public class UserBean {
private int id;
private String name;
public UserBean(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserBean [id=" + id + ", name=" + name + "]";
}
}
SEI:
package ws.spring.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface UserWS {
@WebMethod
public UserBean getUserById(int id);
}
SEIImpl
package ws.spring.server;
import javax.jws.WebService;
@WebService
public class UserWSImpl implements UserWS {
public UserWSImpl(){
System.out.println("初始化 UserWSImpl");
}
@Override
public UserBean getUserById(int id) {
System.out.println("server getUserById:"+id);
return new UserBean(1, "张三");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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/jaxws">
<!-- 引cxf的一些核心配置 -->
<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" />
<jaxws:endpoint
id="userWS"
implementor="ws.spring.server.UserWSImpl"
address="/userws">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<bean class="ws.server.interceptor.MyServerInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ws_spring_interceptor_server</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>