Acegi安全系統詳解

 Acegi是Spring Framework 下最成熟的安全系統,它提供了強大靈活的企業級安全服務,如:

    1 : 完善的認證和授權機制

    2 : Http資源訪問控制

    3 : Method 調用訪問控制

    4 : Access Control List (ACL) 基於對象實例的訪問控制

    5 : Yale Central Authentication Service (CAS) 耶魯單點登陸

    6 : X509 認證

    7 : 當前所有流行容器的認證適配器

    8 : Channel Security頻道安全管理等功能

具體 :
  1. Http資源訪問控制
     http://apps:8080/index.htm -> for public
     http://apps:8080/user.htm -> for authorized user
  2. 方法調用訪問控制
    public void getData() -> all user
    public void modifyData() -> supervisor only
  3. 對象實例保護
    order.getValue() < $100 -> all user
    order.getValue() > $100 -> supervisor only


Acegi是非入侵式安全架構 因爲 :

  1. 基於Servlet FilterSpring aop,  使商業邏輯安全邏輯分開,結構更清晰
  2. 使用Spring 來代理對象能方便地保護方法調用


 基於角色的權限控制(RBAC)  :

Acegi 自帶的 sample 表設計很簡單: users表{username,password,enabled} authorities表{username,authority},這樣簡單的設計無法適應複雜的權限需求,故SpringSide選用RBAC模型權限控制數據庫表進行擴展。 RBACRole-Based Access Control引入了ROLE的概念,使User(用戶)和Permission(權限)分離,一個用戶擁有多個角色,一個角色擁有有多個相應的權限,從而減少了權限管理的複雜度,可更靈活地支持安全策略。


同時,我們也引入了resource(資源)的概念,一個資源對應多個權限,資源分爲ACL,URL,和FUNTION三種。注意,URLFUNTION的權限命名需要以AUTH_開頭纔會有資格參加投票, 同樣的ACL權限命名需要ACL_開頭。



2.1  在Web.xml中的配置 :

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

1<filter>
2    <filter-name>securityFilter</filter-name>
3    <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
4    <init-param>
5        <param-name>targetClass</param-name>
6        <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
7    </init-param>
8</filter>

2) filter-mapping
  <filter-mapping>限定了FilterToBeanProxyURL匹配模式,

 1<filter-mapping>
 2    <filter-name>securityFilter</filter-name>
 3    <url-pattern>/j_security_check</url-pattern>
 4</filter-mapping>
 5
 6<filter-mapping>
 7    <filter-name>securityFilter</filter-name>
 8    <url-pattern>/dwr/**//*</url-pattern>
 9</filter-mapping>
10
11<filter-mapping>
12    <filter-name>securityFilter</filter-name>
13    <url-pattern>*.html</url-pattern>
14</filter-mapping>
15
16<filter-mapping>
17    <filter-name>securityFilter</filter-name>
18    <url-pattern>*.jsp</url-pattern>
19</filter-mapping>  

3) HttpSessionEventPublisher
  <listener>的HttpSessionEventPublisher用於發佈HttpSessionApplicationEventsHttpSessionDestroyedEvent事件給spring的applicationcontext

1    <listener>
2        <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class>
3    </listener>
4

注:appfuse1.9.3中沒有發現這個 監聽器

--------------------------------------
2.2 : 在applicationContext-acegi-security.xml中

2.2.1 FILTER CHAIN

FilterChainProxy會按順序來調用這些filter,使這些filter能享用Spring ioc的功能, CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定義了url比較前先轉爲小寫, PATTERN_TYPE_APACHE_ANT定義了使用Apache ant的匹配模式 

 1    <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
 2        <property name="filterInvocationDefinitionSource">
 3            <value>
 4                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
 5                PATTERN_TYPE_APACHE_ANT
 6               /** *//**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,
 7                                 basicProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter,
 8                                exceptionTranslationFilter,filterInvocationInterceptor,securityEnforcementFilter
 9            </value>
10        </property>
11    </bean>

 


這裏補充一段別人的教程  :

其中對web路徑請求的認證中,我們需要了解一下securityEnforcementFilter
1<bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter">
2     <property name="filterSecurityInterceptor">
3         <ref local="filterInvocationInterceptor"/>
4     </property>
5
6     <property name="authenticationEntryPoint">
7         <ref local="authenticationProcessingFilterEntryPoint"/>
8     </property>
9</bean>

這裏,主要是filterInvocationInterceptor
 1<bean id="filterInvocationInterceptor" class="net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor">
 2      <property name="authenticationManager"><ref bean="authenticationManager"/></property>
 3      <property name="accessDecisionManager"><ref local="httpRequestAccessDecisionManager"/></property>
 4      <property name="objectDefinitionSource">
 5         <value>
 6                  CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
 7                  PATTERN_TYPE_APACHE_ANT
 8                  /wo.html=ROLE_ANONYMOUS,ROLE_USER
 9                  /index.jsp=ROLE_ANONYMOUS,ROLE_USER
10                 /hello.htm=ROLE_ANONYMOUS,ROLE_USER
11                 /logoff.jsp=ROLE_ANONYMOUS,ROLE_USER
12                 /switchuser.jsp=ROLE_SUPERVISOR
13                 /j_acegi_switch_user=ROLE_SUPERVISOR
14                 /acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER
15                 /** *//**=ROLE_USER
16         </value>
17      </property>
18</bean>
   
   在此,主要對objectDefinitionSource值進行處理。這裏配置了很多path=role ,
   其作用就是在請求指定的路徑時,是需要當前用戶具有對應的角色的,如果具有相應角色,則正常訪問。否則跳轉至

  這裏需要說明的就是/index.jsp=ROLE_ANONYMOUS,ROLE_USER 這裏的角色,ROLE_是標記,ANONYMOUS 是角色名稱ANONYMOUS是只可以匿名訪問,  這個角色無需定義

而ROLE_USER 中的USER則是用戶定義的,接下來我們介紹這部分:
 
用戶角色管理:
acegi security提供了用戶角色的獲取接口,以及一個缺省的實現(包括對應的數據庫表定義)
1<bean id="jdbcDaoImpl" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">
2      <property name="dataSource"><ref bean="dataSource"/></property>
3</bean>

可參看這裏的net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl,需要注意的是這個dao的實現是同acegi security提供的表定義一致的。
如果這個角色和用戶處理模型不能滿足自己的需要,自己可以提供自己的實現。只需要將
1<bean id="jdbcDaoImpl" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">

修改成自己類實現即可

  從嚴格意義上來說,以下權限部分的介紹應該不在acegi security處理的範圍之內,不過acegi security是提供了相應的機制的:
權限管理
權限在acegi security 主要以acl的概念出現:即 access control list    

1<bean id="basicAclExtendedDao" class="net.sf.acegisecurity.acl.basic.jdbc.JdbcExtendedDaoImpl">
2      <property name="dataSource"><ref bean="dataSource"/></property>
3   </bean>
4

   這個類實現中有acl的產生,獲取和刪除操作
  
應用數據權限的處理:
   如果我們應用數據的權限要藉助於acegi security 來實現的話,那主要工作就是調用 basicAclExtendedDao 中的相關方法。閱讀basicAclExtendedDao即可明白。
  

以上簡要的介紹了一下自己學習acegi security的一些瞭解。自己最後得出的結論是,如果自己的應用規模很小,完全可以不用acegi security。如果要用acegi security,很多時候是需要重新實現自己的權限和用戶模型的。


引入別人教程完畢

2.2.2 基礎認證

1) authenticationManager
  起到認證管理的作用,它將驗證的功能委託給多個Provider,並通過遍歷Providers, 以保證獲取不同來源的身份認證,若某個Provider能成功確認當前用戶的身份,authenticate()方法會返回一個完整的包含用戶授權信息的Authentication對象,否則會拋出一個AuthenticationException
Acegi提供了不同的AuthenticationProvider的實現,如:

 1        DaoAuthenticationProvider 從數據庫中讀取用戶信息驗證身份
 2        AnonymousAuthenticationProvider 匿名用戶身份認證
 3        RememberMeAuthenticationProvider 已存cookie中的用戶信息身份認證
 4        AuthByAdapterProvider 使用容器的適配器驗證身份
 5        CasAuthenticationProvider 根據Yale中心認證服務驗證身份, 用於實現單點登陸
 6        JaasAuthenticationProvider 從JASS登陸配置中獲取用戶信息驗證身份
 7        RemoteAuthenticationProvider 根據遠程服務驗證用戶身份
 8        RunAsImplAuthenticationProvider 對身份已被管理器替換的用戶進行驗證
 9        X509AuthenticationProvider 從X509認證中獲取用戶信息驗證身份
10        TestingAuthenticationProvider 單元測試時使用

 

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


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

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

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






























































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


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


4) jdbcDaoImpl
  用於在數據中獲取用戶信息。 acegi提供了用戶及授權的表結構,但是您也可以自己來實現。通過usersByUsernameQuery這個SQL
得到你的(用戶ID,密碼,狀態信息);通過authoritiesByUsernameQuery這個SQL得到你的(用戶ID,授權信息)
 1<bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
 2    <property name=
發佈了0 篇原創文章 · 獲贊 1 · 訪問量 6653
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章