« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
| 公告 |
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。 |
Blog信息 |
blog名称: 日志总数:210 评论数量:205 留言数量:-19 访问次数:923795 建立时间:2007年5月10日 |

| |
[acegi权限认证]Acegi+hibernate 动态实现基于角色的权限管理(1) 文章收藏, 网上资源, 软件技术, 电脑与网络
李小白 发表于 2007/7/19 16:42:58 |
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'>500)this.width=500" align=top border=0><bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"> 500)this.width=500'>500)this.width=500" align=top border=0> <property name="authenticationManager"><ref bean="authenticationManager"/></property> 500)this.width=500'>500)this.width=500" align=top border=0> <property name="accessDecisionManager"><ref local="httpRequestAccessDecisionManager"/></property> 500)this.width=500'>500)this.width=500" align=top border=0> <property name="objectDefinitionSource"> 500)this.width=500'>500)this.width=500" align=top border=0> <value> 500)this.width=500'>500)this.width=500" align=top border=0> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON 500)this.width=500'>500)this.width=500" align=top border=0> PATTERN_TYPE_APACHE_ANT 500)this.width=500'>500)this.width=500" align=top border=0> /index.jsp=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'>500)this.width=500" align=top border=0> /hello.htm=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'>500)this.width=500" align=top border=0> /logoff.jsp=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'>500)this.width=500" align=top border=0> /switchuser.jsp=ROLE_SUPERVISOR 500)this.width=500'>500)this.width=500" align=top border=0> /j_acegi_switch_user=ROLE_SUPERVISOR 500)this.width=500'>500)this.width=500" align=top border=0> /acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER 500)this.width=500'>500)this.width=500" align=top border=0> /**=ROLE_USER 500)this.width=500'>500)this.width=500" align=top border=0> </value> 500)this.width=500'>500)this.width=500" align=top border=0> </property> 500)this.width=500'>500)this.width=500" align=top border=0> </bean> 500)this.width=500'>500)this.width=500" align=top border=0>同样是将页面的访问权限写死在配置文件中,再来看看它的tag是如何处理的
500)this.width=500'>500)this.width=500" align=top border=0><auth:authorize ifAnyGranted="ROLE_DELETE"> 500)this.width=500'>500)this.width=500" align=top border=0> <a href="">删除</a> 500)this.width=500'>500)this.width=500" align=top border=0></auth:authorize> 可见它是要求我们对链接或者其他资源的保护时提供 用户角色,可是既然角色是用户自己添加的我们又如何来写死在这里呢? 还有就是它对用户验证默认使用的是jdbc,即 JdbcDaoImpl
500)this.width=500'>500)this.width=500" align=top border=0><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 500)this.width=500'>500)this.width=500" align=top border=0> <property name="dataSource"><ref local="dataSource"/></property> 500)this.width=500'>500)this.width=500" align=top border=0> </bean> 而我们希望基于Hibernate的Dao来实现。 可见仅仅使用现有的acegi 是 无法满足我们项目开发的需求的。 |
|
|