1.自定义拦截器
package com.cxf.interceptor;
import java.util.List;
import javax.xml.soap.SOAPException;
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.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
//AbstractPhaseInterceptor可以指定拦截器在那个阶段起作用
public class UserInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
Logger logger=Logger.getLogger(UserInterceptor.class);
//显示调用父类的有参数的构造器就不会在隐示调用父类无参构造器
public UserInterceptor() {
super(Phase.PRE_INVOKE);//在调用之前拦截soap消息
}
@Override//message就是被拦截到的消息
public void handleMessage(SoapMessage msg) throws Fault {
//获取所有的头元素
List<Header> headers=msg.getHeaders();
if(headers==null || headers.size()==0){
throw new Fault(new SOAPException("缺少Head 元素"));
}
//获取第一个头元素(w3c.dom.Element)
Header firstHead= headers.get(0);
Element element= (Element) firstHead.getObject();
NodeList nameList= element.getElementsByTagName("userName");
NodeList passWordList= element.getElementsByTagName("passWord");
if(nameList.getLength()!=1 || passWordList.getLength()!=1){
throw new Fault(new SOAPException("用户名密码格式有问题"));
}
// 获取用户名密码
String userName=nameList.item(0).getTextContent();
String passWord=passWordList.item(0).getTextContent();
logger.info(userName+":"+passWord);
if(! userName.equals("rain") || ! passWord.equals("rain"))
throw new Fault(new IllegalArgumentException("用户名密错误"));
}
}
2.Web.Xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/context/*.xml</param-value> </context-param> <!-- Spring监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- CXFServlet 的访问地址--> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/WebService/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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" /> <!-- Service --> <bean id="UserServiceImpl" class="com.cxf.service.impl.UserServiceImp"></bean> <!-- WebService --> <bean id="UserServiceWs" class="com.cxf.ws.impl.UserServiceWs"> <property name="userService" ref="UserServiceImpl"></property> </bean> <!-- 拦截器 --> <bean id="userInterceptor" class="com.cxf.interceptor.UserInterceptor"/> <!-- 发布的地址,实现的接口,实现类 ,拦截器--> <jaxws:server id="userManager" address="/userManager" serviceClass="com.cxf.ws.IUserServiceWs" > <jaxws:serviceBean> <ref bean="UserServiceWs"/> </jaxws:serviceBean> <jaxws:inInterceptors> <ref bean="userInterceptor"/> </jaxws:inInterceptors> </jaxws:server> </beans>