四?Acegi ACL使用?
4.1 基本概念
????? 在google中搜索'acl'會找到很多相關的介紹,而且涉及的范圍也特別廣泛。ACL是(Access Control List)的縮寫,顧名思義,ACL是‘訪問控制列表’的意思。通俗點說,ACL保存了所有用戶或角色對資源的訪問權限。最典型的ACL實現是流行操作系統(window, unix)的文件訪問控制系統,精確定義了某個用戶或角色對某個特定文件的讀、寫、執行等權限,更通俗的例子是可以定義某個管理員只能管一部分的訂單,而另一個管理員只能管另一部分的。
4.2 Acegi ACL配置
Acegi好早就實現了ACL(好像是0.5),但是使用起來確實有點麻煩,所以用的不是太廣泛。這里簡單的說明一下使用方法,希望有更多的朋友來試試。
首先要理解Acegi里面Voter的概念,ACL正是在一個Voter上擴展起來的。現來看一下AclVoter的配置。
???????? < property? name ="processConfigAttribute" >
???????????? < value > ACL_READ </ value >
???????? </ property >
???????? < property? name ="processDomainObjectClass" >
???????????? < value > org.springside.modules.security.acl.domain.AclDomainAware </ value >
???????? </ property >
???????? < property? name ="aclManager" >
???????????? < ref? local ="aclManager" />
???????? </ property >
???????? < property? name ="requirePermission" >
???????????? < list >
???????????????? < ref? local ="org.acegisecurity.acl.basic.SimpleAclEntry.ADMINISTRATION" />
???????????????? < ref? local ="org.acegisecurity.acl.basic.SimpleAclEntry.READ" />
???????????? </ list >
???????? </ property >
???? </ bean >
-
ACL_READ指的是這個Voter對哪些SecurityConfig起作用,我們可以把ACL_READ配置在想要攔截的Method上。比方說我們要攔截readOrder這個方法,以實現ACL控制,可以這樣配置。
orderManager.readOrder=ACL_READ - processDomainObjectClass指出哪些DomainObject是要進行ACL校驗的。
- aclManager是一個比較重要的概念,主要負責在權限列表中根據用戶和DomainObject取得acl列表。
- requirePermission指出要進行這個操作必須具備的acl權限,比方說read操作就必須有ADMINISTRATION或READ兩個權限。
其實整個過程看下來比較清晰,下面來看一下AclManager如何配置。
???? < bean? id ="aclManager" ?class ="org.acegisecurity.acl.AclProviderManager" >
???????? < property? name ="providers" >
???????????? < list >
???????????????? < ref? local ="basicAclProvider" />
???????????? </ list >
???????? </ property >
???? </ bean >
???? < bean? id ="basicAclProvider" ?class ="org.acegisecurity.acl.basic.BasicAclProvider" >
???????? < property? name ="basicAclDao" >
???????????? < ref? local ="basicAclExtendedDao" />
???????? </ property >
???? </ bean >
???? < bean? id ="basicAclExtendedDao" ?class ="org.acegisecurity.acl.basic.jdbc.JdbcExtendedDaoImpl" >
???????? < property? name ="dataSource" >
???????????? < ref? bean ="dataSource" />
???????? </ property >
???? </ bean >
很明顯ACLManager繼承了Acegi的一貫風格,Provider可以提供多種取得ACL訪問列表的途徑,默認的是用basicAclProvider在數據庫中取得。既然提到了數據庫,那我們就來看一下Acegi默認提供的ACL在數據庫里的保存表結構:
id? IDENTITY ? NOT ? NULL ,
object_identity?VARCHAR_IGNORECASE( 250 )? NOT ? NULL ,
parent_object? INTEGER ,
acl_class?VARCHAR_IGNORECASE( 250 )? NOT ? NULL ,
CONSTRAINT ?unique_object_identity? UNIQUE (object_identity),
FOREIGN ? KEY ?(parent_object)? REFERENCES ?acl_object_identity(id)
);
CREATE ? TABLE ?acl_permission?(
id? IDENTITY ? NOT ? NULL ,
acl_object_identity? INTEGER ? NOT ? NULL ,
recipient?VARCHAR_IGNORECASE( 100 )? NOT ? NULL ,
mask? INTEGER ? NOT ? NULL ,
CONSTRAINT ?unique_recipient? UNIQUE (acl_object_identity,?recipient),
FOREIGN ? KEY ?(acl_object_identity)? REFERENCES ?acl_object_identity(id)
);
- acl_object_identity表存放了所有受保護的domainObject的信息。其中object_identity字段保存了domainObject的class和id,默認的保存格式是:domainClass:domainObjectId。
- acl_permission?就是ACL權限列表了,recipient?是用戶或角色信息,mask表示了這個用戶或角色對這個domainObject的訪問權限。注意這些信息的保存格式都是可以根據自己的需要改變的。
這樣讀取和刪除的時候Acegi就能很好的完成攔截工作,但是讀取一個List的時候,如何才能把該用戶不能操作的domainObject剔除掉呢?這就需要afterInvocationManager來完成這個工作。下面來看下配置:
???? < bean? id ="afterInvocationManager" ?class ="org.acegisecurity.afterinvocation.AfterInvocationProviderManager" >
???????? < property? name ="providers" >
???????????? < list >
???????????????? < ref? local ="afterAclCollectionRead" />
???????????? </ list >
???????? </ property >
???? </ bean >
???? <!-- ?Processes?AFTER_ACL_COLLECTION_READ?configuration?settings? -->
???? < bean? id ="afterAclCollectionRead" ?class ="org.acegisecurity.afterinvocation.BasicAclEntryAfterInvocationCollectionFilteringProvider" >
???????? < property? name ="aclManager" >
???????????? < ref? local ="aclManager" />
???????? </ property >
???????? < property? name ="requirePermission" >
???????????? < list >
???????????????? < ref? local ="org.acegisecurity.acl.basic.SimpleAclEntry.ADMINISTRATION" />
???????????????? < ref? local ="org.acegisecurity.acl.basic.SimpleAclEntry.READ" />
???????????? </ list >
???????? </ property >
???? </ bean >
afterAclCollectionRead會在攔截的方法執行結束的時候執行。主要的作用就是在返回的List中挨個檢查domainObject的操作權限,然后根據requirePermission來剔除不符合的domainObject。
4.3 使用RuleEngine設置的ACL規則
在SpringSide里使用了RuleEngine來設置ACL規則,具體規則見
bookstore-sample\resources\rules\drl
大盤預測 國富論