这里主要介绍头文件的验证、跟用户的验证
1、创建增加头文件
/*
* Description: AddSoapHeader.java
* Copyright belongs to the original author or authors.
*/
package com.cxf.client.validate;
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;
public class AddSoapHeader extends AbstractSoapInterceptor {
private static String nameURI="http://localhost:8080/cxf/webservices/reservationService";
public AddSoapHeader(){
super(Phase.WRITE);
}
public void handleMessage(SoapMessage message) throws Fault {
System.out.println("come in ClientHeaderIntercepter");
String spPassword="guo";
String spName="zhong";
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);
}
}
2、创建读取头文件
package com.cxf.filter;
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;
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 {
System.out.println("come in ServicesAuthorInterceptor ");
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;
}
NodeList nodes=head.getElementsByTagName("tns:spId");
NodeList nodepass=head.getElementsByTagName("tns:spPassword");
if("zhong".equals(nodes.item(0).getTextContent())&&("guo".equals(nodepass.item(0).getTextContent()))){
System.out.println("验证成功");
}else {
SOAPException soapException=new SOAPException("exception handle");
throw new Fault(soapException);
}
}
}
3、在applicationContext-ws.xml文件中增加拦截器
<?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/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="outMessageInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<!-- 添加头文件 进行验证 密码 -->
<jaxws:endpoint id="reservationService"
implementor="com.cxf.webService.impl.HelloWorldImpl" address="/reservationService">
<jaxws:inInterceptors>
<bean class="com.cxf.filter.ReadSoapHeader"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
4、在客户端的信息中增加头文件的拦截器
package com.cxf.client.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cxf.client.validate.AddSoapHeader;
import com.cxf.client.validate.ClientUsernamePasswordHandler;
import com.cxf.webService.HelloWorld;
import com.cxf.webService.IHelloService;
public class CxfTest {
public static void main(String[] args) {
CxfTest ct = new CxfTest();
ct.execute3();
}
public void execute3() {
List list = new ArrayList();
AddSoapHeader saopInterceptor = new AddSoapHeader();
list.add(saopInterceptor);
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloWorld.class);
String wsdlAdder = "http://localhost:8080/cxf_web/services/reservationService";
factory.setAddress(wsdlAdder);
factory.setOutInterceptors(list);
HelloWorld helloWorld = (HelloWorld) factory.create();
String str = helloWorld.sayHi("cxf hello");
System.out.println(str);
}
}