1 : 完善的認證和授權機制,
2 : Http資源訪問控制,
3 : Method 調用訪問控制,
4 : Access Control List (ACL) 基于對象實例的訪問控制,
5 : Yale Central Authentication Service (CAS) 耶魯單點登陸,
6 : X509 認證,
7 : 當前所有流行容器的認證適配器,
8 : Channel Security頻道安全管理等功能。
具體 :
- Http資源訪問控制
http://apps:8080/index.htm -> for public
http://apps:8080/user.htm -> for authorized user - 方法調用訪問控制
public void getData() -> all user
public void modifyData() -> supervisor only - 對象實例保護
order.getValue() < $100 -> all user
order.getValue() > $100 -> supervisor only
Acegi是非入侵式安全架構 因為 :
- 基于Servlet Filter和Spring aop, 使商業邏輯和安全邏輯分開,結構更清晰
- 使用Spring 來代理對象,能方便地保護方法調用
基于角色的權限控制(RBAC) :
Acegi 自帶的 sample 表設計很簡單: users表{username,password,enabled} authorities表{username,authority},這樣簡單的設計無法適應復雜的權限需求,故SpringSide選用RBAC模型對權限控制數據庫表進行擴展。 RBAC(Role-Based Access Control)引入了ROLE的概念,使User(用戶)和Permission(權限)分離,一個用戶擁有多個角色,一個角色擁有有多個相應的權限,從而減少了權限管理的復雜度,可更靈活地支持安全策略。
同時,我們也引入了resource(資源)的概念,一個資源對應多個權限,資源分為ACL,URL,和FUNTION三種。注意,URL和FUNTION的權限命名需要以AUTH_開頭才會有資格參加投票, 同樣的ACL權限命名需要ACL_開頭。
2.1 在Web.xml中的配置 :
1) FilterToBeanProxy
Acegi通過實現了Filter接口的FilterToBeanProxy提供一種特殊的使用Servlet Filter的方式,它委托Spring中的Bean -- FilterChainProxy來完成過濾功能,這好處是簡化了web.xml的配置,并且充分利用了Spring IOC的優勢。FilterChainProxy包含了處理認證過程的filter列表,每個filter都有各自的功能。

2

3

4

5

6

7

8

2) filter-mapping
<filter-mapping>限定了FilterToBeanProxy的URL匹配模式,

2

3

4

5

6

7

8


9

10

11

12

13

14

15

16

17

18

19

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

2

3

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的匹配模式

2

3

4

5

6


7

8

9

10

11

這里補充一段別人的教程 :
其中對web路徑請求的認證中,我們需要了解一下securityEnforcementFilter

2

3

4

5

6

7

8

9

這里,主要是filterInvocationInterceptor,

2

3

4

5

6

7

8

9

10

11

12

13

14

15


16

17

18

在此,主要對objectDefinitionSource值進行處理。這里配置了很多path=role ,
其作用就是在請求指定的路徑時,是需要當前用戶具有對應的角色的,如果具有相應角色,則正常訪問。否則跳轉至
這里需要說明的就是/index.jsp=ROLE_ANONYMOUS,ROLE_USER 這里的角色,ROLE_是標記,ANONYMOUS 是角色名稱。ANONYMOUS是只可以匿名訪問, 這個角色無需定義。
而ROLE_USER 中的USER則是用戶定義的,接下來我們介紹這部分:
用戶角色管理:
acegi security提供了用戶角色的獲取接口,以及一個缺省的實現(包括對應的數據庫表定義)

2

3

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

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

2

3

4

這個類實現中有acl的產生,獲取和刪除操作
應用數據權限的處理:
如果我們應用數據的權限要借助于acegi security 來實現的話,那主要工作就是調用 basicAclExtendedDao 中的相關方法。閱讀basicAclExtendedDao即可明白。
以上簡要的介紹了一下自己學習acegi security的一些了解。自己最后得出的結論是,如果自己的應用規模很小,完全可以不用acegi security。如果要用acegi security,很多時候是需要重新實現自己的權限和用戶模型的。
引入別人教程完畢
1) authenticationManager
起到認證管理的作用,它將驗證的功能委托給多個Provider,并通過遍歷Providers, 以保證獲取不同來源的身份認證,若某個Provider能成功確認當前用戶的身份,authenticate()方法會返回一個完整的包含用戶授權信息的Authentication對象,否則會拋出一個AuthenticationException。
Acegi提供了不同的AuthenticationProvider的實現,如:

2

3

4

5

6

7

8

9

10


2

3

4

5

6

7

8

9

每個認證者會對自己指定的證明信息進行認證,如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="dataSource" ref="dataSource"/>
3<property name="usersByUsernameQuery">
4<value>select loginid,passwd,1 from users where loginid = ?</value>
5</property>
6<property name="authoritiesByUsernameQuery">
7<value>
8select u.loginid,p.name from users u,roles r,permissions p,
9user_role ur,role_permis rp where u.id=ur.user_id and
10r.id=ur.role_id and p.id=rp.permis_id and r.id=rp.role_id and
11p.status='1' and u.loginid=?
12</value>
13</property>
14</bean>
5) userCache & resourceCache
緩存用戶和資源相對應的權限信息。每當請求一個受保護資源時,daoAuthenticationProvider就會被調用以獲取用戶授權信息。如果每次都從數據庫獲取的話,那代價很高,對于不常改變的用戶和資源信息來說,最好是把相關授權信息緩存起來。(詳見 2.6.3 資源權限定義擴展 )
userCache提供了兩種實現: NullUserCache和EhCacheBasedUserCache, NullUserCache實際上就是不進行任何緩存,EhCacheBasedUserCache是使用Ehcache來實現緩功能。
1<bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
2<property name="cacheManager" ref="cacheManager"/>
3<property name="cacheName" value="userCache"/>
4</bean>
5
6<bean id="userCache"
class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache" autowire="byName">
7<property name="cache" ref="userCacheBackend"/>
8</bean>
9
10<bean id="resourceCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
11<property name="cacheManager" ref="cacheManager"/>
12<property name="cacheName" value="resourceCache"/>
13</bean>
14
15<bean id="resourceCache"
class="org.springside.modules.security.service.acegi.cache.ResourceCache" autowire="byName">
16<property name="cache" ref="resourceCacheBackend"/>
17</bean>
6) basicProcessingFilter
用于處理HTTP頭的認證信息,如從Spring遠程協議(如Hessian和Burlap)或普通的瀏覽器如IE,Navigator的HTTP頭中獲取用戶
信息,將他們轉交給通過authenticationManager屬性裝配的認證管理器。如果認證成功,會將一個Authentication對象放到會話中
,否則,如果認證失敗,會將控制轉交給認證入口點(通過authenticationEntryPoint屬性裝配)
1<bean id="basicProcessingFilter" class="org.acegisecurity.ui.basicauth.BasicProcessingFilter">
2<property name="authenticationManager" ref="authenticationManager"/>
3<property name="authenticationEntryPoint" ref="basicProcessingFilterEntryPoint"/>
4</bean>
7) basicProcessingFilterEntryPoint
通過向瀏覽器發送一個HTTP401(未授權)消息,提示用戶登錄。
處理基于HTTP的授權過程, 在當驗證過程出現異常后的"去向",通常實現轉向、在response里加入error信息等功能。
1<bean id="basicProcessingFilterEntryPoint"
2class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">
3<property name="realmName" value="SpringSide Realm"/>
4</bean>
8) authenticationProcessingFilterEntryPoint
當拋出AccessDeniedException時,將用戶重定向到登錄界面。屬性loginFormUrl配置了一個登錄表單的URL,當需要用戶登錄時,authenticationProcessingFilterEntryPoint會將用戶重定向到該URL

2

3

4

5

6

7

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

2

2) httpRequestAccessDecisionManager
經過投票機制來決定是否可以訪問某一資源(URL或方法)。allowIfAllAbstainDecisions為false時如果有一個或以上的decisionVoters投票通過,則授權通過。可選的決策機制有ConsensusBased和UnanimousBased
1<bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
2<property name="allowIfAllAbstainDecisions" value="false"/>
3<property name="decisionVoters">
4<list>
5<ref bean="roleVoter"/>
6</list>
7</property>
8</bean>
3) roleVoter
必須是以rolePrefix設定的value開頭的權限才能進行投票,如AUTH_ , ROLE_
1<bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
2<property name="rolePrefix" value="AUTH_"/>
3</bean>
4)exceptionTranslationFilter
異常轉換過濾器,主要是處理AccessDeniedException和AuthenticationException,將給每個異常找到合適的"去向"
1<bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
2<property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint"/>
3</bean>
5) authenticationProcessingFilter
和servlet spec差不多,處理登陸請求.當身份驗證成功時,AuthenticationProcessingFilter會在會話中放置一個Authentication對象,并且重定向到登錄成功頁面
authenticationFailureUrl定義登陸失敗時轉向的頁面
defaultTargetUrl定義登陸成功時轉向的頁面
filterProcessesUrl定義登陸請求的頁面
rememberMeServices用于在驗證成功后添加cookie信息
1<bean id="authenticationProcessingFilter"
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
2<property name="authenticationManager" ref="authenticationManager"/>
3<property name="authenticationFailureUrl">
4<value>/security/login.jsp?login_error=1</value>
5</property>
6<property name="defaultTargetUrl">
7<value>/admin/index.jsp</value>
8</property>
9<property name="filterProcessesUrl">
10<value>/j_acegi_security_check</value>
11</property>
12<property name="rememberMeServices" ref="rememberMeServices"/>
13</bean>
6) filterInvocationInterceptor
在執行轉向url前檢查objectDefinitionSource中設定的用戶權限信息。首先,objectDefinitionSource中定義了訪問URL需要的屬性信息(這里的屬性信息僅僅是標志,告訴accessDecisionManager要用哪些voter來投票)。然后,authenticationManager掉用自己的provider來對用戶的認證信息進行校驗。最后,有投票者根據用戶持有認證和訪問url需要的屬性,調用自己的voter來投票,決定是否允許訪問。
1<bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
2<property name="authenticationManager" ref="authenticationManager"/>
3<property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
4<property name="objectDefinitionSource" ref="filterDefinitionSource"/>
5</bean>
7)filterDefinitionSource(詳見 2.6.3 資源權限定義擴展)
自定義DBFilterInvocationDefinitionSource從數據庫和cache中讀取保護資源及其需要的訪問權限信息
1<bean id="filterDefinitionSource"
2class="org.springside.modules.security.service.acegi.DBFilterInvocationDefinitionSource">
3<property name="convertUrlToLowercaseBeforeComparison" value="true"/>
4<property name="useAntPath" value="true"/>
5<property name="acegiCacheManager" ref="acegiCacheManager"/>
6</bean>
2.2.4 方法調用安全控制
(詳見 2.6.3 資源權限定義擴展)
1) methodSecurityInterceptor
在執行方法前進行攔截,檢查用戶權限信息1<bean id="methodSecurityInterceptor"
2class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
3<property name="authenticationManager" ref="authenticationManager"/>
4<property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
5<property name="objectDefinitionSource" ref="methodDefinitionSource"/>
6</bean>
2) methodDefinitionSource
自定義MethodDefinitionSource從cache中讀取權限1<bean id="methodDefinitionSource"
2class="org.springside.modules.security.service.acegi.DBMethodDefinitionSource">
3<property name="acegiCacheManager" ref="acegiCacheManager"/>
4</bean>