常言笑的家

          Spring, Hibernate, Struts, Ajax, RoR

          Acegi 使用 Acl(1)

          首先要配置一個filter,這個filter用一個代理bean寫在了spring里面,其實根正常的filter沒有任何區別。

          代碼
          1<bean id="securityEnforcementFilter"   
          2    class="org.acegisecurity.intercept.web.SecurityEnforcementFilter">   
          3    <property name="filterSecurityInterceptor">   
          4        <ref local="filterInvocationInterceptor" />   
          5    </property>   
          6    <property name="authenticationEntryPoint">   
          7        <ref local="authenticationProcessingFilterEntryPoint" />   
          8    </property>   
          9</bean>  

           

          "filterInvocationInterceptor" 是一個攔截器,說是攔截器,其實就是在filter里面執行一下他的攔截方法,這里可沒有什么aop.
          authenticationEntryPoint 交驗失敗的時候轉到的地方,為什么說是地方,因為通過配置可以轉到其它的url甚至其它的協議下(http 轉到 https等等)
           
          代碼
           1<bean id="authenticationProcessingFilterEntryPoint"   
           2    class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">   
           3    <property name="loginFormUrl">   
           4        <value>/error.security</value>   
           5    </property>   
           6    <property name="forceHttps">   
           7        <value>false</value>   
           8    </property>   
           9</bean>  
          10

           

          這個就是失敗的時候轉到的地方,我們可以配置url和是否使用https

           

          代碼
           1<bean id="filterInvocationInterceptor"   
           2    class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">   
           3    <property name="authenticationManager">   
           4        <ref bean="authenticationManager" />   
           5    </property>   
           6    <property name="accessDecisionManager">   
           7        <ref local="httpRequestAccessDecisionManager" />   
           8    </property>   
           9    <property name="objectDefinitionSource">   
          10        <value>   
          11            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON    
          12            PATTERN_TYPE_APACHE_ANT    
          13            /index.jsp=ROLE_ANONYMOUS,ROLE_USER    
          14            /j_acegi_switch_user=ROLE_SUPERVISOR   
          15            /login.security=ROLE_ANONYMOUS,ROLE_USER                /test.do=ROLE_CODER   
          16            /**.do*=ROLE_USER    
          17        </value>   
          18    </property>   
          19</bean>  

           

          這個就是前面提到的攔截器。簡單解釋一下:
          authenticationManager 在acegi里面的主要作用就是管理維護用戶的權限角色等信息,比方說想要用戶的ROLE就要在這里面拿了。里面配置了多種全縣的來源,可以從DAO里面來(就是數據庫里面),可以是cookies里面的,也可以是匿名的權限,每種權限都以一種Provider的形式提供:

          代碼
           1<bean id="authenticationManager"   
           2    class="org.acegisecurity.providers.ProviderManager">   
           3    <property name="providers">   
           4        <list>   
           5            <ref local="daoAuthenticationProvider" />   
           6            <ref local="anonymousAuthenticationProvider" />   
           7            <ref local="rememberMeAuthenticationProvider" />   
           8        </list>   
           9    </property>   
          10</bean>

           

          objectDefinitionSource在acegi里面就是配置權限信息,說明哪一個url需要什么權限才能訪問,acegi默認用<value>來表示,其實這正是acegi的不足之處,還好能夠補救。我來說明一下:
          我們知道在spring里面<value>標簽比較特殊,spring首先找到這個屬性的類型,然后把value里面的內容以String的類型取出來(Spring做了一下包裝,為TypedString)。然后根據這個屬性的類型找他的Editer,然后用Editer來處理String為需要的類型。但是我們不希望用String來表達url,很明顯url里面有=就不會玩了。我們可以把這個信息寫到數據庫里面,然后讀取,這里面不說了以前有一位高手已經解釋過了。

          接下來就是httpRequestAccessDecisionManager了,AccessDecisionManager在acegi里面是決策者,就是根據你所擁有的權限和訪問URL需要的權限來決定你到底能不能訪問。

           

          代碼
           1<bean id="httpRequestAccessDecisionManager"   
           2    class="org.acegisecurity.vote.AffirmativeBased">   
           3    <property name="allowIfAllAbstainDecisions">   
           4        <value>false</value>   
           5    </property>   
           6    <property name="decisionVoters">   
           7        <list>   
           8            <ref bean="roleVoter" />   
           9        </list>   
          10    </property>   
          11</bean>  

           

          決策者里面是投票者,這個上面已經解釋過了,一個投票者校驗一種權限。整個流程已經說完了。

          posted on 2006-12-16 22:27 常言笑 閱讀(214) 評論(0)  編輯  收藏 所屬分類: JAVA/J2EE

          My Links

          Blog Stats

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 五河县| 北碚区| 昌吉市| 鄄城县| 聊城市| 盘山县| 永川市| 阜平县| 璧山县| 壤塘县| 花莲县| 富裕县| 上蔡县| 长沙县| 中山市| 安龙县| 金川县| 尤溪县| 清水河县| 都江堰市| 苍梧县| 珲春市| 吉安县| 山阴县| 陇川县| 光山县| 太仆寺旗| 布拖县| 昂仁县| 赣榆县| 通许县| 澄江县| 嘉义市| 开化县| 汉阴县| 成安县| 阿拉善盟| 塔河县| 开封县| 浦城县| 自治县|