---
title: "ldap与Acegi? ----Acegi配置文件解剖"
author: "rmn190"
date: "2008-03-23T12:19:06Z"
tags: ["Acegi", "Bean", "JSP", "Spring", "配置管理"]
categories: ["企业架构"]
slug: "ldap-acegi-acegi"
draft: false
lastmod: "2008-03-23T12:19:06Z"
id: 175041
url: "https://rmn190.iteye.com/blog/175041"
---

ldap与Acegi? ----Acegi配置文件解剖

这两个之间没什么必然的联系吧?ldap不熟悉,Acegi更是不熟悉.呵呵,这两个再搅和在一起就乱了.

ldap好像是与数据库连接相关的一个东东,那它与现在常见的数据库连接JDBC有什么区别?与JNDI呢?与这两个比又有什么优势?这个优势又怎么暗合了Acegi的需求?

对于Acegi只知道它是与Spring协作很好的一个Security框架,先不说Security别的方面,现在只看它如何管理登录这块,登录肯定是要访问数据库,那么在Acegi的配置文件中又是怎么来体现管理这个访问数据库的呢?

仔细研究项目中定义的applicationContext-acegi-secutiry.xml文件,这里面配置了如下的几个Bean:  
 1,filterChainProxy --&gt;org.acegisecurity.util.FilterChainProxy  
    property: filterInvocationDefinitionSource,其值有:  
        CONVERT\_URL\_TOLOWERCASE\_BEFORE\_COMPARISON  
        PATTEN\_TYPE\_APCHE\_ANT

        /\*\*=httpSessionContextIntegrationFilter,        logoutFilter,authenicationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor

        ?\**  
        这么一长串是参数是干啥的,filterInvocationDefinitionSource是个什么类型?  
            看了一下它的源代码,其类型为:FilterInvocationDefinitionSource,那么这个类型怎么又会接收上面那么长的字符串?

            再看FilterInvocationDefinitionSource的源代码,它是个接口,这下更惨了,FilterChainProxy初始化调用set时怎么又会初始化一个接口?内部匿名类?

            先往下看.........  
        ?  
2,httpSessionContextIntegrationFilter --&gt;org.acegisecurity.context.HttpSessionContextIntegrationFilter. 这个类没有属性可配.  
3,logoutFilter --&gt;org.acegisecurity.ui.logout.LogoutFilter  
    通过&lt;constuctor-arg&gt;配置了value="/index.htm".  
    又通过&lt;constructor-arg&gt;配置了 一个List其值为:  
      
    &lt;ref bean = "remeberMeServices"&gt;?????????????????????????????????  
      
    和&lt;bean class = "org.acegisecurity.ui.logout.SecurityContextLogoutHandler" /&gt;  
4,authenicationProcessingFilter --&gt; org.acegisecurity.ui.webapp.AuthenticationProcessingFilter  
    六个property:  
        authenticationManager ref authenticationManager ??????????????????????????  
        authenticationFailureUrl value="/jsp/accessDenied.jsp"  
        alwaysUseDefaultTargetUrl value = "true"  
        defaultTargetUrl value="/pages/content.html"/  
        filterProcessesUrl value="/jsp/j\_acegi\_security\_check" ???????这个好像很关键的.  
        rememberMeServices  ref="rememberMeServices" ???????????????????  
5,securityContextHolderAwareRequestFilter --&gt;　org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter　没任何属性可配．

6,rememberMeProcessingFilter --&gt; org.acegisecurity.ui.rememberme.RememberMeProcessingFilter  
    两个属性:  
    authenticationManager ref="authenticationManager" ???????????????????  
    rememberMeServices ref="rememberMeServices" ??????????????????

7,anonymousProcessingFilter --&gt; org.acegisecurity.providers.anonymous.AnonymousProcessingFilter  
    两个属性:  
    key value="changeThis"  
    userAttribute  value="anonymousUser,ROLE\_ANONYMOUS"

8,exceptionTranslationFilter --&gt; org.acegisecurity.ui.ExceptionTranslationFilter  
    两个属性:  
    &lt;property name="authenticationEntryPoint"&gt;  
            &lt;bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"&gt;  
                &lt;property name="loginFormUrl" value="/jsp/login.jsp"/&gt;  
                &lt;property name="forceHttps" value="false"/&gt;  
            &lt;/bean&gt;  
        &lt;/property&gt;  
        &lt;property name="accessDeniedHandler"&gt;  
            &lt;bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl"&gt;  
                &lt;property name="errorPage" value="/jsp/accessDenied.jsp"/&gt;  
            &lt;/bean&gt;  
        &lt;/property&gt;

9,filterInvocationInterceptor --&gt; org.acegisecurity.intercept.web.FilterSecurityInterceptor  
    三个属性:  
    authenticationManager ref="authenticationManager" ???????????????????????  
    &lt;property name="accessDecisionManager"&gt;  
            &lt;bean class="org.acegisecurity.vote.AffirmativeBased"&gt;  
                &lt;property name="allowIfAllAbstainDecisions" value="false"/&gt;  
                &lt;property name="decisionVoters"&gt;  
                    &lt;list&gt;  
                        &lt;bean class="org.acegisecurity.vote.RoleVoter"/&gt;  
                        &lt;bean class="org.acegisecurity.vote.AuthenticatedVoter"/&gt;  
                    &lt;/list&gt;  
                &lt;/property&gt;  
            &lt;/bean&gt;  
        &lt;/property&gt;  
        &lt;property name="objectDefinitionSource"&gt;  
            &lt;value&gt;  
                CONVERT\_URL\_TO\_LOWERCASE\_BEFORE\_COMPARISON  
                PATTERN\_TYPE\_APACHE\_ANT  
                /jsp/login.jsp=ROLE\_ANONYMOUS  
                /jsp/\*\*=ROLE\_MPIXTOOLGROUP  
            &lt;/value&gt;  
        &lt;/property&gt;

10, rememberMeServices --&gt; org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices  
    两个属性:  
    &lt;property name="userDetailsService" ref="userDetailsService"/&gt; ????????????  
        &lt;property name="key" value="changeThis"/&gt;  
11, authenticationManager --&gt; org.acegisecurity.providers.ProviderManager  
    一个属性:  
    &lt;property name="providers"&gt; ????????难道说这可就是登录信息的验证来源?providers嘛.  
            &lt;list&gt;  
                &lt;!-- To Disable LDAP, comment out ldapAuthProvider reference below --&gt;  
                &lt;ref local="ldapAuthProvider"/&gt;  
                &lt;ref local="daoAuthenticationProvider"/&gt;  
                &lt;bean class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider"&gt;  
                    &lt;property name="key" value="changeThis"/&gt;  
                &lt;/bean&gt;  
                &lt;bean class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"&gt;  
                    &lt;property name="key" value="changeThis"/&gt;  
                &lt;/bean&gt;  
            &lt;/list&gt;  
        &lt;/property&gt;  
12, daoAuthenticationProvider --&gt; org.acegisecurity.providers.dao.DaoAuthenticationProvider  
    一个属性:  
    &lt;property name="userDetailsService" ref="userDetailsService"/&gt;  
13, userDetailsService --&gt; org.acegisecurity.userdetails.memory.InMemoryDaoImpl  
    一个属性:  
    &lt;property name="userMap"&gt;  
            &lt;value&gt;  
                jklaassen=4moreyears,ROLE\_ADMIN  
                test=test,ROLE\_MPIXTOOLGROUP   ?????????????这是静态的验证,没有去数据库里查用户和密码吗?  
                devteam=get2work,ROLE\_MPIXTOOLGROUP  
                jgaerlan=1234,ROLE\_MPIXTOOLGROUP  
                opts=opts,ROLE\_OPERATIONS  
            &lt;/value&gt;  
        &lt;/property&gt;

    注意下面有一段配置被注释掉了: ??????????????这个是要到数据库里查找吧?  
    &lt;!--  
            &lt;bean id="userDetailsService" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"&gt;  
            &lt;property name="dataSource"&gt;  
                &lt;ref bean="dataSource"/&gt;  
            &lt;/property&gt;  
            &lt;property name="usersByUsernameQuery"&gt;  
                &lt;value&gt;{call dbo.MLab\_User\_GetInfoByUserName(?)}&lt;/value&gt;  
            &lt;/property&gt;  
            &lt;property name="authoritiesByUsernameQuery"&gt;  
                &lt;value&gt;{call dbo.MLab\_UserRole\_GetInfoByID(?)}&lt;/value&gt;  
            &lt;/property&gt;  
            &lt;/bean&gt;  
    --&gt;  
14, initialDirContextFactory --&gt; org.acegisecurity.ldap.DefaultInitialDirContextFactory  
    其配置如下:  
    &lt;constructor-arg value="ldap://dc03:389/OU=Employees,OU=Pleasanton,dc=kittyhawk,dc=funmail,dc=com"/&gt;  
        &lt;property name="managerDn"&gt;  
            &lt;value&gt;cn=mpixtool,OU=Employees,OU=Pleasanton,dc=kittyhawk,dc=funmail,dc=com&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="managerPassword"&gt;  
            &lt;value&gt;p@55w0rd&lt;/value&gt; ??????????????????这个密码并没有用到呀,而是用test的  
        &lt;/property&gt;

15, userSearch --&gt; org.acegisecurity.ldap.search.FilterBasedLdapUserSearch ?????????这个来负责登录用户的验证?  
    &lt;constructor-arg index="0"&gt;  
            &lt;value&gt;&lt;/value&gt;  
        &lt;/constructor-arg&gt;  
        &lt;constructor-arg index="1"&gt;  
            &lt;value&gt;sAMAccountName={0}&lt;/value&gt;  
        &lt;/constructor-arg&gt;  
        &lt;constructor-arg index="2"&gt;  
            &lt;ref local="initialDirContextFactory"/&gt;  
        &lt;/constructor-arg&gt;  
        &lt;property name="searchSubtree"&gt;  
            &lt;value&gt;true&lt;/value&gt;  
        &lt;/property&gt;  
16,     ldapAuthProvider --&gt; org.acegisecurity.providers.ldap.LdapAuthenticationProvider  
    &lt;constructor-arg&gt;  
            &lt;bean class="org.acegisecurity.providers.ldap.authenticator.BindAuthenticator"&gt;  
                &lt;constructor-arg&gt;  
                    &lt;ref local="initialDirContextFactory"/&gt;  
                &lt;/constructor-arg&gt;  
                &lt;property name="userSearch"&gt;  
                    &lt;ref local="userSearch"/&gt;  
                &lt;/property&gt;  
            &lt;/bean&gt;  
        &lt;/constructor-arg&gt;  
        &lt;constructor-arg&gt;  
            &lt;bean class="org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator"&gt;  
                &lt;constructor-arg&gt;  
                    &lt;ref local="initialDirContextFactory"/&gt;  
                &lt;/constructor-arg&gt;  
                &lt;constructor-arg&gt;  
                    &lt;value&gt;&lt;/value&gt;  
                &lt;/constructor-arg&gt;  
                &lt;property name="groupRoleAttribute"&gt;  
                    &lt;value&gt;cn&lt;/value&gt;  
                &lt;/property&gt;  
                &lt;property name="rolePrefix"&gt;  
                    &lt;value&gt;ROLE\_&lt;/value&gt;  
                &lt;/property&gt;  
                &lt;property name="convertToUpperCase"&gt;  
                    &lt;value&gt;true&lt;/value&gt;  
                &lt;/property&gt;  
                &lt;property name="defaultRole"&gt;  
                    &lt;value&gt;IS\_AUTHENTICATED\_FULLY&lt;/value&gt;  
                &lt;/property&gt;  
            &lt;/bean&gt;  
        &lt;/constructor-arg&gt;

一共配置了16个Bean,  
    filterChainProxy引用的Bean有:  
        --httpSessionContextIntegrationFilter,上面标号的第2个Bean.  
        --logoutFilter,上面标号的第3个Bean.  
            ----这个logoutFilter引用的有:  
                ------ rememberMeServices(标号为10)  
                    --------这个rememberMeServices引用的有:  
                        ----------userDetailsService(标号为13)  
        --authenicationProcessingFilter, 上面标号的第4个Bean.  
            ----这个authenicationProcessingFilter引用的有:  
                ------authenticationManager(标号为11)  
                    --------这个authenticationManager引用的有:  
                        ----------ldapAuthProvider(标号为16)  
                            ------------这个IdapAuthProvider引用的有:  
                                --------------initialDirContextFactory(标号为14)  
                                --------------userSearch(标号为15)  
                        ----------daoAuthenticationProvider(标号为12)  
                            ------------这个authenticationManager引用的有:  
                                --------------userDetailsService(标号为13)  
                ------ rememberMeServices(标号为10)  
        --securityContextHolderAwareRequestFilter, 上面标号的第5个Bean.  
        --rememberMeProcessingFilter,上面标号的第6个Bean.  
            ----这个rememberMeProcessingFilter引用的有:  
                ------authenticationManager(标号为11)  
                ------ rememberMeServices(标号为10)  
        --anonymousProcessingFilter,上面标号的第7个Bean.  
        --exceptionTranslationFilter,上面标号的第8个Bean.  
        --filterInvocationInterceptor,上面标号的第9个Bean.  
            ----这个filterInvocationInterceptor引用的有:  
                ------authenticationManager(标号为11)

        止此整个Bean树解析完毕,16个Bean中除了filterChainProxy自己外的15个里它引用了8个,另外的7个又都间接地引用,filterChainProxy是带头大哥无疑了!