ITeye博客

CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用)

Sev7en_jun · 2012-01-12 18:23 · Web Service · 企业架构 · Markdown

CXF 入门:HelloWorld接口发布

CXF 入门: 远程接口调用

 

下面具体的webservice实现类直接用的是上面的,这里不再说明

 

CXF拦截器使用,创建一个使用SOAPHeader的安全验证
xml格式:
<soap:Header>
	<auth:authentication xmlns:auth="http://gd.chinamobile.com//authentication">
		<auth:systemID>1</auth:systemID>
		<auth:userID>test</auth:userID>
		<auth:password>test</auth:password>
	</auth:authentication>
</soap:Header>

一,首先在服务端创建一个拦截器(被调用端),需要继承org.apache.cxf.phase.AbstractPhaseInterceptor
代码如下:

import java.util.List;

import javax.xml.soap.SOAPException;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class AuthIntercetpr extends AbstractPhaseInterceptor<SoapMessage> {
	private static final Logger logger = Logger.getLogger(AuthIntercetpr.class);
    public static final String xml_namespaceUR_att = "http://gd.chinamobile.com//authentication";
	public static final String xml_header_el = "soap:Header";
	public static final String xml_authentication_el = "auth:authentication";
	public static final String xml_systemID_el = "auth:systemID";
	public static final String xml_userID_el = "auth:userID";
	public static final String xml_password_el = "auth:password";
	public AuthIntercetpr() {
		// 指定该拦截器在哪个阶段被激发
		super(Phase.PRE_INVOKE);
	}

	// 处理消息
	public void handleMessage(SoapMessage message) {
		logger.info("==================SoapMessage =" + message);
		// 获取SOAP消息的全部头
		List<Header> headers = message.getHeaders();

		if (null == headers || headers.size() < 1) {
			throw new Fault(new SOAPException("SOAP消息头格式不对哦!"));
		}
		for (Header header : headers) {
			SoapHeader soapHeader = (SoapHeader) header;
			// 取出SOAP的Header元素
			Element element = (Element) soapHeader.getObject();
			logger.info("ELEMENT =" + element.toString());
			XMLUtils.printDOM(element);
			NodeList userIdNodes = element
					.getElementsByTagName(xml_userID_el);
			NodeList pwdNodes = element
					.getElementsByTagName(xml_password_el);
			NodeList systemIdNodes = element
					.getElementsByTagName(xml_systemID_el);
			logger.info("############ 打印帐号信息 ##############");
			logger.info(userIdNodes.item(0) + "="
					+ userIdNodes.item(0).getTextContent());
			logger.info(systemIdNodes.item(0) + "="
					+ systemIdNodes.item(0).getTextContent());
			logger.info(pwdNodes.item(0) + "="
					+ pwdNodes.item(0).getTextContent());
			logger.info("############————————##############");
			if (null != userIdNodes
					&& userIdNodes.item(0).getTextContent().equels("test") ) {
				if (null != pwdNodes
						&& pwdNodes.item(0).getTextContent().equals("test")) {
					logger.info("$$$$$$$$ 认证成功");
				} else {//认证失败则抛出异常,停止继续操作
					SOAPException soapExc = new SOAPException("阁下可能不是合法用户!");
					throw new Fault(soapExc);
				}
			} else {//认证失败则抛出异常,停止继续操作
				SOAPException soapExc = new SOAPException("阁下可能不是合法用户!");
				throw new Fault(soapExc);
			}
			
		}
	}
}

二,修改cxf-beans.xml

<!--id:随意配,implementor:指定接口具体实现类,address:随意配,访问时会用到,下面会做说明-->
<!--拦截器-->
<bean id="authIntercetpr" class="unitTest.AuthIntercetpr"></bean>
<jaxws:endpoint id="HelloWorldService" implementor="com.ws.HelloWorldServiceImpl"
		address="/IHelloService">
		<!-- 在此配置调用当前ws所触发的拦截器-->
		<jaxws:inInterceptors><ref bean="authIntercetpr" /></bean> 
			
		<!--或者直接在这里写<bean  class="unitTest.AuthIntercetpr"></bean>-->
		</jaxws:inInterceptors>
</jaxws:endpoint>

到此服务端工作完毕!!!
下面是客户端(调用端)
三,这边同样创建一个拦截器,实现org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor

import java.text.SimpleDateFormat;
import java.util.Date;
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.helpers.XMLUtils;
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 {

	public static final String xml_namespaceUR_att = "http://gd.chinamobile.com//authentication";
	public static final String xml_header_el = "soap:Header";
	public static final String xml_authentication_el = "auth:authentication";
	public static final String xml_systemID_el = "auth:systemID";
	public static final String xml_userID_el = "auth:userID";
	public static final String xml_password_el = "auth:password";

	public AddSoapHeader() {
	// 指定该拦截器在哪个阶段被激发
		super(Phase.WRITE);
	}

	public void handleMessage(SoapMessage message) throws Fault {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String time = sd.format(date);
		String userId = "test";
		String sysId = "1";
		String password = "test";

		QName qname = new QName("RequestSOAPHeader");//这个值暂时不清楚具体做什么用,可以随便写

		Document doc = (Document) DOMUtils.createDocument();
		Element root = doc.createElement(xml_header_el);
		Element eSysId = doc.createElement(xml_systemID_el);
		eSysId.setTextContent(sysId);
		Element eUserId = doc.createElement(xml_userID_el);
		eUserId.setTextContent(userId);
		Element ePwd = doc.createElement(xml_password_el);
		ePwd.setTextContent(password);
		Element child = doc.createElementNS(xml_namespaceUR_att,
				xml_authentication_el);
		child.appendChild(eSysId);
		child.appendChild(eUserId);
		child.appendChild(ePwd);
		root.appendChild(child);
		XMLUtils.printDOM(root);// 只是打印xml内容到控制台,可删除
		SoapHeader head = new SoapHeader(qname, root);
		List<Header> headers = message.getHeaders();
		headers.add(head);
		
	}

}

四,具体调用ws的类代码


	private static final String webServiceConTimeout = "6000";
	private static final String webServiceRevTimeout = "6000";
	。。。。。。。
        HelloWorldServiceImplService hello = new HelloWorldServiceImplService();  
        HelloWorldService service = hello.getHelloWorldServiceImplPort();
        //以上什么意思请参考:http://learning.iteye.com/admin/blogs/1333223
		Client clientProxy = ClientProxy.getClient(service);//通过目标ws获取代理
		//注入拦截器,getOutInterceptors代表调用服务端时触发,getInInterceptors就是被调用才触发
		clientProxy.getOutInterceptors().add(ash);
		// 超时时间设置
		HTTPConduit http = (HTTPConduit) clientProxy.getConduit();
		HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
		httpClientPolicy.setConnectionTimeout(Integer
				.valueOf(webServiceConTimeout));
		httpClientPolicy.setReceiveTimeout(Integer
				.valueOf(webServiceRevTimeout));
		httpClientPolicy.setAllowChunking(false);
		http.setClient(httpClientPolicy);
        //以上插入点超时设置方式
		//下面这行代码是具体调用服务段的deleteTeskTask()
		CallResult cResult = service.deleteTeskTask("1223");
				
		。
		。
		
客户端代码到此结束

五,还有一种方式是通过JaxWsProxyFactoryBean方式,注册拦截器及实例化ws,代码如下:

private static final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

AddSoapHeader ash = new AddSoapHeader();
		ArrayList list = new ArrayList();
		// 添加soap header 信息
		list.add(ash);
		//注入拦截器,getOutInterceptors代表调用服务端时触发,getInInterceptors就是被调用才触发
		 factory.setOutInterceptors(list);
		 factory.setServiceClass(HelloWorldService.class);//实例化ws
		 factory.setAddress("http://xxx.xxx.xxx.xxx:8004/services/IHelloService");
		 Object obj = factory.create();
		 HelloWorldService service = (HelloWorldService) obj;
		 //下面这行代码是具体调用服务段的deleteTeskTask()
		CallResult cResult = service.deleteTeskTask("1223");
		
##########这段代码可替代步骤(四)#####

到此全部工作结束
具体一些概念还请自己baidu/google

评论(16)

  1. #16 楼 · a807966224 · 2018-02-07 13:08

    还是 多谢楼主 总结的挺好的 !!!

  2. #15 楼 · a807966224 · 2018-02-07 13:08

    然后 通过 SOAPMessage.getHeader(qname); 取出相应的 header头信息,用来认证,这样可以避免从 SOAPMessage里取出多个头信息, 认证多次

  3. #14 楼 · a807966224 · 2018-02-07 12:55

    我也是接触这东西不久,QName qname = new QName ("namespace路径", "localpart");

  4. #13 楼 · a807966224 · 2018-02-07 12:52

    楼主 不知道你有没有出现 从headers 里取出来长度为2的头信息, 这样会在一次请求里 出现两次认证 很奇怪啊

  5. #12 楼 · jiangwenxian · 2013-10-29 17:08

    在log.info(userIdNodes.item(0) + "="
    + userIdNodes.item(0).getTextContent()); 报空指针异常

  6. #11 楼 · lloWorld · 2013-06-06 12:06

    您好,xml格式:
    soap:Header
    <auth:authentication xmlns:auth="http://gd.chinamobile.com//authentication">
    auth:systemID1</auth:systemID>
    auth:userIDtest</auth:userID>
    auth:passwordtest</auth:password>
    </auth:authentication>
    </soap:Header> 这个如何生成的?

  7. #10 楼 · q269223 · 2013-03-15 17:34

    Caused by: javax.xml.soap.SOAPException: SOAP消息头格式不对哦! ... 27 more 在客户端传输SOAPHeader的时候压根就没传过去。希望楼主可以解释一下。··

  8. #9 楼 · Sev7en_jun · 2012-11-04 12:31

    liuwuhen writes:

    你好,楼主你的例子,实验成了吗?

    测试ok的

  9. #8 楼 · liuwuhen · 2012-10-30 17:03

    你好,楼主你的例子,实验成了吗?

  10. #7 楼 · liuxueping1987 · 2012-05-07 13:56

    按照楼主的操作做,在以下代码报异常。 if (null == headers || headers.size() < 1) {
    50. throw new Fault(new SOAPException("SOAP消息头格式不对哦!"));
    51. }

  11. #6 楼 · wenqxin · 2012-01-14 10:43

    Sev7en_jun writes:

    [quote=Sev7en_jun][quote=wenqxin]为什么不通过wss4j 提供的回调类进行实现呢,cxf 已经实现了啊。配置会比这个简单[/quote]

    其实对于复杂程度都差不多一样,这个部分代码也可以直接通过配置来实现,这里是通过自定义的xml信息来校验,回调函数应该是定死了的[/quote]

    [color=red]PS:不知道那种可以把安全级别设置更高一点??[/color]

    wss4j 提供了不同安全级别的认证方式,常用的就是usernameToken(用户名和密码),你还可以通过授权文件等方式。

  12. #5 楼 · Sev7en_jun · 2012-01-13 19:01

    Sev7en_jun writes:

    [quote=wenqxin]为什么不通过wss4j 提供的回调类进行实现呢,cxf 已经实现了啊。配置会比这个简单[/quote]

    其实对于复杂程度都差不多一样,这个部分代码也可以直接通过配置来实现,这里是通过自定义的xml信息来校验,回调函数应该是定死了的

    PS:不知道那种可以把安全级别设置更高一点??

  13. #4 楼 · Sev7en_jun · 2012-01-13 19:00

    wenqxin writes:

    为什么不通过wss4j 提供的回调类进行实现呢,cxf 已经实现了啊。配置会比这个简单

    其实对于复杂程度都差不多一样,这个部分代码也可以直接通过配置来实现,这里是通过自定义的xml信息来校验,回调函数应该是定死了的

  14. #3 楼 · wenqxin · 2012-01-13 09:36

    为什么不通过wss4j 提供的回调类进行实现呢,cxf 已经实现了啊。配置会比这个简单

  15. #2 楼 · Sev7en_jun · 2012-01-12 23:42

    baoyun618 writes:

    正想学习cxf呢,借楼主好文

    过奖了,我也是参考网上资料做的,还很多不清楚 呵呵

  16. #1 楼 · baoyun618 · 2012-01-12 22:32

    正想学习cxf呢,借楼主好文

评论功能维护中,当前仅展示历史评论。