---
title: "acegi学习随感"
author: "morle"
date: "2007-07-04T11:42:00Z"
tags: []
slug: "acegi"
draft: false
lastmod: "2007-07-04T11:42:00Z"
id: 97701
url: "https://morle.iteye.com/blog/97701"
---

自接触到acegi认证体系来，在网上搜了些资料，到目前为止，做个笔记，来加深对acegi的学习

 acegi1.04,spring2.0

acegi 配置，涉及登陆验证filter,没有Interceptor

xml 代码 

01. xml version="1.0" encoding="UTF-8"?&gt;
02. &gt;
03. &lt;beans&gt;
04. &lt;bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"&gt;
05. &lt;property name="filterInvocationDefinitionSource"&gt;
06. &lt;value&gt;
07. CONVERT\_URL\_TO\_LOWERCASE\_BEFORE\_COMPARISON
08. PATTERN\_TYPE\_APACHE\_ANT
09. /\*\*=httpSessionContextIntegrationFilter,authenticationProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter
10. value&gt;
11. property&gt;
12. bean&gt;
13. beans&gt;

 

Fiter chain说明：

   1.   httpSessionContextIntegrationFilter:

     每次request前 HttpSessionContextIntegrationFilter从Session中获取Authentication对象，在request完后又把Authentication对象保存到Session中供下次request使用,此filter必须其他Acegi filter前使用

 在上述代码添加下列段，实例化中属性context默认为SecurityContextImpl.class

xml 代码 

1. &lt;bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" /&gt;

   2.  authenticationProcessingFilter:   

通过Providers提供认证者列表,如果一个认证提供者失败可以尝试另外一个认证提供者,以保证获取不同来源的身份认证，如  
         DaoAuthenticationProvider 从数据库中读取用户信息验证身份,可自定义验证身份  
         AnonymousAuthenticationProvider 匿名用户身份认证  
         RememberMeAuthenticationProvider 已存cookie中的用户信息身份认证  
         RunAsImplAuthenticationProvider 对身份已被管理器替换的用户进行验证

         其它的还有  
         AuthByAdapterProvider 使用容器的适配器验证身份  
         CasAuthenticationProvider 根据Yale中心认证服务验证身份, 用于实现单点登陆  
         JaasAuthenticationProvider 从JASS登陆配置中获取用户信息验证身份  
         RemoteAuthenticationProvider 根据远程服务验证用户身份  
         X509AuthenticationProvider 从X509认证中获取用户信息验证身份  
         TestingAuthenticationProvider 单元测试时使用

         每个认证者会对自己指定的证明信息进行认证，如DaoAuthenticationProvider仅对UsernamePasswordAuthenticationToken这个证明信息进行认证。  
    

本人代码如下：DaoAuthenticationProvider，AnonymousAuthenticationProvider ，RememberMeAuthenticationProvider ，RunAsImplAuthenticationProvider ，只需其中的一个通过认证，即可 &lt;property name="key"&gt;&lt;/property&gt;

01. &lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;
02. &lt;property name="providers"&gt;
03. &lt;list&gt;
04. &lt;ref local="daoAuthenticationProvider" /&gt;
05. &lt;ref local="anonymousAuthenticationProvider" /&gt;
06. &lt;ref local="rememberMeAuthenticationProvider" /&gt;
07. &lt;ref local="runAsAuthenticationProvider" /&gt;
08. list&gt;
09. property&gt;
10. bean&gt;

2.1.基于数据库的认证提供者daoAuthenticationProvider:

         authenticationDao 认证数据访问对象，用于获取用户信息，包括：用户名，用户密码，用户状态和用户权限   
          可自定义，本人采用继承org.acegisecurity.providers.dao.DaoAuthenticationProvider的DAO,覆盖retrieveUser和additionalAuthenticationChecks方法,其中的字段userDetailsService，可以通过两种inMemory 或 jdbcDaoImpl获取用户信息，包括：用户名，用户密码，用户状态和用户权限,也可以通过自定义其他方法获取用户信息,覆盖loadUserByUsername方法,本人采用后一种        
         userCache ehcache 缓存user信息

xml 代码 

01. &lt;bean id="daoAuthenticationProvider" class="com.d3.gamestat.web.AuthenticationProviderDao"&gt;
02. &lt;property name="userDetailService"&gt;&lt;ref local="jdbcHbmServiceImp"/&gt;property&gt;
03. &lt;property name="userCache"&gt;&lt;ref local="userCache"/&gt;property&gt;
04. bean&gt;
05. &lt;bean id="jdbcHbmServiceImp" class="com.d3.gamestat.web.JdbcHbmServiceImp"&gt;
06. &lt;property name="serviceProxy"&gt;&lt;ref bean="gameStatServiceProxy"/&gt;property&gt;
07. &lt;property name="serviceId"&gt;&lt;value&gt;scurityUserManagervalue&gt;property&gt;
08. &lt;property name="methodName"&gt;&lt;value&gt;getUservalue&gt;property&gt;
09. bean&gt;
10. &lt;bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache"&gt;
11. &lt;property name="cache"&gt;&lt;ref local="userCacheBackend"/&gt;property&gt;
12. bean&gt;
13. &lt;bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;
14. &lt;property name="cacheManager"&gt;&lt;ref local="cacheManager"/&gt;property&gt;
15. &lt;property name="cacheName"&gt;&lt;value&gt;userCachevalue&gt;property&gt;
16. bean&gt;
17. &lt;bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/&gt;

  2.2认证提供者anonymousAuthenticationProvider,用于认证匿名用户,字段key中是org.acegisecurity.providers.anonymous.AnonymousProcessingFilter中的key字段

xml 代码 

1. &lt;bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider"&gt;
2. &lt;property name="key"&gt;&lt;value&gt;ssl&lt;/value&gt;&lt;/property&gt;
3. &lt;/bean&gt;

 2.3认证提供者rememberMeAuthenticationProvider,用于认证 cookies 登陆,字段key中是org.acegisecurity.ui.rememberme.RememberMeProcessingFilter中rememberMeServices中的key字段  

xml 代码 

1. &lt;bean id="rememberMeAuthenticationProvider" class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"&gt;
2. &lt;property name="key"&gt;&lt;value&gt;springRocks&lt;/value&gt;&lt;/property&gt;
3. &lt;/bean&gt;

 

2.4认证提供者runAsAuthenticationProvider,用于认证 更改用户信息,字段key中是org.acegisecurity.runas.RunAsManagerImpl中的key字段

xml 代码 

1. &lt;bean id="runAsAuthenticationProvider" class="org.acegisecurity.runas.RunAsImplAuthenticationProvider"&gt;
2. &lt;property name="key"&gt;&lt;value&gt;runAs&lt;/value&gt;property&gt;
3. bean&gt;

 3.anonymousProcessingFilter

    anonymousProcessingFilter的作用是判断ContextHolder中是否有Authentication对象，如果没有就创建一个Authentication对象，其中包含的用户名是anonymousUser，用户权限是ROLE\_ANONYMOUS。这使得没有登录的匿名用户能够自动的获得匿名的用户名和权限

xml 代码 

1. &lt;bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter"&gt;
2. &lt;property name="key"&gt;&lt;value&gt;sslvalue&gt;property&gt;
3. &lt;property name="userAttribute"&gt;
4. &lt;value&gt;anonymousUser,ROLE\_ANONYMOUSvalue&gt;
5. property&gt;
6. bean&gt;

 4.exceptionTranslationFilter:

   用户尚未通过身份验证时，会将控制转交到一个认证入口点，提供三种实现  
    BasicProcessingFilterEnteyPoint :HTTP基本认证处理  
    AuthenticationProcessingFilterEntryPoint :将用户重新定向到一个基于HTML表单的登入界面  
    CasProcessingFilterEntryPoint :将用户重新定向到一个基于Yale CAS登入界面

xml 代码 

01. &lt;bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter"&gt;
02. &lt;property name="authenticationEntryPoint"&gt;
03. &lt;bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"&gt;
04. &lt;property name="loginFormUrl" value="/login.jsp"/&gt;
05. &lt;property name="forceHttps" value="false"/&gt;
06. bean&gt;
07. property&gt;
08. &lt;property name="accessDeniedHandler"&gt;
09. &lt;bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl"&gt;
10. &lt;property name="errorPage" value="/login.jsp"/&gt;
11. bean&gt;
12. property&gt;
13. bean&gt;

编辑器总出问题,本想全部发布在上面,无奈,先写这些,鉴于本人能力有限,希望给某些人一点参考