ITeye博客

acegi学习随感

morle · 2007-07-04 11:42 · acegi学习 · Markdown

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

 acegi1.04,spring2.0

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

xml 代码 

  1. xml version="1.0" encoding="UTF-8"?>
  2. >
  3. <beans>
  4. <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
  5. <property name="filterInvocationDefinitionSource">
  6. <value>
  7. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  8. PATTERN_TYPE_APACHE_ANT
  9. /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter
  10. value>
  11. property>
  12. bean>
  13. beans>

 

Fiter chain说明:

   1.   httpSessionContextIntegrationFilter:

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

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

xml 代码 

  1. <bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" />

   2.  authenticationProcessingFilter:   

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

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

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

本人代码如下:DaoAuthenticationProvider,AnonymousAuthenticationProvider ,RememberMeAuthenticationProvider ,RunAsImplAuthenticationProvider ,只需其中的一个通过认证,即可 <property name="key"></property>

  1. <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
  2. <property name="providers">
  3. <list>
  4. <ref local="daoAuthenticationProvider" />
  5. <ref local="anonymousAuthenticationProvider" />
  6. <ref local="rememberMeAuthenticationProvider" />
  7. <ref local="runAsAuthenticationProvider" />
  8. list>
  9. property>
  10. bean>

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

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

xml 代码 

  1. <bean id="daoAuthenticationProvider" class="com.d3.gamestat.web.AuthenticationProviderDao">
  2. <property name="userDetailService"><ref local="jdbcHbmServiceImp"/>property>
  3. <property name="userCache"><ref local="userCache"/>property>
  4. bean>
  5. <bean id="jdbcHbmServiceImp" class="com.d3.gamestat.web.JdbcHbmServiceImp">
  6. <property name="serviceProxy"><ref bean="gameStatServiceProxy"/>property>
  7. <property name="serviceId"><value>scurityUserManagervalue>property>
  8. <property name="methodName"><value>getUservalue>property>
  9. bean>
  10. <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
  11. <property name="cache"><ref local="userCacheBackend"/>property>
  12. bean>
  13. <bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  14. <property name="cacheManager"><ref local="cacheManager"/>property>
  15. <property name="cacheName"><value>userCachevalue>property>
  16. bean>
  17. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

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

xml 代码 

  1. <bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
  2. <property name="key"><value>ssl</value></property>
  3. </bean>

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

xml 代码 

  1. <bean id="rememberMeAuthenticationProvider" class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
  2. <property name="key"><value>springRocks</value></property>
  3. </bean>

 

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

xml 代码 

  1. <bean id="runAsAuthenticationProvider" class="org.acegisecurity.runas.RunAsImplAuthenticationProvider">
  2. <property name="key"><value>runAs</value>property>
  3. bean>

 3.anonymousProcessingFilter

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

xml 代码 

  1. <bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
  2. <property name="key"><value>sslvalue>property>
  3. <property name="userAttribute">
  4. <value>anonymousUser,ROLE_ANONYMOUSvalue>
  5. property>
  6. bean>

 4.exceptionTranslationFilter:

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

xml 代码 

  1. <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
  2. <property name="authenticationEntryPoint">
  3. <bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
  4. <property name="loginFormUrl" value="/login.jsp"/>
  5. <property name="forceHttps" value="false"/>
  6. bean>
  7. property>
  8. <property name="accessDeniedHandler">
  9. <bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
  10. <property name="errorPage" value="/login.jsp"/>
  11. bean>
  12. property>
  13. bean>

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

评论(1)

  1. #1 楼 · camelr · 2007-07-09 19:52

    能详细解释一下filterInvocationDefinitionSource属性的设置含义么?下面代码中三行值是如何映射到对象实例中的?

    # CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON   
    #                 PATTERN_TYPE_APACHE_ANT   
    #                 /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter
    
    

    谢谢!

评论功能维护中,当前仅展示历史评论。