`
陈谏辉
  • 浏览: 48410 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

acegi"安全控制"的设置-2

阅读更多

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了一个美观一点的版本。

分享到:
评论

相关推荐

    Acegi-spring安全框架

    Acegi能做什么 Acegi的体系结构 Acegi核心组件 典型的web认证过程 Acegi的登陆认证 Acegi对安全对象的访问控制 Filter 组件 Acegi的不足之处

    Acegi安全系统介绍.doc

    Acegi安全系统介绍 1 1.1 介绍 1 1.2 管理身份验证 3 1.3 控制访问 20 1.4 保护Web应用程序 24 1.5 保护方法调用 29 1.6 领域对象安全(Domain Object Security) 31

    Spring Acegi权限控制

    Spring Acegi权限控制,安全系统就只包括两个问题: 认证和授权.

    Acegi安全系统详解

    Acegi是Spring Framework 下最成熟的安全系统,它提供了强大灵活的企业级安全服务,如: 1 : 完善的认证和授权机制, 2 : Http资源访问控制, 3 : Method调用访问控制, 4 : Access Control List (ACL) 基于对象...

    Acegi安全系统详解.doc

    Acegi安全系统详解,介绍安全认证框架,用户认证,权限控制等

    Acegi安全系统介绍(一)

    Acegi是SpringFramework下最成熟的安全系统,它提供了强大灵活的企业级安全服务,如:完善的认证和授权机制,Http资源访问控制,Method调用访问控制,AccessControlList(ACL)基于对象实例的访问控制,...

    Acegi + Spring + Hibernate + Struts2搭建

    Acegi是一个可插拔框架,主要是在SSH框架上插入Acegi安全框架。

    acegi的使用

    好资料,大家一起分享,用户javaweb开发的权限和安全控制

    关于web spring acegi 权限配置xml

    如何定义spring security的安全认证框架,对url和系统类method进行过滤以及权限分配和控制

    Acegi安全框架下实现的通用权限管理系统 (2010年)

    把Acegi安全框架引入到SSH(表现层+控制层+持久层)架构中,对Acegi安全框架进行配置动态扩展,实现一个通用权限管理子系统畅对可能出现的问题进行分析,分别给出相应的解决方案。把子系统应用到一个账单管理系统中,其...

    Acegi 数据库配置安全策略 源代码及图解

    注:此为王政 所作,个人在网上淘到,与各位 做用户管理的朋友分享!内包含 转换XML 配置策略到数据库动态配置安全策略,及一些UML图解

    Acegi配置指南

    Acegi是基于Spring Web应用的安全框架,即是“声明式”的访问安全控制的解决方案,用户基本上不需编写代码而仅通过配置就可以实施应用系统的安全。

    基于Acegi的Web系统安全架构的设计与实现 (2011年)

    分析了Acegi安全框架,讨论了Acegi安全访问控制粒度问题,以及认证与授权过程.通过应用Aeegi设计实现了一个实际网站的安全系统,并验证了Acegi框架对保护Web应用程序安全、有效、可行.

    Acegi (和spring 的AOP关系)

    三大组件(安全控制组件,拦截器,资源安全管理)

    简单acegi程序

    在应用系统开发的过程中权限的设置和用户的安全管理是系统中不可缺少的部分,但如果自己去实现有时因为项目的原因可能就会在后期忽略了这块,在spring中有集成了这个部分,感谢spring 给我们带来了这么好的东西。

    Spring Security-3.0.1中文官方文档(翻译版)

    16. acegi 到spring security 的转换方式 16.1. Spring Security 是什么 16.2. 目标 16.3. 步骤 16.4. 总结 V. 高级话题 17. 领域对象安全(ACLs) 17.1. 概述 17.2. 关键概念 17.3. 开始 18. 预...

    基于Spring框架应用的权限控制系统的研究和实现

    本文探讨了Acegi安全框架中各部件之间的交互,并通过扩展Acegi数据库设计来实现基于Spring框架的应用的安全控制方法。 关键词Spring;Acegi;认证;授权1引言 近年来,随着Internet技术的迅猛发展,计算机网络已...

    关于web spring acegi 权限配置xml(补充)

    如何定义spring security的安全认证框架,对url和系统类method进行过滤以及权限分配和控制(补充) 关于DataBaseFilterInvocationDefinitionSource.java的实现

    Spring Security 中文教程.pdf

    16. acegi到spring security的转换方式 16.1. Spring Security是什么 16.2. 目标 16.3. 步骤 16.4. 总结 V. 高级话题 17. 领域对象安全(ACLs) 17.1. 概述 17.2. 关键概念 17.3. 开始 18. 预认证...

Global site tag (gtag.js) - Google Analytics