Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7604965 建立时间:2006年5月29日 |

| |
[Hibernate]Acegi+hibernate 动态实现基于角色的权限管理(1) 软件技术, 电脑与网络
lhwork 发表于 2006/6/13 11:31:01 |
最近在做项目遇到了权限管理,用户要求可以自己建立不同的角色对系统的资源进行控制,
不同的用户有不同的角色,又恰恰框架中用到了struts+spring+hibernate,要求在web层调用 业务逻辑层
时不考虑权限,web层可以控制用户的显示界面,逻辑层处理用户权限问题。 想来想去好像只有spring 的aop 可以做到,在调用到
接口
中的方法时,首先检查用户的权限,如果检查通过则继续执行,否则抛出异常。但是新的问题又出现了,如何在逻辑层上来得到当前用户的id,以致用户的
角色,总不能每次都要从web中传来一个 httprequest,或者 session
这类的吧。在网上看了很多资料,发现了acegi,恰好解决了以上的难题,具体的实现原理这里就不多说了,网上有很多相关资料。 说正题,首先来看看acegi 的官方 example ,我下载的是acegi-security-1.0.0-RC1,解压缩后可以看到acegi-security-sample-contacts-filter.war,打开配置文件有这样几句
1 <bean id="contactManagerSecurity" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"> 2 <property name="authenticationManager"><ref bean="authenticationManager"/></property> 3 <property name="accessDecisionManager"><ref local="businessAccessDecisionManager"/></property> 4 <property name="afterInvocationManager"><ref local="afterInvocationManager"/></property> 5 <property name="objectDefinitionSource"> 6 <value> 7 sample.contact.ContactManager.create=ROLE_USER 8 sample.contact.ContactManager.getAllRecipients=ROLE_USER 9 sample.contact.ContactManager.getAll=ROLE_USER,AFTER_ACL_COLLECTION_READ 10 sample.contact.ContactManager.getById=ROLE_USER,AFTER_ACL_READ 11 sample.contact.ContactManager.delete=ACL_CONTACT_DELETE 12 sample.contact.ContactManager.deletePermission=ACL_CONTACT_ADMIN 13 sample.contact.ContactManager.addPermission=ACL_CONTACT_ADMIN 14 </value> 15 </property> 16 </bean> 17 可以看到它是通过读配置文件来判断执行某个方法所需要的角色的,再看这几句
500)this.width=500'><bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"> 500)this.width=500'> <property name="authenticationManager"><ref bean="authenticationManager"/></property> 500)this.width=500'> <property name="accessDecisionManager"><ref local="httpRequestAccessDecisionManager"/></property> 500)this.width=500'> <property name="objectDefinitionSource"> 500)this.width=500'> <value> 500)this.width=500'> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON 500)this.width=500'> PATTERN_TYPE_APACHE_ANT 500)this.width=500'> /index.jsp=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'> /hello.htm=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'> /logoff.jsp=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'> /switchuser.jsp=ROLE_SUPERVISOR 500)this.width=500'> /j_acegi_switch_user=ROLE_SUPERVISOR 500)this.width=500'> /acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'> /**=ROLE_USER 500)this.width=500'> </value> 500)this.width=500'> </property> 500)this.width=500'> </bean> 500)this.width=500'>同样是将页面的访问权限写死在配置文件中,再来看看它的tag是如何处理的
500)this.width=500'><auth:authorize ifAnyGranted="ROLE_DELETE"> 500)this.width=500'> <a href="">删除</a> 500)this.width=500'></auth:authorize> 可见它是要求我们对链接或者其他资源的保护时提供 用户角色,可是既然角色是用户自己添加的我们又如何来写死在这里呢? 还有就是它对用户验证默认使用的是jdbc,即 JdbcDaoImpl
500)this.width=500'><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 500)this.width=500'> <property name="dataSource"><ref local="dataSource"/></property> 500)this.width=500'> </bean> 而我们希望基于Hibernate的Dao来实现。 可见仅仅使用现有的acegi 是 无法满足我们项目开发的需求的。 |
|
|