基本步骤:
1、保证服务一启动就加载Spring;
2、在web-xml中配置CXF的核心配置;
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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/>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 配置CXF -->
<!-- 下面的配置说明凡是请求路径为/studyCXF/*均交给CXFServlet处理 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<!-- CXF核心配置:org.apache.cxf.transport.servlet.CXFServlet -->
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/studyCXF/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、在Spring配置文件中导入CXF提供Schema、xml配置;
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 先写命名空间,再写具体路径 -->
<!-- web类加载路径:两类 webRoot下面的class以及lib路径下面的jar文件 -->
<import resource="classpath:/META-INF/cxf/cxf.xml"/>
<import resource="classpath:/META-INF/cxf/cxf-servlet.xml"/>
<import resource="classpath:/META-INF/cxf/cxf-extension-soap.xml"/></beans>
4、在Spring配置文件中,使用jaxws:endpoint 来暴露webService;
5、如果要添加拦截器,在jaxws:endpoint里面添加inInterceptors及outInterceptors元素。
<!-- implementor提供者-->
<!-- 设置为以i个bean前面加#号 -->
<jaxws:endpoint address="/fkjava" implementor="#helloWorldWS">
<!-- 添加拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<bean class="cxf.ws.auth.AuthInInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
拦截器
package cxf.ws.auth;
import java.util.List;
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;
import org.w3c.dom.NodeList;
//通过AbstractPhaseInterceptor决定拦截器在哪里起作用
public class AuthInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public AuthInInterceptor(){
//显示调用父类有参构造器
super(Phase.PRE_INVOKE);
}
//实现自己的拦截器,需要实现handleMessage方法
//handleMessage方法的形参SoapMessage就是拦截到的SOAP消息
//一旦获取了SOAP消息,就可以解析或修改SOAP消息
@Override
public void handleMessage(SoapMessage msg) throws Fault {
System.out.println("-----------------------------");
//得到消息的所有header
List<Header> headers=msg.getHeaders();
if(headers==null||headers.size()<1){
throw new Fault(new IllegalArgumentException("Header为空"));
}
Header firstHeader=headers.get(0);
Element ele=(Element) firstHeader.getObject();
NodeList userIds=ele.getElementsByTagName("userId");
NodeList userPasss=ele.getElementsByTagName("userPass");
if(userIds.getLength()<1){
throw new Fault(new IllegalArgumentException("用户名格式不对"));
}
if(userPasss.getLength()<1){
throw new Fault(new IllegalArgumentException("密码格式不对"));
}
String userId=userIds.item(0).getTextContent();
String userPass=userPasss.item(0).getTextContent();
if(!(userId.equals("Charles")&&userPass.equals("duyx5218"))){
throw new Fault(new IllegalArgumentException("用户名、密码不匹配"));
}
}
}