Acegi Security -- Spring下最优秀的安全系统 http://www.springside.org.cn/docs/reference/Acegi.htm

1. Acegi 介绍

2. Acegi 配置

3. Acegi 扩展

4. Acegi ACL使用

5. FAQ

Acegi是Spring Framework 下最成熟的安全系统,它提供了强大灵活的企业级安全服务,如完善的认证和授权机制,Http资源访问控制,Method 调用访问控制,Access Control List (ACL) 基于对象实例的访问控制,Yale Central Authentication Service (CAS) 耶鲁单点登陆,X509 认证,当前所有流行容器的认证适配器,Channel Security频道安全管理等功能。

1.1 网站资源

官方网站      http://acegisecurity.sourceforge.net
论坛            http://forum.springframework.org/forumdisplay.php?f=33
Jira              http://opensource.atlassian.com/projects/spring/browse/SEC

1.2 多方面的安全控制粒度

  1. URL 资源访问控制
     http://apps:8080/index.htm -> for public
     http://apps:8080/user.htm -> for authorized user
  2. 方法调用访问控制
    public void getData() -> all user
    public void modifyData() -> supervisor only
  3. 对象实例保护
    order.getValue() < $100 -> all user
    order.getValue() > $100 -> supervisor only

1.3 非入侵式安全架构

  1. 基于Servlet Filter和Spring aop,  使商业逻辑和安全逻辑分开,结构更清晰
  2. 使用Spring 来代理对象,能方便地保护方法调用

1.4 其它安全架构

Acegi只是安全框架之一,其实还存在其它优秀的安全框架可供选择:

二 Acegi安全系统的配置

Acegi 的配置看起来非常复杂,但事实上在实际项目的安全应用中我们并不需要那么多功能,清楚的了解Acegi配置中各项的功能,有助于我们灵活的运用Acegi于实践中。

2.1 在Web.xml中的配置

1)  FilterToBeanProxy
 
 Acegi通过实现了Filter接口的FilterToBeanProxy提供一种特殊的使用Servlet
Filter的方式,它委托Spring中的Bean --
FilterChainProxy来完成过滤功能,这好处是简化了web.xml的配置,并且充分利用了Spring
IOC的优势。FilterChainProxy包含了处理认证过程的filter列表,每个filter都有各自的功能。

    <filter>
        <filter-name>Acegi Filter Chain Proxy</filter-name>
        <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
        <init-param>
            <param-name>targetClass</param-name>
            <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
        </init-param>
    </filter>

2) filter-mapping
  <filter-mapping>限定了FilterToBeanProxy的URL匹配模式,只有*.do和*.jsp和/j_acegi_security_check 的请求才会受到权限控制,对javascript,css等不限制。

   <filter-mapping>
      <filter-name>Acegi Filter Chain Proxy</filter-name>
      <url-pattern>*.do</url-pattern>
    </filter-mapping>
   
    <filter-mapping>
      <filter-name>Acegi Filter Chain Proxy</filter-name>
      <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
   
    <filter-mapping>
      <filter-name>Acegi Filter Chain Proxy</filter-name>
      <url-pattern>/j_acegi_security_check</url-pattern>
</filter-mapping>

3) HttpSessionEventPublisher
 
 <listener>的HttpSessionEventPublisher用于发布
HttpSessionApplicationEvents和HttpSessionDestroyedEvent事件给spring的
applicationcontext。

    <listener>
        <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class>
    </listener>

2.2 在applicationContext-acegi-security.xml中

2.2.1 FILTER CHAIN

  FilterChainProxy会按顺序来调用这些filter,
使这些filter能享用Spring ioc的功能,
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定义了url比较前先转为小写,
PATTERN_TYPE_APACHE_ANT定义了使用Apache ant的匹配模式

    <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
        <property name="filterInvocationDefinitionSource">
            <value>
                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                PATTERN_TYPE_APACHE_ANT
               /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,
basicProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter,
exceptionTranslationFilter,filterInvocationInterceptor
            </value>
        </property>
    </bean>

2.2.2 基础认证

1) authenticationManager
 
 起到认证管理的作用,它将验证的功能委托给多个Provider,并通过遍历Providers,
以保证获取不同来源的身份认证,若某个Provider能成功确认当前用户的身份,authenticate()方法会返回一个完整的包含用户授权信息的
Authentication对象,否则会抛出一个AuthenticationException。
Acegi提供了不同的AuthenticationProvider的实现,如:
        DaoAuthenticationProvider 从数据库中读取用户信息验证身份
        AnonymousAuthenticationProvider 匿名用户身份认证
        RememberMeAuthenticationProvider 已存cookie中的用户信息身份认证
        AuthByAdapterProvider 使用容器的适配器验证身份
        CasAuthenticationProvider 根据Yale中心认证服务验证身份, 用于实现单点登陆
        JaasAuthenticationProvider 从JASS登陆配置中获取用户信息验证身份
        RemoteAuthenticationProvider 根据远程服务验证用户身份
        RunAsImplAuthenticationProvider 对身份已被管理器替换的用户进行验证
        X509AuthenticationProvider 从X509认证中获取用户信息验证身份
        TestingAuthenticationProvider 单元测试时使用

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

<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
        <property name="providers">
            <list>
                <ref local="daoAuthenticationProvider"/>
                <ref local="anonymousAuthenticationProvider"/>
                <ref local="rememberMeAuthenticationProvider"/>
            </list>
        </property>
</bean>

2) daoAuthenticationProvider
 
 进行简单的基于数据库的身份验证。DaoAuthenticationProvider获取数据库中的账号密码并进行匹配,若成功则在通过用户身份的同
时返回一个包含授权信息的Authentication对象,否则身份验证失败,抛出一个AuthenticatiionException。

    <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="jdbcDaoImpl"/>
        <property name="userCache" ref="userCache"/>
        <property name="passwordEncoder" ref="passwordEncoder"/>
   </bean>

3) passwordEncoder 
  使用加密器对用户输入的明文进行加密。Acegi提供了三种加密器:
PlaintextPasswordEncoder—默认,不加密,返回明文.
ShaPasswordEncoder—哈希算法(SHA)加密
Md5PasswordEncoder—消息摘要(MD5)加密

<bean id="passwordEncoder" class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"/>

4) jdbcDaoImpl 
 
 用于在数据中获取用户信息。
acegi提供了用户及授权的表结构,但是您也可以自己来实现。通过usersByUsernameQuery这个SQL得到你的(用户ID,密码,状态
信息);通过authoritiesByUsernameQuery这个SQL得到你的(用户ID,授权信息)

<bean id="jdbcDaoImpl"
class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
       
<property name="dataSource"
ref="dataSource"/>
        <property
name="usersByUsernameQuery">
           
<value>select loginid,passwd,1 from users where loginid =
?</value>
       
</property>
        <property
name="authoritiesByUsernameQuery">
           
<value>select u.loginid,p.name from users u,roles r,permissions
p,user_role ur,role_permis rp where u.id=ur.user_id and r.id=ur.role_id and
p.id=rp.permis_id
and
               
r.id=rp.role_id and p.status='1' and
u.loginid=?</value>
       
</property>
</bean>

5) userCache &  resourceCache 
  缓存用户和资源相对应的权限信息。每当请求一个受保护资源时,daoAuthenticationProvider就会被调用以获取用户授权信息。如果每次都从数据库获取的话,那代价很高,对于不常改变的用户和资源信息来说,最好是把相关授权信息缓存起来。(详见 2.6.3 资源权限定义扩展 )
userCache提供了两种实现: NullUserCache和EhCacheBasedUserCache, NullUserCache实际上就是不进行任何缓存,EhCacheBasedUserCache是使用Ehcache来实现缓功能。

    <bean id="userCacheBackend"
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
       
<property name="cacheManager"
ref="cacheManager"/>
       
<property name="cacheName" value="userCache"/>
    </bean>
    <bean id="userCache"
class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache"
autowire="byName">
        <property
name="cache" ref="userCacheBackend"/>
   
</bean>
    <bean id="resourceCacheBackend"
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
       
<property name="cacheManager"
ref="cacheManager"/>
       
<property name="cacheName" value="resourceCache"/>
    </bean>
    <bean id="resourceCache"
class="org.springside.modules.security.service.acegi.cache.ResourceCache"
autowire="byName">
        <property
name="cache" ref="resourceCacheBackend"/>
    </bean>

6) basicProcessingFilter 
 
 用于处理HTTP头的认证信息,如从Spring远程协议(如Hessian和Burlap)或普通的浏览器如IE,Navigator的HTTP头中
获取用户信息,将他们转交给通过authenticationManager属性装配的认证管理器。如果认证成功,会将一个Authentication
对象放到会话中,否则,如果认证失败,会将控制转交给认证入口点(通过authenticationEntryPoint属性装配)

    <bean id="basicProcessingFilter" class="org.acegisecurity.ui.basicauth.BasicProcessingFilter">
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="authenticationEntryPoint" ref="basicProcessingFilterEntryPoint"/>
    </bean>

7) basicProcessingFilterEntryPoint 
  通过向浏览器发送一个HTTP401(未授权)消息,提示用户登录。
处理基于HTTP的授权过程, 在当验证过程出现异常后的"去向",通常实现转向、在response里加入error信息等功能。

 <bean
id="basicProcessingFilterEntryPoint"
class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">
       
<property name="realmName" value="SpringSide Realm"/>
</bean>

8) authenticationProcessingFilterEntryPoint 
 
 当抛出AccessDeniedException时,将用户重定向到登录界面。属性loginFormUrl配置了一个登录表单的URL,当需要用户
登录时,authenticationProcessingFilterEntryPoint会将用户重定向到该URL

<bean id="authenticationProcessingFilterEntryPoint"
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
       
<property
name="loginFormUrl">
           
<value>/security/login.jsp</value>
       
</property>
        <property
name="forceHttps" value="false"/>
</bean>

2.2.3 HTTP安全请求

1) httpSessionContextIntegrationFilter
 
 每次request前
HttpSessionContextIntegrationFilter从Session中获取Authentication对象,在request完
后, 又把Authentication对象保存到Session中供下次request使用,此filter必须其他Acegi
filter前使用,使之能跨越多个请求。

<bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter"></bean>
    <bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
        <property name="allowIfAllAbstainDecisions" value="false"/>
        <property name="decisionVoters">
            <list>
                <ref bean="roleVoter"/>
            </list>
        </property>
</bean>

2) httpRequestAccessDecisionManager
 
 经过投票机制来决定是否可以访问某一资源(URL或方法)。allowIfAllAbstainDecisions为false时如果有一个或以上的
decisionVoters投票通过,则授权通过。可选的决策机制有ConsensusBased和UnanimousBased

    <bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
        <property name="allowIfAllAbstainDecisions" value="false"/>
        <property name="decisionVoters">
            <list>
                <ref bean="roleVoter"/>
            </list>
        </property>
    </bean>

3) roleVoter
   必须是以rolePrefix设定的value开头的权限才能进行投票,如AUTH_ , ROLE_

    <bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
        <property name="rolePrefix" value="AUTH_"/>
   </bean>

4)exceptionTranslationFilter
  异常转换过滤器,主要是处理AccessDeniedException和AuthenticationException,将给每个异常找到合适的"去向"

   <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
        <property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint"/>
    </bean>

5) authenticationProcessingFilter
  和servlet spec差不多,处理登陆请求.当身份验证成功时,AuthenticationProcessingFilter会在会话中放置一个Authentication对象,并且重定向到登录成功页面
         authenticationFailureUrl定义登陆失败时转向的页面
         defaultTargetUrl定义登陆成功时转向的页面
         filterProcessesUrl定义登陆请求的页面
         rememberMeServices用于在验证成功后添加cookie信息

    <bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="authenticationFailureUrl">
            <value>/security/login.jsp?login_error=1</value>
        </property>
        <property name="defaultTargetUrl">
            <value>/admin/index.jsp</value>
        </property>
        <property name="filterProcessesUrl">
            <value>/j_acegi_security_check</value>
        </property>
        <property name="rememberMeServices" ref="rememberMeServices"/>
    </bean>

6) filterInvocationInterceptor
 
 在执行转向url前检查objectDefinitionSource中设定的用户权限信息。首先,objectDefinitionSource中定
义了访问URL需要的属性信息(这里的属性信息仅仅是标志,告诉accessDecisionManager要用哪些voter来投票)。然
后,authenticationManager掉用自己的provider来对用户的认证信息进行校验。最后,有投票者根据用户持有认证和访问url需
要的属性,调用自己的voter来投票,决定是否允许访问。

    <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
        <property name="objectDefinitionSource" ref="filterDefinitionSource"/>
    </bean>

7) filterDefinitionSource (详见 2.6.3 资源权限定义扩展)
  自定义DBFilterInvocationDefinitionSource从数据库和cache中读取保护资源及其需要的访问权限信息

<bean id="filterDefinitionSource" class="org.springside.modules.security.service.acegi.DBFilterInvocationDefinitionSource">
        <property name="convertUrlToLowercaseBeforeComparison" value="true"/>
        <property name="useAntPath" value="true"/>
        <property name="acegiCacheManager" ref="acegiCacheManager"/>
</bean>

2.2.4 方法调用安全控制

(详见 2.6.3 资源权限定义扩展)

1) methodSecurityInterceptor
  在执行方法前进行拦截,检查用户权限信息
2) methodDefinitionSource
  自定义MethodDefinitionSource从cache中读取权限

   <bean id="methodSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
        <property name="objectDefinitionSource" ref="methodDefinitionSource"/>
    </bean>
    <bean id="methodDefinitionSource" class="org.springside.modules.security.service.acegi.DBMethodDefinitionSource">
        <property name="acegiCacheManager" ref="acegiCacheManager"/>
    </bean>

2.3 Jcaptcha验证码

采用 http://jcaptcha.sourceforge.net 作为通用的验证码方案,请参考SpringSide中的例子,或网上的:
http://www.coachthrasher.com/page/blog?entry=jcaptcha_with_appfuse

差沙在此过程中又发现acegi logout filter的错误,进行了修正。

另外它默认提供的图片比较难认,我们custom了一个美观一点的版本。

原文来自:http://www.cnblogs.com/dengjiali/articles/3344041.html

转:Acegi Security的更多相关文章

  1. acegi security实践教程—入门

    Acegi Security概念    Acegi Security是基于J2EE的企业软件应用提供全面的安全服务.通俗的说,就是封装的安全框架.提到安全,大家脑子中第一反应肯定是权限控制.的确如此, ...

  2. Apache Shiro和Spring Security的详细对比

    参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...

  3. Spring安全框架 Spring Security

    Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架. Spring Security  为基于J2EE企业应用软件提供了全面 ...

  4. Spring3之Security

    1.spring security介绍 Spring Security原来叫做Acegi Security,可用于加强任何Java应用的安全,但是最常用于基于Web的应用.下面首先理解几个安全术语: ...

  5. 使用 Acegi 保护 Java 应用程序

    第 1 部分: 架构概览和安全过滤器 Acegi Security System 是一种功能强大并易于使用的替代性方案,使您不必再为 Java 企业应用程序编写大量的安全代码.虽然它专门针对使用 Sp ...

  6. Spring Security 入门(1-1)Spring Security是什么?

    1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...

  7. Spring Security(五):2.2 History

    Spring Security began in late 2003 as "The Acegi Security System for Spring". A question w ...

  8. Acegi框架

    Acegi(Acegi Security)框架,是一个能为基于Spring的企业应用提供强大而灵活安全访问控制解决方案的框架,Acegi已经成为 Spring官方的一个子项目,所以也称为Spring ...

  9. spring security 学习文档

    web service Prepared by:   Sea                                                                       ...

随机推荐

  1. IOS学习笔记 O2

    第二章 Objective-C语言基础进阶 一.Objective-C语言写法简化 利用@property快速生成setget方法 1.首先来复习一下setget写法,这是上一节笔记写的setget方 ...

  2. web应用程序传递连接字符串给FastReport数据源

    public static FastReport.Report fr = new FastReport.Report(); public static FastReport.EnvironmentSe ...

  3. mysql 查询每个班级成绩前两名

  4. Java printf

  5. Unity IOC容器的简单应用(转)

    转自:http://blog.csdn.net/wanzhuan2010/article/details/7763280 Unity是Unity是微软patterns& practices组用 ...

  6. cognos制作报表流程

    第一章 创建报表 制作ReportStudio报表,步骤如下图所示: 第二章 添加查询 1.点击中间栏的查询资源管理器,然后点击查询,就可以新建查询.步骤如下图所示: 2.点击查询出现的页面: 3.在 ...

  7. Android 断点续传 思路

    大部分http服务器本身是可以支持range字段和断点续传的.另外 http返回206字段表示支持断点续传. 但是遇到支持的服务器的时候,就需要手动去处理断点续传的功能. 客户端在请求文件的时候添加 ...

  8. 用户图形界面(GUI)学习笔记(一)——Swing与AWT

    一.Swing与AWT 对象窗口工具箱(Abstact Window Toolkit,AWT),是JAVA1.0刚出现的时候,包含的一个GUI设计类库.它将处理用户界面元素的任务委派给每个目标平台(W ...

  9. Linux内核--网络栈实现分析(六)--应用层获取数据包(上)

    本文分析基于内核Linux 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7541907 更多请看专栏,地址http: ...

  10. ASP.NET 4.5.256 has not been registered on the Web server. You need to manually configure your Web server for ASP.NET 4.5.256 in order for your site to run correctly

    Microsoft .NET Framework 4.6安装后,用户可能会在使用Microsoft Visual Studio 创建(或打开现有项目时)网站.或Windows Azure项目时遇到下面 ...