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

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