CXF实现用户验证(相比前面的另外的一种方法完全Spring)

12 篇文章 0 订阅
10 篇文章 0 订阅

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");

			 
			 }
	}


 

2...................................配置公共过滤器xml文件
	<!-- 公共过滤器 -->
	<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);
	}
}


 

7.........................完成。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值