spring与xfire的整合有两种方法,我们先看一下第一种:
步骤1、修改你的web.xm文件:

 

<servlet>  
<servlet-name>xfire</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
</servlet>  
  
<servlet-mapping>  
<servlet-name>xfire</servlet-name>  
<url-pattern>/*</url-pattern>  
</servlet-mapping>

 

可以看到xfire是通过org.springframework.web.servlet.DispatcherServlet这个方法来实现的。

 

步骤2、在WEB-INF下添加xfire-servlet.xml文件,配置方法如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!-- START SNIPPET: xfire -->
   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/icmeservice">   <!-- 这里是WebService的名称 -->    
                    <ref bean="echo"/>
                </entry>
            </map>
        </property>
    </bean>
    
    <!-- Declare a parent bean with all properties common to both services -->
    <bean id="echo" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceFactory">
            <ref bean="xfire.serviceFactory"/>
        </property>
        <property name="xfire">
            <ref bean="xfire"/>
        </property> 
      <!--   定义service接口,以及service的实现类 -->
        <property name="serviceBean">
            <ref bean="sBean"/>
        </property>
        <property name="serviceClass">
            <value>com.hys.icmews.webservice.WebService</value>
        </property>
    </bean>    
    <bean id="sBean" class="com.hys.icmews.webservice.WebServiceImpl">
 </bean>
 
    
</beans>

 

到此 一个服务平台就搭建好了,启动服务,通过client可实现WebService的访问。

 

 

另外一种方法是将配置写在spring的配置文件中,个人感觉这种方法更适合开发人员

 

步骤1、配置web.xml

 

这个是我的xml配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:applicationContext*.xml
  </param-value>
 </context-param>
  
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 
 <servlet>    
        <servlet-name>XFireServlet</servlet-name>    
        <servlet-class>    
            org.codehaus.xfire.spring.XFireSpringServlet    
        </servlet-class>    
    </servlet>    
        
    <servlet-mapping>    
        <servlet-name>XFireServlet</servlet-name>    
        <url-pattern>/servlet/XFireServlet/*</url-pattern>    
    </servlet-mapping>    
        
    <servlet-mapping>    
        <servlet-name>XFireServlet</servlet-name>    
        <url-pattern>/service/*</url-pattern>    
    </servlet-mapping>
    
</web-app>

步骤2、在applicationContext.xml相同的路径下,新建一个applicationContext_webService.xml文件,我的文件路径是在src根目录下面

 

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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />    
        
    <!-- WebService接口的实现类-->    
    <bean id="sBean" class="com.hys.icmews.webservice.imp.WebServiceImpl" />
    <!-- end -->    
        
    <!-- 下面的配置是WebService的标准配置 -->    
    <bean id="WebService" class="org.codehaus.xfire.spring.ServiceBean"> <!-- WebService的名字 -->    
        <property name="xfire" ref="xfire" />         <!-- WebService的实现类 -->   
        <property name="serviceBean" ref="sBean" />            <!-- 定义webservice接口  -->
        <property name="serviceClass" value="com.hys.icmews.webservice.WebService" />        <!-- 
        <property name="inHandlers">  
  <list>  
  <ref bean="addressingHandler"/>  
  </list>  
  </property>  
   -->
    </bean>
       
</beans>

 

通过上面的配置,就可以将Spring的bean和xfire的WebService很好的结合起来了。如果要考虑安全性,还需要进一步配置。这里我也是参考了网上的一部分资料,没有得到进一步的证实,现将方法提供给大家做一个参考。

 

下面将详细描述如何通过WSS4J的方式来实现WebService的数字证书的加密验证,这里大量的参考了SpringSide,非常感谢!
下面看一下关于WSS4J的spring配置文件


Java代码 
   
 
 
<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">    
<beans>    
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"
        
    <!-- WebService Impl  -->    
"accountWebServiceImpl" class="xxx.com.account.webservice.impl.AccountWebServiceImpl" autowire="byName"
    <!-- end -->    
        
    <!-- 使用 WSS4J验证 -->    
"accountWebServiceWSS4J" class="org.codehaus.xfire.spring.remoting.XFireExporter">    
"serviceBean" ref="accountWebServiceImpl"/>    
"serviceClass" value="xxx.com.account.webservice.wss4j.AccountWebServiceWSS4J"/>    
"inHandlers">    
            <list>    
"domInHandler"/>    
"wss4jInHandler"/>    
"validateUserTokenHandler"/>    
            </list>    
        </property>    
    </bean>     
        
"domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler"/>    
        
"wss4jInHandler" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">    
"properties">    
            <props>    
"action">UserToken</prop>    
"passwordCallbackClass">xxx.com.account.webservice.wss4j.PasswordHandler</prop>    
            </props>    
        </property>    
    </bean>    
        
    <!--  使用 WSS4J验证 Signature模式 -->    
"accountWebServiceWSS4JSign" class="org.codehaus.xfire.spring.remoting.XFireExporter">    
"serviceBean" ref="accountWebServiceImpl"/>    
"serviceClass" value="xxx.com.account.webservice.wss4j.AccountWebServiceWSS4JSign"/>    
"inHandlers">    
            <list>    
"domInHandler"/>    
"wss4jInHandlerSign"/>    
"validateUserTokenHandler"/>    
            </list>    
        </property>    
    </bean>    
        
"wss4jInHandlerSign" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">    
"properties">    
          <props>    
"action">Signature</prop>    
"signaturePropFile">com/real/cn/account/webservice/wss4j/server_security_sign.properties</prop>    
"passwordCallbackClass">xxx.com.account.webservice.wss4j.PasswordHandler</prop>    
          </props>    
        </property>    
    </bean>    
        
    <!-- 使用 WSS4J验证 Encrypt模式 -->    
"accountWebServiceWSS4JEnc" class="org.codehaus.xfire.spring.remoting.XFireExporter">    
"serviceBean" ref="accountWebServiceImpl"/>    
"serviceClass" value="xxx.com.account.webservice.wss4j.AccountWebServiceWSS4JEnc"/>    
"inHandlers">    
            <list>    
"domInHandler"/>    
"wss4jInHandlerEnc"/>    
"validateUserTokenHandler"/>    
            </list>    
        </property>    
    </bean>    
        
"wss4jInHandlerEnc" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">    
"properties">    
          <props>    
"action">Encrypt</prop>    
"decryptionPropFile">com/real/cn/account/webservice/wss4j/server_security_enc.properties</prop>    
"passwordCallbackClass">xxx.com.account.webservice.wss4j.PasswordHandler</prop>    
          </props>    
        </property>    
    </bean>    
        
"validateUserTokenHandler"
          class="xxx.com.account.webservice.wss4j.WSS4JTokenHandler"/>    
            
</beans>  
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
 
 <!-- WebService Impl  -->
 <bean id="accountWebServiceImpl" class="xxx.com.account.webservice.impl.AccountWebServiceImpl" autowire="byName" />
 <!-- end -->
 
 <!-- 使用 WSS4J验证 -->
 <bean id="accountWebServiceWSS4J" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceBean" ref="accountWebServiceImpl"/>
        <property name="serviceClass" value="xxx.com.account.webservice.wss4j.AccountWebServiceWSS4J"/>
        <property name="inHandlers">
            <list>
                <ref bean="domInHandler"/>
                <ref bean="wss4jInHandler"/>
                <ref bean="validateUserTokenHandler"/>
            </list>
        </property>
    </bean> 
 
 <bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler"/>
 
 <bean id="wss4jInHandler" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">
        <property name="properties">
            <props>
                <prop key="action">UserToken</prop>
                <prop key="passwordCallbackClass">xxx.com.account.webservice.wss4j.PasswordHandler</prop>
            </props>
        </property>
    </bean>
    
    <!--  使用 WSS4J验证 Signature模式 -->
    <bean id="accountWebServiceWSS4JSign" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceBean" ref="accountWebServiceImpl"/>
        <property name="serviceClass" value="xxx.com.account.webservice.wss4j.AccountWebServiceWSS4JSign"/>
        <property name="inHandlers">
            <list>
                <ref bean="domInHandler"/>
                <ref bean="wss4jInHandlerSign"/>
                <ref bean="validateUserTokenHandler"/>
            </list>
        </property>
    </bean>
    
    <bean id="wss4jInHandlerSign" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">
        <property name="properties">
          <props>
            <prop key="action">Signature</prop>
            <prop key="signaturePropFile">com/real/cn/account/webservice/wss4j/server_security_sign.properties</prop>
            <prop key="passwordCallbackClass">xxx.com.account.webservice.wss4j.PasswordHandler</prop>
          </props>
        </property>
    </bean>
    
    <!-- 使用 WSS4J验证 Encrypt模式 -->
    <bean id="accountWebServiceWSS4JEnc" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceBean" ref="accountWebServiceImpl"/>
        <property name="serviceClass" value="xxx.com.account.webservice.wss4j.AccountWebServiceWSS4JEnc"/>
        <property name="inHandlers">
            <list>
                <ref bean="domInHandler"/>
                <ref bean="wss4jInHandlerEnc"/>
                <ref bean="validateUserTokenHandler"/>
            </list>
        </property>
    </bean>
    
    <bean id="wss4jInHandlerEnc" class="org.codehaus.xfire.security.wss4j.WSS4JInHandler">
        <property name="properties">
          <props>
            <prop key="action">Encrypt</prop>
            <prop key="decryptionPropFile">com/real/cn/account/webservice/wss4j/server_security_enc.properties</prop>
            <prop key="passwordCallbackClass">xxx.com.account.webservice.wss4j.PasswordHandler</prop>
          </props>
        </property>
    </bean>
    
    <bean id="validateUserTokenHandler"
    class="xxx.com.account.webservice.wss4j.WSS4JTokenHandler"/>
        
</beans>

Encrypt模式是指客户端使用公钥加密数据流,然后发送到服务端,服务端通过私钥进行校验,这种方式适合集中式的服务;Signature模式是指客户端使用私钥加密数据流,服务端通过公钥来校验,这种方式适合分布式服务。
对于Encrypt模式和Signature模式的接口,只要继承AccountWebService就可以。

server_security_enc.properties和server_security_sign.properties文件保存了证书的位置以及用户名和密码,这里的用户名和密码,这样就有密码和证书两重的校验方式

对于server端和client端来讲
私钥,格式如下:


Java代码


1. org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin    
2. org.apache.ws.security.crypto.merlin.keystore.type=jks    
3. org.apache.ws.security.crypto.merlin.keystore.password=xxx    
4. org.apache.ws.security.crypto.merlin.alias.password=xxx    
5. org.apache.ws.security.crypto.merlin.keystore.alias=xxx    
6. org.apache.ws.security.crypto.merlin.file=.../account_server_enc.jks  
 
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=xxx
org.apache.ws.security.crypto.merlin.alias.password=xxx
org.apache.ws.security.crypto.merlin.keystore.alias=xxx
org.apache.ws.security.crypto.merlin.file=.../account_server_enc.jks


公钥,格式如下:


Java代码


1. org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin    
2. org.apache.ws.security.crypto.merlin.keystore.type=jks    
3. org.apache.ws.security.crypto.merlin.keystore.password=xxx    
4. org.apache.ws.security.crypto.merlin.keystore.alias=xxx    
5. org.apache.ws.security.crypto.merlin.file=.../account_client_enc.jks