我可以使用Spring Security ACL与实体权限,但我想知道如何测试如果用户有访问“创造”(第2位)上一类许可。Spring Security的ACL - 创建权限
喜欢的东西:
aclPermissionEvaluator.hasPermission(auth, clazz, "create")
有人能帮助我吗?
在此先感谢
我可以使用Spring Security ACL与实体权限,但我想知道如何测试如果用户有访问“创造”(第2位)上一类许可。Spring Security的ACL - 创建权限
喜欢的东西:
aclPermissionEvaluator.hasPermission(auth, clazz, "create")
有人能帮助我吗?
在此先感谢
您可以使用Spring的SpEL注释,例如, @PreAuthorize
,并覆盖PermissionEvaluator
接口的hasPermission
方法。如果你正在使用逐许可口罩,以及用户的权限(为int
)评估为“15”(1111
),并为对象所需要的权限是“6”(0110
),你可以这样做如下:
public boolean hasPermission(Authentication auth, Object targetObject, Object requiredPermissions) {
int permissionMask = MyUserClass.getMask();
int permissionsRequired = Integer.valueOf(requiredPermissions.toString());
return ((permissionMask | requiredPermissions) == permissionMask);
}
只要对象的权限掩码中活动的位在用户权限上处于活动状态,就会返回true。然后,你需要在你的security.xml
文件中声明该自定义权限评估:
<security:global-method-security pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler"/>
</security:global-method-security>
<bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="permissionEvaluator"/>
</bean>
<bean id="permissionEvaluator" class="my.project.package.CustomPermissionEvaluator"/>
现在,只要您拨打hasPermission()
,您的自定义计算器将处理请求。很明显,你可以使用任何你喜欢的逻辑来评估权限 - 只要确保返回类型为boolean
,并且要传递的参数与你发送的数据(或评估对象;注意格式异常)相匹配。
请注意,您的自定义参数必须为Object
覆盖hasPermission()
通过;您也可以通过更改签名来处理任何你喜欢的参数类型(如字符串或INT)重载方法,编译器应选择最具体的签名。但是,由于您正在实现PermissionEvaluator接口,因此无论如何您都必须包含给定的签名(Authentication,Object,Object),因此除非您有特定的需要编写重载方法,否则您也可以重写。
嗨@cabbagery,感谢您的答案,但它仍然没有寻找特定类别的许可... – Fiftoine 2013-04-25 09:46:31
您是在谈论类级注释驱动的访问,还是您在谈论ACL中的对象'acl_class'表)?另外,如果你还没有搜索过,请查看[Mark Serrano的ACL教程](http://krams915.blogspot.com/2011/01/spring-security-3-full-acl-tutorial.html)。我已经使用了他的几个教程与其他资源和我自己的一些实验,并发现它非常有帮助。 – cabbagery 2013-04-25 21:12:21