加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF webService应用---服务端

发布时间:2020-12-16 22:47:53 所属栏目:安全 来源:网络整理
导读:1.配置cxf ? xml import?resource="classpath:META-INF/cxf/cxf.xml"?/import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?/ 2.添加Interceptor 可验证用户名密码,ip..等等--前置监听器 public?class?CxfUserInterceptor?extends?AbstractPhaseInte

1.配置cxf ? xml

<import?resource="classpath:META-INF/cxf/cxf.xml"?/>
<import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?/>

2.添加Interceptor 可验证用户名密码,ip..等等--前置监听器

public?class?CxfUserInterceptor?extends?AbstractPhaseInterceptor<SoapMessage>?{
???
????private?Logger?log?=?LoggerFactory.getLogger(CxfUserInterceptor.class);

????private?static?ThreadLocal<Map<String,?String>>?reqHeaderLocal?=?new?ThreadLocal<Map<String,?String>>();

????public?CxfUserInterceptor()?{
????????super(Phase.PRE_INVOKE);
????}

????/**
?????*?拦截器过滤方法。
?????*/
????public?void?handleMessage(SoapMessage?message)?throws?Fault?{
????????if?(reqHeaderLocal.get()?==?null)?{
????????????reqHeaderLocal.set(new?HashMap<String,?String>());
????????}
????????String?accountNodeStr?=?null,?passwordNodeStr?=?null;
????????QName?qnameCredentials?=?new?QName("SoapHeader");
????????if?(message.hasHeader(qnameCredentials))?{
????????????//?取得请求验证的数据
????????????Header?header?=?message.getHeader(qnameCredentials);
????????????Element?elementOrderCredential?=?(Element)?header.getObject();
????????????Node?accountNode?=?elementOrderCredential.getFirstChild();
????????????Node?passwordNode?=?elementOrderCredential.getLastChild();
????????????if?(accountNode?!=?null)?{
????????????????accountNodeStr?=?accountNode.getTextContent();
????????????}
????????????if?(passwordNode?!=?null)?{
????????????????passwordNodeStr?=?passwordNode.getTextContent();
????????????}
????????????//这里验证用户名密码
????????????//......................
???????????
????????}?else?{
????????????throw?new?Fault(new?IllegalAccessException("WebService?request?paramter?no?head!"));
????????}
???????
????}

3.配置拦截器

<bean?id="cxfUserInterceptor"??
		class="com.tima.webservice.impl.util.CxfUserInterceptor"?/>

4.发布方法

@WebService(targetNamespace?=?"http://user.api.webservice.tima.com/")
public?interface?SysOrgAndUserHandleService?{

	?public?static?final?String?NAMESPACE?=?"http://user.api.webservice.tima.com/";
	
	?
	
	?@WebMethod
	?@WebResult(targetNamespace?=?NAMESPACE)
	public?String?sysDemension(?@WebParam(targetNamespace?=?NAMESPACE)?String?xml,@WebParam(targetNamespace?=?NAMESPACE)?String?method);
	}

	
接口实现:
	?
@WebService(endpointInterface?=?"com.tima.webservice.api.SysOrgAndUserHandleService",?serviceName?=?"SysOrgAndUserHandleService",?targetNamespace?=?"http://user.api.webservice.tima.com/")
public?class?SysOrgAndUserHandleServiceImpl?implements
		SysOrgAndUserHandleService?{
		
????????@Override
	public?String?sysDemension(String?xml,String?method)?{
		log.info("WebService:sysDemension接收参数:"+xml+",Method:"+method);
		//方法具体实现。。。。。。
	}
}

5.后置监听器

public?class?AfterInterceptor?extends?AbstractSoapInterceptor?{
	
	public?AfterInterceptor(){
		super(Phase.SETUP);
	}

	@Override
	public?void?handleMessage(SoapMessage?msg)?throws?Fault?{
		//?do?same?thing......
	}

}

6.xml 配置接口

<bean?id="SysOrgAndUserHandleServiceImpl"?class="com.tima.webservice.impl.SysOrgAndUserHandleServiceImpl"/>
	
	<jaxws:endpoint?id="SysOrgAndUserHandleService"	
					implementor="#SysOrgAndUserHandleServiceImpl"?
					implementorClass="com.tima.webservice.impl.SysOrgAndUserHandleServiceImpl"	
					address="/SysOrgAndUserHandleService"?>
		
		<jaxws:inInterceptors>??
			<ref?bean="cxfUserInterceptor"?/>??
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref?bean="afterInterceptor"?/>?
		</jaxws:outInterceptors>
		
	</jaxws:endpoint>

7.附加:

/**
	?*?XML?转换?String?返回?对象List集合
	?*?@param?element
	?*?@param?map
	?*/
	@SuppressWarnings("unchecked")
	public?static?void?xmlConvertMap(Element?element,List<Map<String,?Object>>?list)?{
		List<DefaultElement>?listEl?=?new?ArrayList<DefaultElement>();
		//当根节点有数据的时候
		if?(!CollectionUtils.isEmpty(element.attributes()))?{
			listEl.add((DefaultElement)element);
		}
		listEl.addAll(element.elements());
		for?(DefaultElement?def?:?listEl)?{
			Map<String,?Object>?map?=?new?HashMap<String,?Object>();
			List<DefaultAttribute>?listAtt?=?def.attributes();
			for?(DefaultAttribute?att?:?listAtt)?{
				map.put(att.getName(),?att.getValue());
			}
			list.add(map);
			//xmlConvertMap(def,list);
		}
	}
	
	
	?/**
?????*?将一个?Map?对象转化为一个?JavaBean
?????*?@param?type?要转化的类型
?????*?@param?map?包含属性值的?map
?????*?@return?转化出来的?JavaBean?对象
?????*/
????@SuppressWarnings("rawtypes")
	public?static?Object?convertMapToBean(Class?type,?Map?map)?{
????????Object?obj?=?null;
		try?{
			obj?=?convertMapExt(type,?map);
		}?catch?(InstantiationException?e)?{
			e.printStackTrace();
		}?catch?(IllegalAccessException?e)?{
			e.printStackTrace();
		}?catch?(InvocationTargetException?e)?{
			e.printStackTrace();
		}?catch?(IntrospectionException?e)?{
			e.printStackTrace();
		}
????????return?obj;
????}

????


????/**
?????*?map转换为对象
?????*?@param?type
?????*?@param?map
?????*?@return
?????*?@throws?IntrospectionException
?????*?@throws?InstantiationException
?????*?@throws?IllegalAccessException
?????*?@throws?InvocationTargetException
?????*/
	@SuppressWarnings("rawtypes")
	private?static?Object?convertMapExt(Class?type,?Map?map)
			throws?IntrospectionException,?InstantiationException,IllegalAccessException,?InvocationTargetException?{
		BeanInfo?beanInfo?=?Introspector.getBeanInfo(type);?//?获取类属性
????????Object?obj?=?type.newInstance();?//?创建?JavaBean?对象
????????//?给?JavaBean?对象的属性赋值
????????PropertyDescriptor[]?propertyDescriptors?=??beanInfo.getPropertyDescriptors();
????????for?(PropertyDescriptor?descriptor?:?propertyDescriptors)?{??
????????????String?propertyName?=?descriptor.getName();
???????????
????????????if?(map.containsKey(propertyName))?{
????????????	String?typeCls?=?descriptor.getPropertyType().getName();
????????????????Object?value?=?map.get(propertyName);
????????????????if?(typeCls.equals("java.lang.Long"))?{
????????????????	descriptor.getWriteMethod().invoke(obj,?Long.parseLong(value.toString()));
????????????????}
????????????????else?if?(typeCls.equals("java.util.Date"))?{
????????????????	descriptor.getWriteMethod().invoke(obj,?DateUtil.parseDate(value.toString()));
????????????????}
????????????????else?if?(typeCls.equals("java.lang.Integer"))?{
????????????????	descriptor.getWriteMethod().invoke(obj,?Integer.parseInt(value.toString()));
????????????????}
????????????????else?if?(typeCls.equals("java.lang.Double"))?{
????????????????	descriptor.getWriteMethod().invoke(obj,?Double.parseDouble(value.toString()));
????????????????}
????????????????else?if?(typeCls.equals("java.lang.Float"))?{
????????????????	descriptor.getWriteMethod().invoke(obj,?Float.parseFloat(value.toString()));
????????????????}
????????????????else?if?(typeCls.equals("java.lang.Short"))?{
????????????????	descriptor.getWriteMethod().invoke(obj,?Short.parseShort(value.toString()));
????????????????}
????????????????else?{
????????????????	descriptor.getWriteMethod().invoke(obj,value.toString());
????????????????}
????????????????
????????????}
????????}
		return?obj;
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读