隨筆-112  評論-73  文章-0  trackbacks-0

          看了一段時間Acegi的文檔和例子,今天終于把這個難纏的家伙給搞定了.其實就是一些概念的理解的差別,搞得暈頭轉向的.

          在網上找了一些文章和例子,同一個內容卻有不同的說法.有的說acegi沒有角色的概念,有的說在認證通過后將用戶對就在角色保存在Session中.搞得很暈.

          經過幾天看源碼和例子,我覺得acegi是沒有角色的概念的.它只是判斷一個用戶有沒有操作或訪問某一個資源的權限,如果有權限就可以訪問,沒有權限就拋出異常。這個中間沒有涉及到的角色,但是我們可以通過擴展原有的類加入角色的概念,畢竟大多數的權限控制都有角色。(擴展方法后面有介紹)

          基本原理(我寫得比較簡單,個人也只是簡單應用的程度)

          1、用戶請求登錄,根據配置校驗用戶名和密碼,用戶名和密碼acegi默認是在xml文件中配置,可以通過數據庫來保存,主要是實現UserDetailsService.loadUserByUsername方法。這個方法返回一個org.acegisecurity.userdetails .UserDetails。org.acegisecurity.userdetails.User實現了UserDetails。它的構造方法是:

          User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, GrantedAuthority[] authorities)
          Construct the User with the details required by DaoAuthenticationProvider


          2、Logout處理

          acegi 的logout的地址是j_acegi_logout。例如:<a href="/j_acegi_logout">logout</a>。如果有web.xml的fitler-mapping中配置的不是/*,則要加入這樣代碼:

          <filter-mapping>
          <filter-name>Acegi Filter Chain Proxy</filter-name>
          <url-pattern>/j_acegi_logout</url-pattern>
          </filter-mapping>
          在acegi配置中:

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

          <!-- 認證管理器 -->
          <bean id="authenticationManager"
          class="org.acegisecurity.providers.ProviderManager">
          <property name="providers"><!-- 可有多個認證提供器,其中一個證通過就可以了 -->
          <list>
          <ref local="daoAuthenticationProvider" />
          <!-- 匿名認證 -->
          <!-- ref local="anonymousAuthenticationProvider"/-->
          <!-- ref local="rememberMeAuthenticationProvider" /-->
          </list>
          </property>
          <property name="sessionController"
          ref="concurrentSessionController" />
          </bean>
          <!-- 可以限制同一個用戶名在同一時刻成功登錄同一個應用的次數 -->
          <bean id="concurrentSessionController"
          class="org.acegisecurity.concurrent.ConcurrentSessionControllerImpl">
          <!-- 每個用戶同時登陸一位 -->
          <property name="maximumSessions">
          <value>1</value>
          </property>
          <property name="sessionRegistry" ref="sessionRegistry" />
          <property name="exceptionIfMaximumExceeded" value="true" />
          <!-- 默認是false,即如果超出最大登陸用戶限制,你這個登陸還是允許的。但是其他已經用這個用戶登陸的會有一個給強制OUT的。
          只有是true時,才會不讓你這個用戶登陸 -->
          </bean>
          <bean id="sessionRegistry"
          class="org.acegisecurity.concurrent.SessionRegistryImpl" />
          <bean id="concurrentSessionFilter"
          class="org.acegisecurity.concurrent.ConcurrentSessionFilter">
          <property name="sessionRegistry" ref="sessionRegistry" />
          <property name="expiredUrl">
          <value>/</value>
          </property>
          </bean>

          其中:concurrentSessionFilter,建議放在httpSessionContextIntegrationFilter的前面。這樣就可以logout了。

          Technorati :

          posted on 2007-11-19 12:33 Libo 閱讀(869) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 壤塘县| 天祝| 江门市| 将乐县| 当阳市| 罗平县| 金塔县| 镇康县| 天全县| 余江县| 铅山县| 凉城县| 石泉县| 辉县市| 凤山市| 临沂市| 乐都县| 杭锦后旗| 武山县| 全南县| 永顺县| 昭通市| 城市| 鄂托克前旗| 青浦区| 宣威市| 阜康市| 新源县| 水富县| 武定县| 陵水| 肃宁县| 长治县| 江都市| 嘉善县| 建昌县| 西城区| 金堂县| 孙吴县| 偏关县| 西乡县|