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

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

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

    隨筆-31  評論-7  文章-0  trackbacks-0
    Acegi 的配置看起來非常復(fù)雜,但事實上在實際項目的安全應(yīng)用中我們并不需要那么多功能,清楚的了解Acegi配置中各項的功能,有助于我們靈活的運用Acegi于實踐中。

    2.1 在Web.xml中的配置

    1)  FilterToBeanProxy
      Acegi通過實現(xiàn)了Filter接口的FilterToBeanProxy提供一種特殊的使用Servlet Filter的方式,它委托Spring中的Bean -- FilterChainProxy來完成過濾功能,這好處是簡化了web.xml的配置,并且充分利用了Spring IOC的優(yōu)勢。FilterChainProxy包含了處理認證過程的filter列表,每個filter都有各自的功能。

    <filter>
            
    <filter-name>Acegi Filter Chain Proxy</filter-name>
            
    <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
            
    <init-param>
                
    <param-name>targetClass</param-name>
                
    <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
            
    </init-param>
        
    </filter>
     2) filter-mapping
      <filter-mapping>限定了FilterToBeanProxy的URL匹配模式,只有*.do和*.jsp和/j_acegi_security_check 的請求才會受到權(quán)限控制,對javascript,css等不限制。
      
    
    <filter-mapping>
          
    <filter-name>Acegi Filter Chain Proxy</filter-name>
          
    <url-pattern>*.do</url-pattern>
        
    </filter-mapping>
    <filter-mapping>
          
    <filter-name>Acegi Filter Chain Proxy</filter-name>
          
    <url-pattern>*.jsp</url-pattern>
        
    </filter-mapping>
    <filter-mapping>
          
    <filter-name>Acegi Filter Chain Proxy</filter-name>
          
    <url-pattern>/j_acegi_security_check</url-pattern>
    </filter-mapping>

    3) HttpSessionEventPublisher
      <listener>的HttpSessionEventPublisher用于發(fā)布HttpSessionApplicationEvents和HttpSessionDestroyedEvent事件給spring的applicationcontext。

        <listener>
    <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class>
        
    </listener>


    2.2 在applicationContext-acegi-security.xml中

    2.2.1 FILTER CHAIN

      FilterChainProxy會按順序來調(diào)用這些filter,使這些filter能享用Spring ioc的功能, CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定義了url比較前先轉(zhuǎn)為小寫, PATTERN_TYPE_APACHE_ANT定義了使用Apache ant的匹配模式

        <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
            
    <property name="filterInvocationDefinitionSource">
                
    <value>
                    CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                    PATTERN_TYPE_APACHE_ANT
                   /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,basicProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter, exceptionTranslationFilter,filterInvocationInterceptor
                
    </value> 
           
    </property>
        
    </bean>

    2.2.2 基礎(chǔ)認證

    1) authenticationManager
      起到認證管理的作用,它將驗證的功能委托給多個Provider,并通過遍歷Providers, 以保證獲取不同來源的身份認證,若某個Provider能成功確認當前用戶的身份,authenticate()方法會返回一個完整的包含用戶授權(quán)信息的Authentication對象,否則會拋出一個AuthenticationException。
    Acegi提供了不同的AuthenticationProvider的實現(xiàn),如:
            DaoAuthenticationProvider 從數(shù)據(jù)庫中讀取用戶信息驗證身份
            AnonymousAuthenticationProvider 匿名用戶身份認證
            RememberMeAuthenticationProvider 已存cookie中的用戶信息身份認證
            AuthByAdapterProvider 使用容器的適配器驗證身份
            CasAuthenticationProvider 根據(jù)Yale中心認證服務(wù)驗證身份, 用于實現(xiàn)單點登陸
            JaasAuthenticationProvider 從JASS登陸配置中獲取用戶信息驗證身份
            RemoteAuthenticationProvider 根據(jù)遠程服務(wù)驗證用戶身份
            RunAsImplAuthenticationProvider 對身份已被管理器替換的用戶進行驗證
            X509AuthenticationProvider 從X509認證中獲取用戶信息驗證身份
            TestingAuthenticationProvider 單元測試時使用

            每個認證者會對自己指定的證明信息進行認證,如DaoAuthenticationProvider僅對UsernamePasswordAuthenticationToken這個證明信息進行認證。

    <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
            
    <property name="providers">
                
    <list>
                    
    <ref local="daoAuthenticationProvider"/>
                    
    <ref local="anonymousAuthenticationProvider"/>
                    
    <ref local="rememberMeAuthenticationProvider"/>
                
    </list>
            
    </property>
    </bean>


    2) daoAuthenticationProvider
      進行簡單的基于數(shù)據(jù)庫的身份驗證。DaoAuthenticationProvider獲取數(shù)據(jù)庫中的賬號密碼并進行匹配,若成功則在通過用戶身份的同時返回一個包含授權(quán)信息的Authentication對象,否則身份驗證失敗,拋出一個AuthenticatiionException。

        <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
            
    <property name="userDetailsService" ref="jdbcDaoImpl"/>
            
    <property name="userCache" ref="userCache"/>
            
    <property name="passwordEncoder" ref="passwordEncoder"/>
       
    </bean>


    3) passwordEncoder
      使用加密器對用戶輸入的明文進行加密。Acegi提供了三種加密器:
    PlaintextPasswordEncoder—默認,不加密,返回明文.
    ShaPasswordEncoder—哈希算法(SHA)加密
    Md5PasswordEncoder—消息摘要(MD5)加密

    <bean id="passwordEncoder" class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"/>


    4) jdbcDaoImpl
      用于在數(shù)據(jù)中獲取用戶信息。 acegi提供了用戶及授權(quán)的表結(jié)構(gòu),但是您也可以自己來實現(xiàn)。通過usersByUsernameQuery這個SQL得到你的(用戶ID,密碼,狀態(tài)信息);通過authoritiesByUsernameQuery這個SQL得到你的(用戶ID,授權(quán)信息)

    <bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">        
    <property name="dataSource" ref="dataSource"/>
            
    <property name="usersByUsernameQuery">            
    <value>select loginid,passwd,1 from users where loginid = ?</value>        
    </property>
            
    <property name="authoritiesByUsernameQuery">            
    <value>select u.loginid,p.name from users u,roles r,permissions 
    p,user_role ur,role_permis rp where u.id=ur.user_id and r.id=ur.role_id and 
    p.id=rp.permis_id and r.id=rp.role_id and p.status='1' and u.loginid=?
    </value>
            
    </property></bean>

    5) userCache &  resourceCache
      緩存用戶和資源相對應(yīng)的權(quán)限信息。每當請求一個受保護資源時,daoAuthenticationProvider就會被調(diào)用以獲取用戶授權(quán)信息。如果每次都從數(shù)據(jù)庫獲取的話,那代價很高,對于不常改變的用戶和資源信息來說,最好是把相關(guān)授權(quán)信息緩存起來。(詳見 2.6.3 資源權(quán)限定義擴展 )
    userCache提供了兩種實現(xiàn): NullUserCache和EhCacheBasedUserCache, NullUserCache實際上就是不進行任何緩存,EhCacheBasedUserCache是使用Ehcache來實現(xiàn)緩功能。

       
    
    <bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">   
            
    <property name="cacheManager" ref="cacheManager"/>
            
    <property name="cacheName" value="userCache"/>
        
    </bean>
        
    <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache" autowire="byName">
            
    <property name="cache" ref="userCacheBackend"/>
        
    </bean>
        
    <bean id="resourceCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
            
    <property name="cacheManager" ref="cacheManager"/>
            
    <property name="cacheName" value="resourceCache"/>
        
    </bean>
        
    <bean id="resourceCache" class="org.springside.modules.security.service.acegi.cache.ResourceCache" autowire="byName">
            
    <property name="cache" ref="resourceCacheBackend"/>
        
    </bean>
    6) basicProcessingFilter
      用于處理HTTP頭的認證信息,如從Spring遠程協(xié)議(如Hessian和Burlap)或普通的瀏覽器如IE,Navigator的HTTP頭中獲取用戶信息,將他們轉(zhuǎn)交給通過authenticationManager屬性裝配的認證管理器。如果認證成功,會將一個Authentication對象放到會話中,否則,如果認證失敗,會將控制轉(zhuǎn)交給認證入口點(通過authenticationEntryPoint屬性裝配)
       
    
    <bean id="basicProcessingFilter"
                    class
    ="org.acegisecurity.ui.basicauth.BasicProcessingFilter">
            
    <property name="authenticationManager" ref="authenticationManager"/>
            
    <property name="authenticationEntryPoint" ref="basicProcessingFilterEntryPoint"/>
        
    </bean>
    7) basicProcessingFilterEntryPoint
      通過向瀏覽器發(fā)送一個HTTP401(未授權(quán))消息,提示用戶登錄。
    處理基于HTTP的授權(quán)過程, 在當驗證過程出現(xiàn)異常后的"去向",通常實現(xiàn)轉(zhuǎn)向、在response里加入error信息等功能。
     <bean id="basicProcessingFilterEntryPoint" class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">       
          
    <property name="realmName" value="SpringSide Realm"/>
    </bean>

    8) authenticationProcessingFilterEntryPoint
      當拋出AccessDeniedException時,將用戶重定向到登錄界面。屬性loginFormUrl配置了一個登錄表單的URL,當需要用戶登錄時,authenticationProcessingFilterEntryPoint會將用戶重定向到該URL

    <bean id="authenticationProcessingFilterEntryPoint" 
    class
    ="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">    
        
    <property name="loginFormUrl">
                
    <value>/security/login.jsp</value>     
        
    </property>
            
    <property name="forceHttps" value="false"/>
    </bean>

    2.2.3 HTTP安全請求

    1) httpSessionContextIntegrationFilter
      每次request前 HttpSessionContextIntegrationFilter從Session中獲取Authentication對象,在request完后, 又把Authentication對象保存到Session中供下次request使用,此filter必須其他Acegi filter前使用,使之能跨越多個請求。

    <bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter"></bean>
        
    <bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
            
    <property name="allowIfAllAbstainDecisions" value="false"/>
            
    <property name="decisionVoters">
                
    <list>
                    
    <ref bean="roleVoter"/>
                
    </list>
            
    </property>
    </bean>


    2) httpRequestAccessDecisionManager
      經(jīng)過投票機制來決定是否可以訪問某一資源(URL或方法)。allowIfAllAbstainDecisions為false時如果有一個或以上的decisionVoters投票通過,則授權(quán)通過??蛇x的決策機制有ConsensusBased和UnanimousBased

        <bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
            
    <property name="allowIfAllAbstainDecisions" value="false"/>
            
    <property name="decisionVoters">
                
    <list>
                    
    <ref bean="roleVoter"/>
                
    </list>
            
    </property>
        
    </bean>


    3) roleVoter
       必須是以rolePrefix設(shè)定的value開頭的權(quán)限才能進行投票,如AUTH_ , ROLE_

        <bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
            
    <property name="rolePrefix" value="AUTH_"/>
       
    </bean>

    4)exceptionTranslationFilter
      異常轉(zhuǎn)換過濾器,主要是處理AccessDeniedException和AuthenticationException,將給每個異常找到合適的"去向" 

       <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
            
    <property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint"/>
        
    </bean>

    5) authenticationProcessingFilter
      和servlet spec差不多,處理登陸請求.當身份驗證成功時,AuthenticationProcessingFilter會在會話中放置一個Authentication對象,并且重定向到登錄成功頁面
             authenticationFailureUrl定義登陸失敗時轉(zhuǎn)向的頁面
             defaultTargetUrl定義登陸成功時轉(zhuǎn)向的頁面
             filterProcessesUrl定義登陸請求的頁面
             rememberMeServices用于在驗證成功后添加cookie信息

    <bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">            <property name="authenticationManager" ref="authenticationManager"/>
            
    <property name="authenticationFailureUrl">
                
    <value>/security/login.jsp?login_error=1</value>
            
    </property>
            
    <property name="defaultTargetUrl">
                
    <value>/admin/index.jsp</value>
            
    </property>
            
    <property name="filterProcessesUrl">
                
    <value>/j_acegi_security_check</value>
            
    </property>
            
    <property name="rememberMeServices" ref="rememberMeServices"/>
        
    </bean>

    6) filterInvocationInterceptor
      在執(zhí)行轉(zhuǎn)向url前檢查objectDefinitionSource中設(shè)定的用戶權(quán)限信息。首先,objectDefinitionSource中定義了訪問URL需要的屬性信息(這里的屬性信息僅僅是標志,告訴accessDecisionManager要用哪些voter來投票)。然后,authenticationManager掉用自己的provider來對用戶的認證信息進行校驗。最后,有投票者根據(jù)用戶持有認證和訪問url需要的屬性,調(diào)用自己的voter來投票,決定是否允許訪問。

        <bean id="filterInvocationInterceptor"
    class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
            <property name="authenticationManager" ref="authenticationManager"/>
            <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
            <property name="objectDefinitionSource" ref="filterDefinitionSource"/>
        </bean>


    7) filterDefinitionSource (詳見 2.6.3 資源權(quán)限定義擴展)
      自定義DBFilterInvocationDefinitionSource從數(shù)據(jù)庫和cache中讀取保護資源及其需要的訪問權(quán)限信息 

    <bean id="filterDefinitionSource"
    class="org.springside.modules.security.service.acegi.DBFilterInvocationDefinitionSource">
            <property name="convertUrlToLowercaseBeforeComparison" value="true"/>
            <property name="useAntPath" value="true"/>
            <property name="acegiCacheManager" ref="acegiCacheManager"/>
    </bean>

    2.2.4 方法調(diào)用安全控制

    (詳見 2.6.3 資源權(quán)限定義擴展)

    1) methodSecurityInterceptor
      在執(zhí)行方法前進行攔截,檢查用戶權(quán)限信息
    2) methodDefinitionSource
      自定義MethodDefinitionSource從cache中讀取權(quán)限

       <bean id="methodSecurityInterceptor" 
    class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
            <property name="authenticationManager" ref="authenticationManager"/>
            <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
            <property name="objectDefinitionSource" ref="methodDefinitionSource"/>
        </bean>
        <bean id="methodDefinitionSource"
    class="org.springside.modules.security.service.acegi.DBMethodDefinitionSource">
            <property name="acegiCacheManager" ref="acegiCacheManager"/>
        </bean>

    2.3 Jcaptcha驗證碼

    采用 http://jcaptcha.sourceforge.net 作為通用的驗證碼方案,請參考SpringSide中的例子,或網(wǎng)上的:
    http://www.coachthrasher.com/page/blog?entry=jcaptcha_with_appfuse。

    差沙在此過程中又發(fā)現(xiàn)acegi logout filter的錯誤,進行了修正。

    另外它默認提供的圖片比較難認,我們custom了一個美觀一點的版本。


    文章來源:http://www.cnblogs.com/xiaoao808/archive/2008/07/17/1244773.html
    posted on 2008-07-17 00:42 破名超難起 閱讀(75) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲Av熟妇高潮30p| 美女露隐私全部免费直播| 国产成人免费爽爽爽视频| 最新亚洲人成无码网站| 亚洲成a人片在线观看日本| 成熟女人牲交片免费观看视频| 黄色免费网址在线观看| 亚洲国产精品自在在线观看 | 日韩久久无码免费毛片软件| 国产亚洲人成网站在线观看不卡| 国产成人精品免费视频网页大全| 美女裸体无遮挡免费视频网站| 亚洲小视频在线观看| 免费一级毛片不卡不收费| 在线免费中文字幕| 成年网在线观看免费观看网址| 亚洲日本在线播放| 亚洲日韩VA无码中文字幕 | 亚洲动漫精品无码av天堂| 日韩成全视频观看免费观看高清| 免费毛片a线观看| 黄色a三级三级三级免费看| 亚洲精品熟女国产| 亚洲男同帅GAY片在线观看| 全免费一级午夜毛片| 日韩av无码久久精品免费| 黄色一级视频免费| 亚洲中文字幕无码亚洲成A人片| 亚洲AV永久无码精品成人| 日本中文一区二区三区亚洲| 中字幕视频在线永久在线观看免费 | 亚洲第一区视频在线观看| 亚洲欧洲成人精品香蕉网| 国产福利免费在线观看| 国产人在线成免费视频| 久久久久国产精品免费看| 一区二区在线免费视频| 国产亚洲成在线播放va| 最新亚洲春色Av无码专区| 亚洲国产成人无码av在线播放| 亚洲国产精品嫩草影院在线观看|