<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于JavaWeb開發(fā)
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數(shù)據(jù)加載中……

    實施WebService Security[WS-Security1.0]的Encrypt和Sign模式(XFire+WSS4J)

    鑒于很多系統(tǒng)需要實施WS-Security的標(biāo)準(zhǔn),我們在SpringSide中提供了XFire+WSS4J的Demo,本文介紹SpringSide中Spring+XFire+WSS4J的基本配置

    [WebService Server端配置]
    第一,創(chuàng)建一個基本的BookService
    public interface BookService {
        
    /**
         * 按書名模糊查詢圖書
         
    */

        List findBooksByName(String name);

        
    /**
         * 查找目錄下的所有圖書
         *
         * 
    @param categoryId 如果category為null或“all”, 列出所有圖書。
         
    */

        List findBooksByCategory(String categoryId);

        
    /**
         * 列出所有分類.
         *
         * 
    @return List<Category>,或是null。
         
    */

        List getAllCategorys();
    }

    第二,接口擴(kuò)展,即Extend基本的BookService,在XFire中,不同的WSS4J策略需要針對不同的ServiceClass,否則<inHandlers>里面的定義會Overlap。
    public interface BookServiceWSS4JEnc  extends BookService {

    }

    public interface BookServiceWSS4JSign  extends BookService {

    }

    第三,配置Spring的ApplicationContext文件
        <!--BookService 基類-->
        
    <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">
            
    <property name="serviceFactory" ref="xfire.serviceFactory"/>
            
    <property name="xfire" ref="xfire"/>
        
    </bean>

        
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            
    <property name="mappings">
                
    <value>
                    /BookService=bookService
                    /BookServiceWSS4J=bookServiceWSS4J
                    /BookServiceWSS4JEnc=bookServiceWSS4JEnc
                    /BookServiceWSS4JSign=bookServiceWSS4JSign
                
    </value>
            
    </property>
        
    </bean>

       
    <!--(1)BookWebService 不需要認(rèn)證-->
        
    <bean id="bookService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
            
    <property name="serviceFactory" ref="xfire.serviceFactory"/>
            
    <property name="xfire" ref="xfire"/>
            
    <property name="serviceBean" ref="bookManager"/>
            
    <property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookService"/>
        
    </bean>

        
    <!--  (3)BookWebService 使用 WSS4J驗證-->
        
    <bean id="bookServiceWSS4J" class="org.codehaus.xfire.spring.remoting.XFireExporter">
            
    <property name="serviceBean" ref="bookManager"/>
            
    <property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookServiceWSS4J"/>
            
    <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">UsernameToken</prop>
                    
    <prop key="passwordCallbackClass">org.springside.bookstore.plugins.xfire.wss4j.PasswordHandler</prop>
                
    </props>
            
    </property>
        
    </bean>

        
    <bean id="validateUserTokenHandler" class="org.springside.bookstore.plugins.xfire.wss4j.WSS4JTokenHandler"/>
        
        
    <!--  (4)BookWebService 使用 WSS4J驗證 Encrypt模式-->
        
    <bean id="bookServiceWSS4JEnc" class="org.codehaus.xfire.spring.remoting.XFireExporter">
            
    <property name="serviceBean" ref="bookManager"/>
            
    <property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookServiceWSS4JEnc"/>
            
    <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">org/springside/bookstore/plugins/xfire/wss4j/insecurity_enc.properties</prop>
                
    <prop key="passwordCallbackClass">org.springside.bookstore.plugins.xfire.wss4j.PasswordHandler</prop>
              
    </props>
            
    </property>
        
    </bean>
        
        
    <!--  (5)BookWebService 使用 WSS4J驗證 Signature模式-->
        
    <bean id="bookServiceWSS4JSign" class="org.codehaus.xfire.spring.remoting.XFireExporter">
            
    <property name="serviceBean" ref="bookManager"/>
            
    <property name="serviceClass" value="org.springside.bookstore.plugins.xfire.service.BookServiceWSS4JSign"/>
            
    <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">org/springside/bookstore/plugins/xfire/wss4j/insecurity_sign.properties</prop>
                
    <prop key="passwordCallbackClass">org.springside.bookstore.plugins.xfire.wss4j.PasswordHandler</prop>
              
    </props>
            
    </property>
        
    </bean>
        
    </beans>

    第四,配置insecurity_enc.properties和insecurity_sign.properties兩個密鑰庫配置文件
    insecurity_enc.properties:
    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
    =SpringSide
    org.apache.ws.security.crypto.merlin.alias.password
    =SpringSide
    org.apache.ws.security.crypto.merlin.keystore.alias
    =david
    org.apache.ws.security.crypto.merlin.file
    =org/springside/bookstore/plugins/xfire/wss4j/springside_private.jks

    outsecurity_sign.properties:
    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
    =SpringSide
    org.apache.ws.security.crypto.merlin.keystore.alias
    =david
    org.apache.ws.security.crypto.merlin.file
    =org/springside/bookstore/plugins/xfire/wss4j/springside_public.jks

    第五,使用SecureX生成了兩個keystore文件
    springside_private.jks
    別名名稱: david
    創(chuàng)建日期: 
    2006-8-6
    輸入類型:KeyEntry
    認(rèn)證鏈長度: 
    1
    認(rèn)證 
    [1]:
    Owner: CN
    =david, OU=SpringSide, O=org, L=gz, ST=gd, C=cn
    發(fā)照者: CN
    =david, OU=SpringSide, O=org, L=gz, ST=gd, C=cn
    序號: 44d4cdcd
    有效期間: Sun Aug 
    06 00:56:45 CST 2006 至: Mon Aug 06 00:56:45 CST 2007
    認(rèn)證指紋:
             MD5:  CF:
    97:13:0C:70:D0:4D:B6:B4:27:0F:1A:0B:CF:D9:F2
             SHA1: 8E:8E:E8:BC:
    64:39:C8:43:E4:F7:1B:3B:CE:48:1D:6B:A0:2B:58:B5

    springside_public.jks
    別名名稱: david
    創(chuàng)建日期: 
    2006-8-6
    輸入類型: trustedCertEntry

    Owner: CN
    =david, OU=SpringSide, O=org, L=gz, ST=gd, C=cn
    發(fā)照者: CN
    =david, OU=SpringSide, O=org, L=gz, ST=gd, C=cn
    序號: 44d4cdcd
    有效期間: Sun Aug 
    06 00:56:45 CST 2006 至: Mon Aug 06 00:56:45 CST 2007
    認(rèn)證指紋:
             MD5:  CF:
    97:13:0C:70:D0:4D:B6:B4:27:0F:1A:0B:CF:D9:F2
             SHA1: 8E:8E:E8:BC:
    64:39:C8:43:E4:F7:1B:3B:CE:48:1D:6B:A0:2B:58:B5

    第五,新版本SpringSide需要
    http://www.bouncycastle.org/download/bcprov-jdk15-133.jar
    并且要配置java.security
    另外,還要使用jdk加密增強(qiáng)策略
    http://m.tkk7.com/openssl/archive/2006/03/08/34381.html

    用戶要使用WSS4J,需要配置Bouncycastle這個SecurityProvider,否則
    運行Enc模式的XFire認(rèn)證的時候,會拋出異常:
    org.apache.ws.security.WSSecurityException: An unsupported signature or encryption algorithm was used unsupported key
    配合java.security也是非常簡單:
    在最后加入BouncycastleProvider。
    security.provider.1=sun.security.provider.Sun
    security.provider.2=com.sun.net.ssl.internal.ssl.Provider
    security.provider.3=com.sun.rsajca.Provider
    security.provider.4=com.sun.crypto.provider.SunJCE
    security.provider.5=sun.security.jgss.SunProvider
    security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider

    [WebService Client端配置]
    1,Encrypt模式的Client是在客戶端用david的公鑰加密Soap里面的usernameToken,然后發(fā)送到Web服務(wù),Web服務(wù)用david的私鑰來驗證。這種模式需要客戶端預(yù)先知道服務(wù)器端的公鑰。

    在Encrypt模式中,需要這樣配置ClientHandler:
            Service serviceModel = new ObjectServiceFactory().create(BookServiceWSS4JEnc.class);
            XFireProxyFactory factory 
    = new XFireProxyFactory(getXFire());

            BookService service 
    = (BookService) factory.create(serviceModel, "xfire.local://BookServiceWSS4JEnc");

            Client client 
    = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
            
    //掛上WSS4JOutHandler,提供認(rèn)證
            client.addOutHandler(new DOMOutHandler());
            Properties properties 
    = new Properties();
            configureOutProperties(properties);
            client.addOutHandler(
    new WSS4JOutHandler(properties));

            List list 
    = service.getAllCategorys();
    configureOutProperties函數(shù)負(fù)責(zé)指定Client使用何種安全策略,沒錯,使用outsecurity_enc.properties,這個properties是跟Server端的insecurity_enc.properties一起使用的。
        protected void configureOutProperties(Properties config) {
            config.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.ENCRYPT);
            config.setProperty(WSHandlerConstants.USER, 
    "david");
            
    //config.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordHandler.class.getName());
            
    //Configuration of public key used to encrypt message goes to properties file.
            config.setProperty(WSHandlerConstants.ENC_PROP_FILE,
                                   
    "org/springside/bookstore/plugins/xfire/outsecurity_enc.properties");
        }

    outsecurity_enc.properties:
    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
    =SpringSide
    org.apache.ws.security.crypto.merlin.keystore.alias
    =david
    org.apache.ws.security.crypto.merlin.file
    =org/springside/bookstore/plugins/xfire/wss4j/springside_public.jks


    2, Sign模式的Client同樣也是很簡單,這種模式是Client端用自己的私鑰為usernameToken簽名,服務(wù)器端用Client的公鑰來驗證簽名,因此,服務(wù)器端需要預(yù)先知道客戶端的公鑰。
    對應(yīng)于Encrypt模式,這里的configureOutProperties需要這樣來配置:
        protected void configureOutProperties(Properties properties) {
            properties.setProperty(WSHandlerConstants.ACTION,WSHandlerConstants.SIGNATURE);
            
    // User in keystore
            properties.setProperty(WSHandlerConstants.USER, "david");
            
    // This callback is used to specify password for given user for keystore
            properties.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordHandler.class.getName());
            
    // Configuration for accessing private key in keystore
            properties.setProperty(WSHandlerConstants.SIG_PROP_FILE,"org/springside/bookstore/plugins/xfire/outsecurity_sign.properties");
            properties.setProperty(WSHandlerConstants.SIG_KEY_ID,
    "IssuerSerial");
        }


    outsecurity_sign.properties:
    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
    =SpringSide
    org.apache.ws.security.crypto.merlin.alias.password
    =SpringSide
    org.apache.ws.security.crypto.merlin.keystore.alias
    =david
    org.apache.ws.security.crypto.merlin.file
    =org/springside/bookstore/plugins/xfire/wss4j/springside_private.jks


    posted on 2008-05-07 11:08 々上善若水々 閱讀(1307) 評論(1)  編輯  收藏 所屬分類: WebService

    評論

    # re: 實施WebService Security[WS-Security1.0]的Encrypt和Sign模式(XFire+WSS4J)   回復(fù)  更多評論   

    能給我一個完整的SpringSide + Web Services + WS-Security 和 客戶端調(diào)用 的代碼嗎 ,非常感謝您
    郵箱:guowei821120@163.com
    2008-07-15 22:59 | 過為
    主站蜘蛛池模板: 在线观看亚洲网站| 午夜亚洲AV日韩AV无码大全| 无码日韩精品一区二区免费| 精品无码国产污污污免费网站| 日本视频在线观看永久免费| 久久精品国产影库免费看| 成人久久免费网站| 在线毛片片免费观看| 久久精品成人免费网站| 午夜影院免费观看| 99国产精品免费观看视频| 五月婷婷在线免费观看| 无码人妻一区二区三区免费手机 | 久久久亚洲AV波多野结衣 | 成年美女黄网站色大免费视频| AV无码免费永久在线观看| av无码久久久久不卡免费网站| 波多野结衣免费在线观看| 最近中文字幕无吗高清免费视频| 成年午夜视频免费观看视频| 国产精品视_精品国产免费 | 日韩精品电影一区亚洲| 亚洲成a人片在线观看日本麻豆 | 妻子5免费完整高清电视| 最新中文字幕免费视频| 在线观看91精品国产不卡免费| 又黄又大又爽免费视频| 亚洲中文字幕不卡无码| 久久亚洲熟女cc98cm| 亚洲中文无码mv| 污污污视频在线免费观看| 国产麻豆成人传媒免费观看| 日韩精品极品视频在线观看免费| 亚洲免费二区三区| 在线免费观看国产视频| 青青草原亚洲视频| 亚洲欧洲精品在线| 亚洲AV成人精品日韩一区 | 亚洲一区二区三区在线| 粉色视频在线观看www免费| 免费人成激情视频在线观看冫|