Acegi 配置指南(4)
認(rèn)證模式配置(二)
表單認(rèn)證
表單認(rèn)證利用開發(fā)者開發(fā)的登錄頁(yè)面搜集用戶名和密碼,下面是登錄頁(yè)面的基本代碼:
<form action="j_acegi_security_check" method="post">
用戶名:<input name="j_username"><br>
密 碼:<input name="j_password" type="password"><br>
14天之內(nèi)免登錄<input name="_acegi_security_remember_me" type="checkbox"><br>
<input type="submit" value="登錄">
</form>
同時(shí),還加入了退出、免登錄和匿名三個(gè)過濾器。
代碼:
<!-- 退出過濾器 -->
<bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter">
<constructor-arg value="/index.jsp" />
<constructor-arg>
<list>
<ref local="rememberMeServices" />
<ref local="securityContextLogoutHandler" />
</list>
</constructor-arg>
</bean>
<!-- 安全上下文退出句柄 -->
<bean id="securityContextLogoutHandler" class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler">
<property name="invalidateHttpSession" value="true" />
</bean>
<!-- ================
認(rèn)證部分
================ -->
<!-- 表單認(rèn)證過濾器 -->
<bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationFailureUrl" value="/login.jsp?error=1" />
<property name="defaultTargetUrl" value="/" />
<property name="alwaysUseDefaultTargetUrl" value="true" />
<property name="filterProcessesUrl" value="/j_acegi_security_check" />
<property name="rememberMeServices" ref="rememberMeServices" />
</bean>
<!-- 認(rèn)證管理器 -->
<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
<ref local="daoAuthenticationProvider" />
<ref local="anonymousAuthenticationProvider" />
<ref local="rememberMeAuthenticationProvider" />
</list>
</property>
</bean>
<!-- DAO認(rèn)證源提供者 -->
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="inMemDaoImpl" />
</bean>
<!-- 用戶信息源(內(nèi)存) -->
<bean id="inMemDaoImpl" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
<property name="userMap">
<value>
admin=password,ROLE_ADMIN,ROLE_USER
user1=password,ROLE_USER
</value>
</property>
</bean>
<!-- 免登錄認(rèn)證過濾器 -->
<bean id="rememberMeProcessingFilter" class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="rememberMeServices" ref="rememberMeServices"></property>
</bean>
<!-- 免登錄服務(wù) -->
<bean id="rememberMeServices" class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices">
<property name="userDetailsService" ref="inMemDaoImpl" />
<property name="key" value="springRocks" />
<property name="tokenValiditySeconds" value="1209600" />
<property name="parameter" value="_acegi_security_remember_me" />
<property name="cookieName" value="ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE" />
<!--
<property name="alwaysRemember" value="true" />
-->
</bean>
<!-- 免登錄認(rèn)證源提供者 -->
<bean id="rememberMeAuthenticationProvider" class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
<property name="key" value="springRocks" />
</bean>
<!-- 匿名認(rèn)證過濾器 -->
<bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
<property name="key" value="foobar" />
<property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS" />
</bean>
<!-- 匿名認(rèn)證源提供者 -->
<bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
<property name="key" value="foobar" />
</bean>
<!-- 異常處理過濾器 -->
<bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
<property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint" />
<property name="accessDeniedHandler" ref="accessDeniedHandlerImpl" />
</bean>
<!-- 表單認(rèn)證入口點(diǎn) -->
<bean id="authenticationProcessingFilterEntryPoint" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/login.jsp" />
<property name="forceHttps" value="false" />
<property name="serverSideRedirect" value="false" />
</bean>
<!-- 授權(quán)拒絕句柄 -->
<bean id="accessDeniedHandlerImpl" class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
<property name="errorPage" value="/accessDenied.jsp" />
</bean>
說明:
在基本認(rèn)證過濾器之前加入退出過濾器,之后加入免登錄過濾器和匿名過濾器。參數(shù):
Bean |
參數(shù) |
描述 |
logoutFilter |
構(gòu)造參數(shù)1 |
指定退出后的重定向url |
構(gòu)造參數(shù)2 |
指派退出的執(zhí)行句柄,多值 退出免登錄服務(wù) 退出安全上下文 |
|
securityContextLogoutHandler |
invalidateHttpSession |
是否讓HTTP會(huì)話失效 |
authenticationProcessingFilter |
authenticationManager |
指派認(rèn)證管理器 |
authenticationFailureUrl |
認(rèn)證失敗的URL。 |
|
defaultTargetUrl |
認(rèn)證成功之后的缺省URL。兩種情況下使用:1. 用戶直接進(jìn)入登錄頁(yè);2. alwaysUseDefaultTargetUrl設(shè)為true。(一般情況是用戶進(jìn)入受保護(hù)頁(yè)時(shí),acegi會(huì)先跳轉(zhuǎn)到登錄頁(yè),認(rèn)證成功之后再跳轉(zhuǎn)到用戶要訪問的頁(yè)面。如果用戶直接進(jìn)入登錄頁(yè),認(rèn)證成功之后acegi不知道用戶要訪問的頁(yè)面是什么時(shí),采用該值。) |
|
alwaysUseDefaultTargetUrl |
是否無論用戶要訪問的頁(yè)面是什么,認(rèn)證成功之后都跳轉(zhuǎn)到defaultTargetUrl。 |
|
filterProcessesUrl |
表單提交的Action,默認(rèn)值為 |
|
rememberMeServices |
指派免登錄服務(wù) |
|
authenticationManager |
providers |
指派認(rèn)證源提供者,多值 DAO認(rèn)證源提供者 免登錄認(rèn)證源提供者 匿名認(rèn)證源提供者 |
daoAuthenticationProvider |
userDetailsService |
指派用戶信息源 |
inMemDaoImpl |
userMap |
用戶信息 |
rememberMeProcessingFilter 免登錄過濾器 |
authenticationManager |
指派認(rèn)證管理器 |
rememberMeServices |
指派免登錄服務(wù) |
|
rememberMeServices 免登錄服務(wù) |
userDetailsService |
指派用戶信息源 |
key |
指定密鑰 |
|
tokenValiditySeconds |
免登錄的時(shí)間段,單位為秒,缺省值為1209600,合14天 |
|
parameter |
在登錄表單中提交的參數(shù)名,acegi依據(jù)當(dāng)前值判斷用戶是否需要免登錄服務(wù),缺省值為: |
|
cookieName |
保存在瀏覽器的Cookie名,acegi依據(jù)cookie值完成自動(dòng)登錄,達(dá)到用戶免登錄目的,缺省值為: |
|
alwaysRemember |
是否自動(dòng)提供免登錄服務(wù),將該參數(shù)設(shè)為true時(shí),無論用戶是否選擇都提供免登錄服務(wù),設(shè)為true時(shí)會(huì)覆蓋parameter的作用。(一般在HTTP基本認(rèn)證時(shí)采用,表單認(rèn)證時(shí)不用) |
|
rememberMeAuthenticationProvider 免登錄認(rèn)證源提供者 |
key |
指定密鑰,和免登錄服務(wù)的密鑰保持一致 |
anonymousProcessingFilter 匿名過濾器 |
userAttribute |
指定匿名登錄的用戶和角色,格式: uid,role |
key |
指定密鑰 |
|
anonymousAuthenticationProvider |
key |
指定密鑰,和匿名過濾器的密鑰保持一致 |
exceptionTranslationFilter |
authenticationEntryPoint |
指派認(rèn)證入口點(diǎn) |
accessDeniedHandler |
指派授權(quán)拒絕處理器 |
|
authenticationProcessingFilterEntryPoint |
loginFormUrl |
指定登錄頁(yè)面,如:/login.jsp |
forceHttps |
是否強(qiáng)制使用https協(xié)議 |
|
serverSideRedirect |
是否采用WEB服務(wù)器內(nèi)部跳轉(zhuǎn)到登錄頁(yè)面 |
|
accessDeniedHandlerImpl |
errorPage |
訪問無權(quán)限的頁(yè)面時(shí),acegi跳轉(zhuǎn)的錯(cuò)誤頁(yè)面 |
Spring Bean關(guān)系圖:

說明:每個(gè)圖塊為一個(gè)Spring Bean。斜體Bean和同名正體Bean為同一個(gè)Bean。
問題:
在表單認(rèn)證下,加入了“退出”之后,是可以退出Acegi安全上下文的。因此之前HTTP基本認(rèn)證不能退出可能是Acegi的一個(gè)BUG。
posted @ 2010-02-25 18:02 xuyang 閱讀(1937) | 評(píng)論 (2) | 編輯 收藏