1 、整合已有的用戶表操作

????? a. 修改Hibernate的映射文件

???? b. 實現com.toodou.bbs.security.service.acegi.JdbcDaoImpl,來實現自定義的用戶認證和授權

???????? 對于BBS系統默認的登錄用戶是USER角色

???? c. 增加數據庫鏈接的配置,權限用戶相關的使用專門的數據庫鏈接

???????? 特別要注意一些顯示的和潛在的acegiDataSourceacegiSessionFactory的配置

???????? 注意DAOacegiSessionFactory的配置

2 、權限的區分

????? 有些操作發帖、回復等需要登錄

3 、方法攔截

????? <!-- 用于安全框架攔截方法,用在事務攔截之前的方式 -->

??????? <property name="preInterceptors">

??????????? <list>

??????????????? <ref bean="methodSecurityInterceptor"/>

??????????? </list>

??????? </property>

???? 同樣也可以采用直接使用攔截器的方式:

???? 這對方法的攔截,對于沒有權限的時候,會拋出AccessDeniedException的異常,所以在Action中對于有權限要求的方法就要捕獲該異常,然后統一處理(比如:跳轉到提示頁面)

4 、登錄后的跳轉

????? 通過配置AuthenticationProcessingFilter的屬性,其中包括:

???? authenticationFailureUrl??? 定義登陸失敗時轉向的頁面

defaultTargetUrl?? 定義登陸成功時轉向的頁面

filterProcessesUrl 定義登陸請求的頁面

rememberMeServices 用于在驗證成功后添加cookie信息

5 、登錄后自定義權限的分配

????? 需求是默認的沒有分配權限的用戶的權限是USER.

???? 這方面Acegi已經預留好了接口,只需要繼承JdbcDaoImpl類,然后覆蓋方法:addCustomAuthorities就可以了。例如:

???? protected void addCustomAuthorities(String username, List authorities) {

??? ???? if(authorities.size()==0){

??? ???? ???? logger.info("Authority is User");

??? ???? ???? authorities.add(new GrantedAuthorityImpl("USER"));

??? ???? }

}

6 、用戶退出

????? 是指用戶點擊退出按鈕,無非做兩個動作:session實效和 cookie實效

???? if (session!=null)

???????? session.invalidate();

???????? Cookie terminate = new
Cookie(TokenBasedRememberMeServices.ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE_KEY, null);

???????? terminate.setMaxAge(0);

???????? response.addCookie(terminate);

7 、用戶信息

???? 默認的Acegi登錄成功后會在Session中放下面兩個內容:

???? ACEGI_SECURITY_CONTEXT--------org.acegisecurity.context.SecurityContextImpl@fdd91091: Authentication: org.acegisecurity.providers.UsernamePasswordAuthenticationToken@fdd91091: Username: org.acegisecurity.userdetails.User@fdcb3700: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: USER; Password: [PROTECTED]; Authenticated: true; Details: org.acegisecurity.ui.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: BBBC69F664334F9E4CD0797BC1209DDB; Granted Authorities: USER

?

ACEGI_SECURITY_LAST_USERNAME--------test

<authz:authentication operation="username"/></td>

?

8 Acegi 標簽的使用

<authz:authorize ifAllGranted="ROLE_SUPERVISOR">

? <td>

??? <A HREF="del.htm?id=123">del</A>

? </td>

</authz:authorize>

9 、對于 URL 過濾

???? 發生AccessDeniedException異常并且是anonymous user的時候跳轉到登錄頁面的設置

???? 配置AuthenticationProcessingFilterEntryPoint的屬性loginFormUrl為:/security/login.jsp

?

???? 發生AccessDeniedException異常并且是已登錄用戶的時候跳轉到提示頁面的設置

???? 配置AccessDeniedHandlerImplerrorPage的屬性為:/accessDenied.jsp