2

在我的项目中,我使用 Spring-Data、Spring-Data-Rest 和 Spring-Security。

我需要完成的是在这些存储库上实现域对象安全 (ACL)。特别@PostFilterPageable.findAll()方法。

方法级别的安全性很容易实现,如此所述。

文档中还有一个关于在@Query 此处使用安全表达式的部分。

但是虽然我也可以hasPermission(..)在里面使用方法@Query,但是没有办法在这个方法中包含对象(SQL 行)——具体来说是这样做的:

@Query("select u from #{#entityName} u where 1 = ?#{security.hasPermission(u, 'read') ? 1 : 0}")

现在我明白这与像这样修改查询预执行不同:

@Query("select m from Message m where m.to.id = ?#{ principal?.id }")

我还发现了以下 jira 问题: https ://jira.spring.io/browse/DATACMNS-293 我怀疑一旦它得到解决,就会有解决方案,但似乎不会很快。

我仍然需要实现这个功能,为此我想得到你的意见和可能的解决方案的指示。

现在我正在考虑创建我的自定义注释,它将模仿@PostFilter一个并使用相同的语法,但将在我自己的 BaseRepositoryImplementation 中手动调用。在那里,我将从 type 和 获取存储库接口Repositories#getRepositoryInformationFor(type)#getRepositoryInterface(),找到相应方法的注释并手动调用安全检查。

您是否有不同的解决方案,或者对我提出的解决方案有一些说明?

另外,您是否知道提到的 jira 问题是否有任何时间表?

4

1 回答 1

0

一种轻量级的方法是使用 hasPermission() 方法并在控制器级别实现您自己的“权限评估器”,如果您愿意的话。

@PreAuthorize("hasPermission(#employee, 'edit')")
public void editEmployee(Employee employee) {
   ...
}

@Component
public class PermissionEvaluatorImpl implements PermissionEvaluator {
    @Override
    public boolean hasPermission(Authentication auth,
            Object targetDomainObject, Object permission) {
        // return true if "auth" has "permission" permission for the user.
        // Current-user can be obtained from auth.
    }
    ...
}

这在这里有更详细的描述:http: //www.naturalprogrammer.com/spring-domain-object-security-logged-in-user/

于 2017-09-11T02:42:02.293 回答