acegi security实践教程—basic认证

    上篇已经介绍了acegi整体框架,给大家一个感性的认识。那这边博客开始进入代码实践——基于basic认证。
  我们已经说到,acegi主要通过过滤连来实现认证和授权操作。

  具体步骤如下:

  开发环境:

  MyEclispe10.7.1+tomcat6.0.37+acegi1.0.5+spring2.0

  项目目录如下:其中readme主要用来记录本次验证目的

   acegi security实践教程—basic认证_第1张图片
   

   配置文件

   web.xml:
 <?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version= "2.5">
  < display-name></display-name >
  <!-- spring 配置文件 -->
  < context-param>
    <param-name >contextConfigLocation </param-name >
    <param-value > 
            classpath:config/spring/spring-acegi.xml 
        </param-value >
  </ context-param>
 
  <!-- acegi对页面校验控制 -->
  < filter>
    <filter-name >AcegiFilterChainProxy </filter-name >
    <filter-class >
                org.acegisecurity.util.FilterToBeanProxy
            </filter-class >
    <init-param >
      <param-name >targetBean </param-name >
      <param-value >filterChainProxy </param-value >
    </init-param >
  </ filter>
  < filter-mapping>
    <filter-name >AcegiFilterChainProxy </filter-name >
    <url-pattern >/j_acegi_security_check </url-pattern >
  </ filter-mapping>
  < filter-mapping>
    <filter-name >AcegiFilterChainProxy </filter-name >
    <url-pattern >/j_acegi_logout </url-pattern >
  </ filter-mapping>
  < filter-mapping>
    <filter-name >AcegiFilterChainProxy </filter-name >
    <url-pattern >*.do </url-pattern >
  </ filter-mapping>
  < filter-mapping>
    <filter-name >AcegiFilterChainProxy </filter-name >
    <url-pattern >*.jsp </url-pattern >
  </ filter-mapping>
 
 
  < welcome-file-list>
      <welcome-file >index.jsp </welcome-file >
  </ welcome-file-list>
 
  <!-- spring配置 -->
  < listener>
       <listener-class >
        org.springframework.web.context.ContextLoaderListener
       </listener-class >
 </ listener>
</web-app> 
  
acegi配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns= "http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
     
     <!-- 通过过滤连形式,acegi提供很多filter,其中过滤器执行也有一定的顺序 ,同事支持正则和ant匹配-->
     
     <bean id ="filterChainProxy" class= "org.acegisecurity.util.FilterChainProxy" >
            <property name ="filterInvocationDefinitionSource">
                 <value >
                     PATTERN_TYPE_APACHE_ANT
                     /**=basicProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor
                 </value >
            </property >
     </bean >
           
   <!-- 基于basic认证 -->
     <bean id ="basicProcessingFilter" class= "org.acegisecurity.ui.basicauth.BasicProcessingFilter" >
            <property name ="authenticationManager" ref= "authenticationManager" />
            <property name ="authenticationEntryPoint" ref= "basicProcessingFilterEntryPoint" />
     </bean >
     
     <bean id ="basicProcessingFilterEntryPoint"
           class= "org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint" >
            <property name ="realmName" value="Acegi First Realm Name" />
     </bean >
     
     <bean id ="authenticationManager"
            class= "org.acegisecurity.providers.ProviderManager" >
            <property name ="providers">
                 <list >
                      <ref local ="daoAuthenticationProvider" />
                 </list >
            </property >
     </bean >
     
     
    <!-- 从数据库中读取用户信息验证身份 -->
     <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 >
                     test=1,ROLE_SUPERVISOR
                     zhangsan=1,ROLE_SUPERVISOR,disabled
                 </value >
            </property >
     </bean >
     
     <!-- exception filter -->
     <bean id ="exceptionTranslationFilter"
           class= "org.acegisecurity.ui.ExceptionTranslationFilter" >
            <property name ="authenticationEntryPoint" ref= "basicProcessingFilterEntryPoint" />
     </bean >
     
   <bean id ="filterInvocationInterceptor"
           class= "org.acegisecurity.intercept.web.FilterSecurityInterceptor" >
            <property name ="authenticationManager" ref= "authenticationManager" />
            <property name ="accessDecisionManager" ref= "httpRequestAccessDecisionManager" />
            <property name ="objectDefinitionSource"> 
                 <value ><![CDATA[
                     PATTERN_TYPE_APACHE_ANT
                     /secure.jsp=ROLE_SUPERVISOR
                 ]]></value>
            </property >
     </bean >

     <bean id ="httpRequestAccessDecisionManager"
            class= "org.acegisecurity.vote.AffirmativeBased" >
            <property name ="decisionVoters">
                 <list >
                      <bean class= "org.acegisecurity.vote.RoleVoter" />
                 </list >
            </property >
     </bean >
</beans>

  讲解如下:

  本次测试中,只应用三种filter,根据上篇博客的顺序写好,其中basic认证filter、异常filter、保护urlfilter。
  basic认证filter主要通过认证管理器、然后认证管理器再委托provider认证——daoAuthenticationProvider,其中daoAuthenticationProvider主要通过内存配置方式来获取相应的userDetails对象。
  exception filter中,若出现异常,则交给authenticationEntryPoint,也就是basicProcessingFilterEntryPoint来处理。
  filterInvocationInterceptor,是在认证通过后,accessDecisionManager调用自己的投票机制,进行投票。其中objectDefinitionSource也支持正则和ant模式匹配,比如/secure.jsp文件,就需要ROLE_SUPERVISOR角色,否则也出错。
  In-Memory 认证,在上面的例子中,userMap属性包含了每个用户的用户名,密码,一个授权列表以及一个可选的启用/禁用关键词。使用逗号分隔。用户名必须在等号的左侧,密码必须在等号右侧第一个出现。启用和禁用关键词(大小写敏感)可以出现在第二个或者之后任意位置。剩余的字符串被看作是授予的权限,这些权钱被创建为GrantedAuthorityImpl对象(仅供参考-大多数的应用不需要自定义的GrantedAuthority实现,所以使用默认的实现就可以了)。注意如果一个用户没有密码及或没有被授予权限,该用户不会在in-memory 认证库中创建。
  debug调试:
  具体debug调试关键过程及源码,见下篇博客

你可能感兴趣的:(Acegi,Basic认证)