cxf添加身份认证


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:Envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" >
     <soap:Header>
         <AuthToken>
             <username>username</username>
             <password>password</password>
         </AuthToken>
     </soap:Header>
     <soap:Body>
         <ns2:message xmlns:ns2= "http://spring.cxf.what21.com/" >
             <arg0>i am client!</arg0>
         </ns2:message>
     </soap:Body>
</soap:Envelope>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值