1.说明
在项目开发过程中,可能读者会有这样的需求,那就是你需要提供接口供第三方系统来获取你们系统某个业务的信息。这样的需求其实是在支付系统会更为常见。笔者曾经做过某大型公司的支付系统的接口。我们需求提供大量的接口来供第三方系统来调用。这些接口的返回信息可以是json的格式,亦可是xml的。可能说了这么多,无非就是java中的一门技术,叫RESTful。笔者在博客已经写过SpringMVC的RESTful的用法,笔者可以了解一下。今天,我们将要学习如何使用Spring的框架和CXF的框架来实在拦截身份验证和接口提供提供。
2.CXF的拦截器
一看到拦截器,我想读者可能最先想到的是Struts,是的Struts的拦截器是我们项目开发中最为常用的,但这里我们不讲Struts,我们主要是讨论CXF的拦截器.要想实例CXF的拦截器,我们需要引入cxf-2.3.6.jar架包。并且我们在加载Spring的时候,需要加载这个拦截器。笔者的Spring配置文件命名为applicationContext-webservice.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:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple
http://cxf.apache.org/schemas/simple.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" />
<!-- 全局Bus(输入拦截器) -->
<cxf:bus>
<cxf:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean class="org.a