1、服务端配置,java部分
package com.huating.outinterface;
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.Node;
import org.w3c.dom.NodeList;
import com.huating.jfp.util.Md5AndSha;
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private SAAJInInterceptor saa = new SAAJInInterceptor();
public AuthInterceptor() {
super(Phase.PRE_PROTOCOL);
getAfter().add(SAAJInInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage soapMsg = message.getContent(SOAPMessage.class);
if (soapMsg == null){
saa.handleMessage(message);
soapMsg = message.getContent(SOAPMessage.class);
}
SOAPHeader header = null;
try {
header = soapMsg.getSOAPHeader();
System.out.println(header.toString());
NodeList userNodes = header.getElementsByTagName("username");
NodeList passNodes = header.getElementsByTagName("password");
if(userNodes!=null && passNodes!=null){
Node userNode = userNodes.item(0);
Node passNode = passNodes.item(0);
if(userNode.getFirstChild() != null
&& passNode.getFirstChild() != null
&& userNode.getFirstChild().getNodeValue() != null
&& passNode.getFirstChild().getNodeValue() != null
&& userNode.getFirstChild().getNodeValue().equals("huating")){
String pwd = passNode.getFirstChild().getNodeValue();
String password = new Md5AndSha().convertMD5(pwd);
if(password.equals("a9e711c14d6897e6015bfa38b9ee6166")){
return ;
}
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
throw new Fault(new Exception("auth error."));
}
public static void main(String[] args) {
String password = new Md5AndSha().convertMD5("huating");
System.out.println(password);
}
}
服务端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"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
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
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.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="inInterceptor" class="com.huating.outinterface.AuthInterceptor"/>
<!-- Start 供WebService调用的业务Service -->
<bean id="test12333WS"
class="com.huating.outinterface.Test12333WSIMP">
</bean>
<jaxws:server id="test12333WSServer" address="/test12333WS">
<jaxws:serviceBean>
<ref bean="test12333WS" />
</jaxws:serviceBean>
<!-- 认证处理 -->
<jaxws:inInterceptors>
<ref bean="inInterceptor"/>
</jaxws:inInterceptors>
</jaxws:server>
</beans>
客户端配置java代码:
package com.huating.outinterface;
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 ClientAuthInterceptor extends AbstractSoapInterceptor {
public ClientAuthInterceptor(){
super(Phase.WRITE);
}
public void handleMessage(SoapMessage soapMessage) throws Fault {
Document doc=DOMUtils.createDocument();
// 根节点
Element rootEle=doc.createElementNS("", "AuthToken");
// 用户ID
Element userEle = doc.createElement("username");
userEle.setTextContent("huating");
rootEle.appendChild(userEle);
// 密码
Element passEle = doc.createElement("password");
passEle.setTextContent("huating");
rootEle.appendChild(passEle);
// 添加到头
List<Header> headers = soapMessage.getHeaders();
QName qname=new QName("AuthToken");
SoapHeader head=new SoapHeader(qname, rootEle);
headers.add(head);
}
}
package com.huating.outinterface;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class TestS {
/**
* @param args
*/
public static void main(String[] args) {
JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http://10.250.196.93:8080/fcp/services/test12333WS");
soapFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
soapFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
// 添加拦截器
soapFactoryBean.getOutInterceptors().add(new ClientAuthInterceptor());
soapFactoryBean.setServiceClass(Test12333WS.class);
Object o = soapFactoryBean.create();
Test12333WS helloService = (Test12333WS)o;
String result = null;
try {
result = helloService.say("11");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
}
}
最终的header格式:
<soap:Header>
<AuthToken>
<username>username</username>
<password>password</password>
</AuthToken>
</soap:Header>
<soap:Body>
<arg0>i am client!</arg0>
</ns2:message>
</soap:Body>
</soap:Envelope>