分享

WebService开发笔记 3 -- 增强访问 WebService 的安全性

 江江385 2013-05-24
WebService开发笔记 1中我们创建了一个WebService简单实例,下面我们通过一个简单的用户口令验证机制来加强一下WebService的安全性: 

1.修改WebService 服务端 spring 配置文件 ws-context.xml 
Xml代码  收藏代码
  1. <beans xmlns="http://www./schema/beans"  
  2.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  3.     xmlns:jaxws="http://cxf./jaxws"  
  4.     xsi:schemaLocation="http://cxf./jaxws http://cxf./schemas/jaxws.xsd http://www./schema/beans  http://www./schema/beans/spring-beans.xsd"  
  5.     default-autowire="byName" default-lazy-init="true">  
  6.       
  7.     <jaxws:endpoint id="webServiceSample"  
  8.         address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl">  
  9.   
  10.         <jaxws:inInterceptors>  
  11.             <bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />  
  12.             <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">  
  13.                 <constructor-arg>  
  14.                     <map>  
  15.                         <entry key="action" value="UsernameToken" />  
  16.                         <entry key="passwordType" value="PasswordText" />  
  17.                         <entry key="passwordCallbackClass" value="cn.org.coral.biz.examples.webservice.handler.WsAuthHandler" />  
  18.                     </map>  
  19.                 </constructor-arg>  
  20.             </bean>  
  21.         </jaxws:inInterceptors>     
  22.   
  23.     </jaxws:endpoint>  
  24.       
  25. </beans>  


2.服务端添加passwordCallbackClass回调类,该类进行用户口令验证: 
Java代码  收藏代码
  1. package cn.org.coral.biz.examples.webservice.handler;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.security.auth.callback.Callback;  
  6. import javax.security.auth.callback.CallbackHandler;  
  7. import javax.security.auth.callback.UnsupportedCallbackException;  
  8.   
  9. import org.apache.ws.security.WSPasswordCallback;  
  10.   
  11. public class WsAuthHandler  implements CallbackHandler{  
  12.   
  13.     public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {  
  14.         WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];  
  15.         if (pc.getIdentifer().equals("ws-client")){  
  16.             if (!pc.getPassword().equals("admin")) {  
  17.                 throw new SecurityException("wrong password");  
  18.            }  
  19.         }else{  
  20.             throw new SecurityException("wrong username");  
  21.         }  
  22.     }  
  23.   
  24. }  


3.客户端修改spring 配置文件 wsclient-context.xml 如下: 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"  
  3.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf./jaxws"  
  5.     xsi:schemaLocation="http://cxf./jaxws http://cxf./schemas/jaxws.xsd http://www./schema/beans  http://www./schema/beans/spring-beans.xsd"  
  6.     default-autowire="byName" default-lazy-init="true">  
  7.   
  8.   
  9.     <!-- ws clinet -->  
  10.     <bean id="webServiceSampleClient" class="cn.org.coral.biz.examples.webservice.WebServiceSample"  
  11.         factory-bean="webServiceSampleClientFactory" factory-method="create" />  
  12.   
  13.   
  14.     <bean id="webServiceSampleClientFactory"  
  15.         class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  16.         <property name="serviceClass"  
  17.             value="cn.org.coral.biz.examples.webservice.WebServiceSample" />  
  18.         <property name="address"  
  19.             value="http://88.148.29.54:8080/aio/services/WebServiceSample" />  
  20.         <property name="outInterceptors">  
  21.             <list>  
  22.                 <bean  
  23.                     class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor" />  
  24.                 <ref bean="wss4jOutConfiguration" />  
  25.             </list>  
  26.         </property>  
  27.     </bean>  
  28.   
  29.     <bean id="wss4jOutConfiguration"  
  30.         class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">  
  31.         <property name="properties">  
  32.             <map>  
  33.                 <entry key="action" value="UsernameToken" />  
  34.                 <entry key="user" value="ws-client" />  
  35.                 <entry key="passwordType" value="PasswordText" />  
  36.                 <entry>  
  37.                     <key>  
  38.                         <value>passwordCallbackRef</value>  
  39.                     </key>  
  40.                     <ref bean="passwordCallback" />  
  41.                 </entry>  
  42.             </map>  
  43.         </property>  
  44.     </bean>  
  45.     <bean id="passwordCallback"  
  46.         class="cn.org.coral.biz.examples.webservice.handler.WsClinetAuthHandler">  
  47.     </bean>  
  48.   
  49. </beans>  


4.客户端添加passwordCallback类,通过该类设置访问口令 
Java代码  收藏代码
  1. package cn.org.coral.biz.examples.webservice.handler;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.security.auth.callback.Callback;  
  6. import javax.security.auth.callback.CallbackHandler;  
  7. import javax.security.auth.callback.UnsupportedCallbackException;  
  8.   
  9. import org.apache.ws.security.WSPasswordCallback;  
  10.   
  11. public class WsClinetAuthHandler  implements CallbackHandler{  
  12.   
  13.   
  14.     public void handle(Callback[] callbacks) throws IOException,   
  15.                     UnsupportedCallbackException {   
  16.             for (int i = 0; i < callbacks.length; i++) {   
  17.                     WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];   
  18.                     int usage = pc.getUsage();   
  19.   
  20.   
  21.                     System.out.println("identifier: " + pc.getIdentifer());   
  22.                     System.out.println("usage: " + pc.getUsage());   
  23.                     if (usage == WSPasswordCallback.USERNAME_TOKEN) {   
  24.                             // username token pwd...   
  25.                             pc.setPassword("admin");   
  26.   
  27.                     } else if (usage == WSPasswordCallback.SIGNATURE) {   
  28.                             // set the password for client's keystore.keyPassword   
  29.                             pc.setPassword("keyPassword");   
  30.                     }   
  31.             }   
  32.     }   
  33.   
  34. }  


5.junit单元测试程序: 
Java代码  收藏代码
  1. package cn.org.coral.biz.examples.webservice;  
  2.   
  3. import org.springframework.test.AbstractDependencyInjectionSpringContextTests;  
  4. import org.springframework.util.Assert;  
  5.   
  6. public class TestWebService extends AbstractDependencyInjectionSpringContextTests {  
  7.     WebServiceSample webServiceSampleClient;  
  8.       
  9.     @Override  
  10.     protected String[] getConfigLocations() {  
  11.         setAutowireMode(AUTOWIRE_BY_NAME);  
  12.         return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };  
  13.     }  
  14.   
  15.     /** 
  16.      * @param webServiceSampleClient the webServiceSampleClient to set 
  17.      */  
  18.     public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {  
  19.         this.webServiceSampleClient = webServiceSampleClient;  
  20.     }  
  21.   
  22.     public void testSay(){  
  23.         String result = webServiceSampleClient.say(" world");  
  24.         Assert.hasText(result);       
  25.     }  
  26. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多