`
tof.j
  • 浏览: 132839 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类

Spring安全系统:Acegi Security Acegi简介

阅读更多
Acegi安全系统,是一个用于Spring Framework的安全框架,能够和目前流行的Web容器无缝集成。它使用了Spring的方式提供了安全和认证安全服务,包括使用Bean Context,拦截器和面向接口的编程方式。因此,Acegi安全系统能够轻松地适用于复杂的安全需求。
       安全涉及到两个不同的概念,认证和授权。前者是关于确认用户是否确实是他们所宣称的身份。授权则是关于确认用户是否有允许执行一个特定的操作。
       在Acegi安全系统中,需要被认证的用户,系统或代理称为"Principal"。Acegi安全系统和其他的安全系统不同,它并没有角色和用户组的概念。

Acegi系统设计

  关键组件

      Acegi安全系统包含以下七个关键的功能组件:
         l Authentication对象,包含了Principal,Credential和Principal的授权信息。同时还可以包含关于发起认证请求的客户的其他信息,如IP地址。
        2 ContextHolder对象,使用ThreadLocal储存Authentication对象的地方。
        3 AuthenticationManager,用于认证ContextHolder中的Authentication对象。
        4 AccessDecissionManager,用于授权一个特定的操作。
        5 RunAsManager,当执行特定的操作时,用于选择性地替换Authentication对象。
        6 Secure Object拦截器,用于协调AuthenticationManager,AccessDecissionManager,RunAsManager和特定操作的执行。
        7 ObjectDefinitionSource,包含了特定操作的授权定义。

      这七个关键的功能组件的关系如下图所示(图中灰色部分是关键组件):


  安全管理对象

       Acegi安全系统目前支持两类安全管理对象。
       第一类的安全管理对象管理AOP Alliance的MethodInvocation,开发人员可以用它来保护Spring容器中的业务对象。为了使Spring管理的Bean可以作为MethodInvocation来使用,Bean可以通过ProxyFactoryBean和BeanNameAutoProxyCreator来管理,就像在Spring的事务管理一样使用。
       第二类是FilterInvocation。它用过滤器(Filter)来创建,并简单地包装了HTTP的ServletRequest,ServletResponse和FilterChain。FilterInvocation可以用来保护HTTP资源。通常,开发人员并不需要了解它的工作机制,因为他们只需要将Filter加入web.xml,Acegi安全系统就可以工作了。

  安全配置参数

       每个安全管理对象都可以描述数量不限的各种安全认证请求。例如,MethodInvocation对象可以描述带有任意参数的任意方法的调用,而FilterInvocation可以描述任意的HTTP URL。
       Acegi安全系统需要记录应用于每个认证请求的安全配置参数。例如,对于BankManager.getBalance(int accountNumber)方法和BankManager.approveLoan(int applicationNumber)方法,它们需要的认证请求的安全配置很不相同。
       为了保存不同的认证请求的安全配置,需要使用配置参数。从实现的视角来看,配置参数使用ConfigAttribute接口来表示。Acegi安全系统提供了ConfigAttribute接口的一个实现,SecurityConfig,它把配置参数保存为一个字符串。
       ConfigAttributeDefinition类是ConfigAttribute对象的一个简单的容器,它保存了和特定请求相关的ConfigAttribute的集合。
       当安全拦截器收到一个安全认证请求时,需要决定应用哪一个配置参数。换句话说,它需要找出应用于这个请求的ConfigAttributeDefinition对象。这个查找的过程是由ObjectDefinitionSource接口来处理的。这个接口的主要方法是public ConfigAttributeDefinition getAttributes(Object object),其中Object参数是一个安全管理对象。因为安全管理对象包含有认证请求的详细信息,所以ObjectDefinitionSource接口的实现类可以从中获得所需的详细信息,以查找相关的ConfigAttributeDefiniton对象。

  Acegi如何工作

       为了说明Acegi安全系统如何工作,我们设想一个使用Acegi的例子。通常,一个安全系统需要发挥作用,它必须完成以下的工作:
       l 首先,系统从客户端请求中获得Principal和Credential;
      2 然后系统认证Principal和Credential信息;
      3 如果认证通过,系统取出Principal的授权信息;
      4 接下来,客户端发起操作请求;
      5 系统根据预先配置的参数检查Principal对于该操作的授权;
      6 如果授权检查通过则执行操作,否则拒绝。

      那么,Acegi安全系统是如何完成这些工作的呢?首先,我们来看看Acegi安全系统的认证和授权的相关类图:

 

图中绿色部分是安全拦截器的抽象基类,它包含有两个管理类,AuthenticationManager和AccessDecisionManager,如图中灰色部分。AuthenticationManager用于认证ContextHolder中的Authentication对象(包含了Principal,Credential和Principal的授权信息);AccessDecissionManager则用于授权一个特定的操作。
      下面来看一个MethodSecurityInterceptor的例子:
      <bean id="bankManagerSecurity"
                     class="net.sf.acegisecurity.intercept.method.MethodSecurityInterceptor">
             <property name="validateConfigAttributes">
                    <value>true</value>
            </property>
            <property name="authenticationManager">
                   <ref bean="authenticationManager"/>
            </property>
            <property name="accessDecisionManager">
                  <ref bean="accessDecisionManager"/>
            </property>
            <property name="objectDefinitionSource">
                  <value>
                     net.sf.acegisecurity.context.BankManager.delete*=
                             ROLE_SUPERVISOR,RUN_AS_SERVER
                     net.sf.acegisecurity.context.BankManager.getBalance=
                             ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_
                  </value>
            </property>
      </bean>

 

      上面的配置文件中,MethodSecurityInterceptor是AbstractSecurityInterceptor的一个实现类。它包含了两个管理器,authenticationManager和accessDecisionManager。这两者的配置如下:

      <bean id="authenticationDao" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">
               <property name="dataSource"><ref bean="dataSource"/></property>
      </bean>
      <bean id="daoAuthenticationProvider"
                     class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
               <property name="authenticationDao"><ref bean="authenticationDao"/></property>
      </bean>
      <bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
               <property name="providers">
                      <list><ref bean="daoAuthenticationProvider"/></list>
               </property>
      </bean>

      <bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>
      <bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased">
               <property name="allowIfAllAbstainDecisions"><value>false</value></property>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics