acegi安全框架使用:第二部分
二、dbms实现鉴权
1.dbms鉴权,修改FilterSecurityInterceptor中的objectDefinitionSource属性,注入rdbmsFilterInvocationDefinitionSource,可加入Ehcahe提高性能
<!-- 权限过滤,基于URL的过滤器,使用RDBMS和Ehcache实现 -->
<bean id="securityInterceptor"
class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
<!-- 修改默认值,防止没有授权的角色导致的错误 -->
<property name="validateConfigAttributes" value="true" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager" ref="accessDecisionManager" />
<property name="objectDefinitionSource" ref="rdbmsFilterInvocationDefinitionSource" />
</bean>
<bean id="rdbmsFilterInvocationDefinitionSource" class="com.hengtian.security.RdbmsFilterInvocationDefinitionSource">
<property name="dataSource" ref="dataSource" />
<property name="webresdbCache" ref="webresCacheBackend" />
</bean>
<bean id="webresCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName">
<value>webresdbCache</value>
</property>
</bean>
RdbmsFilterInvocationDefinitionSource是objectDefinitionSource的dbms实现,这个要自己实现
2.aop实现方法过滤
如果不想对action,url等资源过滤,也可对调用方法过滤,实现特定角色只能实现特定方法。在applicationContext.xml中配置,注意这里是用BeanNameAutoProxyCreator实现aop的.
<bean id="serviceSecurityInterceptor"
class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
<property name="validateConfigAttributes" value="true" /> <property
name="authenticationManager" ref="authenticationManager" /> <property
name="accessDecisionManager" ref="accessDecisionManager" /> <property
name="objectDefinitionSource"> <value>
com.role.action.RoleAction.operateRole=ROLE_ADMIN </value> </property>
</bean>
<bean id="serviceSecurityInterceptor"
class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
<property name="validateConfigAttributes" value="true" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager" ref="accessDecisionManager" />
<property name="objectDefinitionSource" ref="rdbmsMethodDefinitionSource" />
</bean>
<bean id="rdbmsMethodDefinitionSource" class="com.hengtian.security.RdbmsMethodDefinitionSource">
<property name="dataSource" ref="dataSource" />
<property name="webresdbCache" ref="webresCacheBackend" />
</bean>
<bean id="webresCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName">
<value>webresdbCache</value>
</property>
</bean>
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>serviceSecurityInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>*Action</value>
</list>
</property>
</bean>
3.表结构设置
按照常规的权限模型设计:用户表、角色表、用户角色关系表、角色功能关系表;系统表、模块表、菜单表、按钮表、按钮资源表(在一个操作做个url的情况使用)
权限这块的表结构所有的系统都查不多,这里只是在功能表里面多加上了url字段。对外显示为特定角色有执行某模块、某菜单、界面中的某个按钮的权限,对内则是某个角色和action的映射关系供acegi鉴权使用