我可以使用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