1........................服务器端编写验证类
package Interceptor;
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 AuthorInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private SAAJInInterceptor saa = new SAAJInInterceptor();
public AuthorInterceptor() {
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 (Exception 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 soapExc = new SOAPException("认证错误");
throw new Fault(soapExc);
}
System.out.println("6");
}
}
<!-- 公共过滤器 -->
<cxf:bus>
<cxf:inInterceptors>
<ref bean="AuthorInterceptor" />
</cxf:inInterceptors>
</cxf:bus>
这样客户端在进行调用接口的时候会自动进行用户验证
3.................................编写客户端的验证代码
package InterCeptor;
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.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class HeaderIntercepter extends AbstractSoapInterceptor {
private String qname;
public HeaderIntercepter() {
super(Phase.WRITE);
}
public void handleMessage(SoapMessage soapMessage) throws Fault {
System.out.println("come in ClientHeaderIntercepter");
String spName = "zhong";
String spPassword = "guo";
QName name = 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(qname, "tns:RequestSOAPHeader");
root.appendChild(spId);
root.appendChild(spPass);
SoapHeader head = new SoapHeader(name, root);
List<Header> headers = soapMessage.getHeaders();
headers.add(head);
}
private Object getHeader() {
QName qName = new QName("", "", "");
Document document = DOMUtils.createDocument();
Element element = document.createElementNS(qname, "RequestSOAPHeader");
Element token = document.createElement("token");
token.setTextContent("kkkkk");
// element.appendChild(token);
SoapHeader header = new SoapHeader(qName, token);
return (header);
}
public String getQname() {
return qname;
}
public void setQname(String qname) {
this.qname = qname;
}
}
4................................通过WSDl2Java自动生成服务端接口代理
5...............................客户端配置Spring的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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="inMessageInterceptor" class="InterCeptor.HeaderIntercepter">
<property name="qname"
value="http://localhost:8080/SpringCXF/services/GetUserInfo" />
</bean>
<jaxws:client id="com.client.GetUserInfo" serviceClass="objectinterface.GetUserInfo"
address="http://localhost:8080/SpringCXF/services/GetUserInfo">
<jaxws:outInterceptors>
<ref bean="inMessageInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>
6.......................最后直接进行接口调用就OK了
package com.client;
import objectinterface.GetUserInfo;
import objectinterface.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class GetUserInfoTest {
public static void main(String[] args) throws Exception {
User user = new User();
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
GetUserInfo userInfo = ctx.getBean("com.client.GetUserInfo", GetUserInfo.class);
String xmlDoc = userInfo.xmlInfo();
user = userInfo.getUserInfo("sa");
System.out.println(user.getUsername());
System.out.println("success");
System.out.println(xmlDoc);
}
}