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

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

???? b. 實(shí)現(xiàn)com.toodou.bbs.security.service.acegi.JdbcDaoImpl,來(lái)實(shí)現(xiàn)自定義的用戶認(rèn)證和授權(quán)

???????? 對(duì)于BBS系統(tǒng)默認(rèn)的登錄用戶是USER角色

???? c. 增加數(shù)據(jù)庫(kù)鏈接的配置,權(quán)限用戶相關(guān)的使用專門(mén)的數(shù)據(jù)庫(kù)鏈接

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

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

2 、權(quán)限的區(qū)分

????? 有些操作發(fā)帖、回復(fù)等需要登錄

3 、方法攔截

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

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

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

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

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

??????? </property>

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

???? 這對(duì)方法的攔截,對(duì)于沒(méi)有權(quán)限的時(shí)候,會(huì)拋出AccessDeniedException的異常,所以在Action中對(duì)于有權(quán)限要求的方法就要捕獲該異常,然后統(tǒng)一處理(比如:跳轉(zhuǎn)到提示頁(yè)面)

4 、登錄后的跳轉(zhuǎn)

????? 通過(guò)配置AuthenticationProcessingFilter的屬性,其中包括:

???? authenticationFailureUrl??? 定義登陸失敗時(shí)轉(zhuǎn)向的頁(yè)面

defaultTargetUrl?? 定義登陸成功時(shí)轉(zhuǎn)向的頁(yè)面

filterProcessesUrl 定義登陸請(qǐng)求的頁(yè)面

rememberMeServices 用于在驗(yàn)證成功后添加cookie信息

5 、登錄后自定義權(quán)限的分配

????? 需求是默認(rèn)的沒(méi)有分配權(quán)限的用戶的權(quán)限是USER.

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

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

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

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

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

??? ???? }

}

6 、用戶退出

????? 是指用戶點(diǎn)擊退出按鈕,無(wú)非做兩個(gè)動(dòng)作:session實(shí)效和 cookie實(shí)效

???? 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 、用戶信息

???? 默認(rèn)的Acegi登錄成功后會(huì)在Session中放下面兩個(gè)內(nèi)容:

???? 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 標(biāo)簽的使用

<authz:authorize ifAllGranted="ROLE_SUPERVISOR">

? <td>

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

? </td>

</authz:authorize>

9 、對(duì)于 URL 過(guò)濾

???? 發(fā)生AccessDeniedException異常并且是anonymous user的時(shí)候跳轉(zhuǎn)到登錄頁(yè)面的設(shè)置

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

?

???? 發(fā)生AccessDeniedException異常并且是已登錄用戶的時(shí)候跳轉(zhuǎn)到提示頁(yè)面的設(shè)置

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