我的JAVA

我的门户
随笔 - 5, 文章 - 0, 评论 - 2, 引用 - 0
数据加载中……

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,默认值为
/j_acegi_security_check

rememberMeServices

指派免登录服务

authenticationManager
认证管理器

providers

指派认证源提供者,多值

DAO认证源提供者

免登录认证源提供者

匿名认证源提供者

daoAuthenticationProvider
DAO
认证源提供者

userDetailsService

指派用户信息源

inMemDaoImpl
用户信息源(内存)

userMap

用户信息

rememberMeProcessingFilter

免登录过滤器

authenticationManager

指派认证管理器

rememberMeServices

指派免登录服务

rememberMeServices

免登录服务

userDetailsService

指派用户信息源

key

指定密钥

tokenValiditySeconds

免登录的时间段,单位为秒,缺省值为1209600,合14

parameter

在登录表单中提交的参数名,acegi依据当前值判断用户是否需要免登录服务,缺省值为:
_acegi_security_remember_me

cookieName

保存在浏览器的Cookie名,acegi依据cookie值完成自动登录,达到用户免登录目的,缺省值为:
ACEGI_SECURITY_HASHED_
REMEMBER_ME_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 on 2010-02-25 18:02 xuyang 阅读(1911) 评论(2)  编辑  收藏 所属分类: Acegi

评论

# re: Acegi 配置指南(4)  回复  更多评论   

很强大的博文,十分有用!学习了!
2012-11-30 10:43 | 插葱的大蒜

# re: Acegi 配置指南(4)  回复  更多评论   

阿斯顿
2016-04-18 15:10 | 阿斯顿

只有注册用户登录后才能发表评论。


网站导航: