Acegi是非入侵式安全架构 因为 :
基于角色的权限控制(RBAC) :
Acegi 自带的 sample 表设计很简单: users表{username,password,enabled} authorities表{username,authority},这样简单的设计无法适应复杂的权限需求,故SpringSide选用RBAC模型对权限控制数据库表进行扩展。 RBAC(Role-Based Access Control)引入了ROLE的概念,使User(用户)和Permission(权限)分离,一个用户拥有多个角色,一个角色拥有有多个相应的权限,从而减少了权限管理的复杂度,可更灵活地支持安全策略。
同时,我们也引入了resource(资源)的概念,一个资源对应多个权限,资源分为ACL,URL,和FUNTION三种。注意,URL和FUNTION的权限命名需要以AUTH_开头才会有资格参加投票, 同样的ACL权限命名需要ACL_开头。
2.1 在Web.xml中的配置 :
1) FilterToBeanProxy
Acegi通过实现了Filter接口的FilterToBeanProxy提供一种特殊的使用Servlet Filter的方式,它委托Spring中的Bean -- FilterChainProxy来完成过滤功能,这好处是简化了web.xml的配置,并且充分利用了Spring IOC的优势。FilterChainProxy包含了处理认证过程的filter列表,每个filter都有各自的功能。
2) filter-mapping
<filter-mapping>限定了FilterToBeanProxy的URL匹配模式,
3) HttpSessionEventPublisher
<listener>的HttpSessionEventPublisher用于发布HttpSessionApplicationEvents和HttpSessionDestroyedEvent事件给spring的applicationcontext。
注:appfuse1.9.3中没有发现这个 监听器
--------------------------------------
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的匹配模式
从严格意义上来说,以下权限部分的介绍应该不在acegi security处理的范围之内,不过acegi security是提供了相应的机制的:
权限管理
权限在acegi security 主要以acl的概念出现:即 access control list
这个类实现中有acl的产生,获取和删除操作
应用数据权限的处理:
如果我们应用数据的权限要借助于acegi security 来实现的话,那主要工作就是调用 basicAclExtendedDao 中的相关方法。阅读basicAclExtendedDao即可明白。
以上简要的介绍了一下自己学习acegi security的一些了解。自己最后得出的结论是,如果自己的应用规模很小,完全可以不用acegi security。如果要用acegi security,很多时候是需要重新实现自己的权限和用户模型的。
引入别人教程完毕
1) authenticationManager
起到认证管理的作用,它将验证的功能委托给多个Provider,并通过遍历Providers, 以保证获取不同来源的身份认证,若某个Provider能成功确认当前用户的身份,authenticate()方法会返回一个完整的包含用户授权信息的Authentication对象,否则会抛出一个AuthenticationException。
Acegi提供了不同的AuthenticationProvider的实现,如:
每个认证者会对自己指定的证明信息进行认证,如DaoAuthenticationProvider仅对UsernamePasswordAuthenticationToken这个证明信息进行认证。
2) daoAuthenticationProvider
进行简单的基于数据库的身份验证。DaoAuthenticationProvider获取数据库中的账号密码并进行匹配,若成功则在通过用户身份的同时返回一个包含授权信息的Authentication对象,否则身份验证失败,抛出一个AuthenticatiionException。
3) passwordEncoder1 < bean id = " daoAuthenticationProvider " class = " org.acegisecurity.providers.dao.DaoAuthenticationProvider " >
2 < property name = " userDetailsService " ref = " jdbcDaoImpl " />
3 < property name = " userCache " ref = " userCache " />
4 < property name = " passwordEncoder " ref = " passwordEncoder " />
5 </ bean >
1 a : PlaintextPasswordEncoder—默认,不加密,返回明文.
2 b : ShaPasswordEncoder—哈希算法(SHA)加密
3 c : Md5PasswordEncoder—消息摘要(MD5)加密
1 < bean id = " passwordEncoder " class = " org.acegisecurity.providers.encoding.Md5PasswordEncoder " />
4) jdbcDaoImpl
用于在数据中获取用户信息。 acegi提供了用户及授权的表结构,但是您也可以自己来实现。通过usersByUsernameQuery这个SQL
得到你的(用户ID,密码,状态信息);通过authoritiesByUsernameQuery这个SQL得到你的(用户ID,授权信息)
1 < bean id = " jdbcDaoImpl " class = " org.acegisecurity.userdetails.jdbc.JdbcDaoImpl " >
2 < property name = " dataSource " ref = " dataSource " />
3 < property name = " usersByUsernameQuery " >
4 < value > select loginid,passwd, 1 from users where loginid = ?</ value >
5 </ property >
6 < property name = " authoritiesByUsernameQuery " >
7 < value >
8 select u.loginid,p.name from users u,roles r,permissions p,
9 user_role ur,role_permis rp where u.id = ur.user_id and
10 r.id = ur.role_id and p.id = rp.permis_id and r.id = rp.role_id and
11 p.status = ' 1 ' and u.loginid =?
12 </ value >
13 </ property >
14 </ bean >
5) userCache & resourceCache
缓存用户和资源相对应的权限信息。每当请求一个受保护资源时,daoAuthenticationProvider就会被调用以获取用户授权信息。如果每次都从数据库获取的话,那代价很高,对于不常改变的用户和资源信息来说,最好是把相关授权信息缓存起来。(详见 2.6.3 资源权限定义扩展 )
userCache提供了两种实现: NullUserCache和EhCacheBasedUserCache, NullUserCache实际上就是不进行任何缓存,EhCacheBasedUserCache是使用Ehcache来实现缓功能。
1 < bean id = " userCacheBackend " class = " org.springframework.cache.ehcache.EhCacheFactoryBean " >
2 < property name = " cacheManager " ref = " cacheManager " />
3 < property name = " cacheName " value = " userCache " />
4 </ bean >
5
6 < bean id = " userCache "
class = " org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache " autowire = " byName " >
7 < property name = " cache " ref = " userCacheBackend " />
8 </ bean >
9
10 < bean id = " resourceCacheBackend " class = " org.springframework.cache.ehcache.EhCacheFactoryBean " >
11 < property name = " cacheManager " ref = " cacheManager " />
12 < property name = " cacheName " value = " resourceCache " />
13 </ bean >
14
15 < bean id = " resourceCache "
class = " org.springside.modules.security.service.acegi.cache.ResourceCache " autowire = " byName " >
16 < property name = " cache " ref = " resourceCacheBackend " />
17 </ bean >
6) basicProcessingFilter
用于处理HTTP头的认证信息,如从Spring远程协议(如Hessian和Burlap)或普通的浏览器如IE,Navigator的HTTP头中获取用户
信息,将他们转交给通过authenticationManager属性装配的认证管理器。如果认证成功,会将一个Authentication对象放到会话中
,否则,如果认证失败,会将控制转交给认证入口点(通过authenticationEntryPoint属性装配)
1 < bean id = " basicProcessingFilter " class = " org.acegisecurity.ui.basicauth.BasicProcessingFilter " >
2 < property name = " authenticationManager " ref = " authenticationManager " />
3 < property name = " authenticationEntryPoint " ref = " basicProcessingFilterEntryPoint " />
4 </ bean >
7) basicProcessingFilterEntryPoint
通过向浏览器发送一个HTTP401(未授权)消息,提示用户登录。
处理基于HTTP的授权过程, 在当验证过程出现异常后的"去向",通常实现转向、在response里加入error信息等功能。
1 < bean id = " basicProcessingFilterEntryPoint "
2 class = " org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint " >
3 < property name = " realmName " value = " SpringSide Realm " />
4 </ bean >
8) authenticationProcessingFilterEntryPoint
当抛出AccessDeniedException时,将用户重定向到登录界面。属性loginFormUrl配置了一个登录表单的URL,当需要用户登录时,authenticationProcessingFilterEntryPoint会将用户重定向到该URL
1) httpSessionContextIntegrationFilter
每次request前 HttpSessionContextIntegrationFilter从Session中获取Authentication对象,在request完后, 又把Authentication对象保存到Session中供下次request使用,此filter必须其他Acegi filter前使用,使之能跨越多个请求。
2) httpRequestAccessDecisionManager
经过投票机制来决定是否可以访问某一资源(URL或方法)。allowIfAllAbstainDecisions为false时如果有一个或以上的decisionVoters投票通过,则授权通过。可选的决策机制有ConsensusBased和UnanimousBased
1 < bean id = " httpRequestAccessDecisionManager " class = " org.acegisecurity.vote.AffirmativeBased " >
2 < property name = " allowIfAllAbstainDecisions " value = " false " />
3 < property name = " decisionVoters " >
4 < list >
5 < ref bean = " roleVoter " />
6 </ list >
7 </ property >
8 </ bean >
3) roleVoter
必须是以rolePrefix设定的value开头的权限才能进行投票,如AUTH_ , ROLE_
1 < bean id = " roleVoter " class = " org.acegisecurity.vote.RoleVoter " >
2 < property name = " rolePrefix " value = " AUTH_ " />
3 </ bean >
4)exceptionTranslationFilter
异常转换过滤器,主要是处理AccessDeniedException和AuthenticationException,将给每个异常找到合适的"去向"
1 < bean id = " exceptionTranslationFilter " class = " org.acegisecurity.ui.ExceptionTranslationFilter " >
2 < property name = " authenticationEntryPoint " ref = " authenticationProcessingFilterEntryPoint " />
3 </ bean >
5) authenticationProcessingFilter
和servlet spec差不多,处理登陆请求.当身份验证成功时,AuthenticationProcessingFilter会在会话中放置一个Authentication对象,并且重定向到登录成功页面
authenticationFailureUrl定义登陆失败时转向的页面
defaultTargetUrl定义登陆成功时转向的页面
filterProcessesUrl定义登陆请求的页面
rememberMeServices用于在验证成功后添加cookie信息
1 < bean id = " authenticationProcessingFilter "
class = " org.acegisecurity.ui.webapp.AuthenticationProcessingFilter " >
2 < property name = " authenticationManager " ref = " authenticationManager " />
3 < property name = " authenticationFailureUrl " >
4 < value >/ security / login.jsp ? login_error = 1 </ value >
5 </ property >
6 < property name = " defaultTargetUrl " >
7 < value >/ admin / index.jsp </ value >
8 </ property >
9 < property name = " filterProcessesUrl " >
10 < value >/ j_acegi_security_check </ value >
11 </ property >
12 < property name = " rememberMeServices " ref = " rememberMeServices " />
13 </ bean >
6) filterInvocationInterceptor
在执行转向url前检查objectDefinitionSource中设定的用户权限信息。首先,objectDefinitionSource中定义了访问URL需要的属性信息(这里的属性信息仅仅是标志,告诉accessDecisionManager要用哪些voter来投票)。然后,authenticationManager掉用自己的provider来对用户的认证信息进行校验。最后,有投票者根据用户持有认证和访问url需要的属性,调用自己的voter来投票,决定是否允许访问。
1 < bean id = " filterInvocationInterceptor " class = " org.acegisecurity.intercept.web.FilterSecurityInterceptor " >
2 < property name = " authenticationManager " ref = " authenticationManager " />
3 < property name = " accessDecisionManager " ref = " httpRequestAccessDecisionManager " />
4 < property name = " objectDefinitionSource " ref = " filterDefinitionSource " />
5 </ bean >
7)filterDefinitionSource(详见 2.6.3 资源权限定义扩展)
自定义DBFilterInvocationDefinitionSource从数据库和cache中读取保护资源及其需要的访问权限信息
1 < bean id = " filterDefinitionSource "
2 class = " org.springside.modules.security.service.acegi.DBFilterInvocationDefinitionSource " >
3 < property name = " convertUrlToLowercaseBeforeComparison " value = " true " />
4 < property name = " useAntPath " value = " true " />
5 < property name = " acegiCacheManager " ref = " acegiCacheManager " />
6 </ bean >2.2.4 方法调用安全控制
(详见 2.6.3 资源权限定义扩展)
1) methodSecurityInterceptor
在执行方法前进行拦截,检查用户权限信息1 < bean id = " methodSecurityInterceptor "
2 class = " org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor " >
3 < property name = " authenticationManager " ref = " authenticationManager " />
4 < property name = " accessDecisionManager " ref = " httpRequestAccessDecisionManager " />
5 < property name = " objectDefinitionSource " ref = " methodDefinitionSource " />
6 </ bean >
2) methodDefinitionSource
自定义MethodDefinitionSource从cache中读取权限1 < bean id = " methodDefinitionSource "
2 class = " org.springside.modules.security.service.acegi.DBMethodDefinitionSource " >
3 < property name = " acegiCacheManager " ref = " acegiCacheManager " />
4 </ bean >
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1503485