我正在尝试编写一个通用的SecurePagingAndSorting存储库,该存储库将检查CRUD操作的安全性,以节省在所有JPA存储库中重复相同的PreAuthorize(具有不同权限)的操作。

下面是一个简单的示例:

@NoRepositoryBean
public interface SecuredPagingAndSortingRepository<T, ID extends Serializable>     extends PagingAndSortingRepository<T, ID> {

@Override
@PreAuthorize("hasPermission(#id, domainType, 'delete'))
 void delete(ID id);

}


现在是问题所在的是domainType参数,因为这是一个通用接口,所以不能进行硬编码。从SecurePagingRepository派生的存储库中获取域类型的最佳方法是什么?

最佳答案

我看到的最好的解决方案是编写自己的接口PermissionEvaluator的实现,然后将其注入安全性上下文中以替换默认的实现。

如果您尝试这种方式,扩展类AclPermissionEvaluator将为您节省许多已经由Spring管理的代码,并确保向后兼容。

07-24 15:51