暂时想出了以下解决方案。由于我的项目相当简单,这可能不适用于更复杂的项目。
- 用户可以读取某个类的所有实体或不读取任何实体
因此任何查询方法都可以用@PreAuthorize包含注释hasRole。
例外是Container我项目中的实体。它可以包含任何子类,Compound并且用户可能无权查看所有子类。它们必须是过滤器。
为此,我创建了一个UserandRole实体。Compound具有 OneToOne 关系,Role并且该角色是 that 的“read_role” Compound。User并Role具有多对多关系。
@Entity
public abstract class Compound {
//...
@OneToOne
private Role readRole;
//...
}
我所有的存储库都实现QueryDSLPredicateExecutor了,这在这里变得非常有用。repositry.findAll(predicate)我们只在服务层创建它们并使用and ,而不是在存储库中创建自定义 findBy-methods repository.findOne(predicate)。谓词包含实际的用户输入+“安全过滤器”。
@PreAuthorize("hasRole('read_Container'")
public T getById(Long id) {
Predicate predicate = QCompoundContainer.compoundContainer.id.eq(id);
predicate = addSecurityFilter(predicate);
T container = getRepository().findOne(predicate);
return container;
}
private Predicate addSecurityFilter(Predicate predicate){
String userName = SecurityContextHolder.getContext().getAuthentication().getName();
predicate = QCompoundContainer.compoundContainer.compound.readRole
.users.any().username.eq(userName).and(predicate);
return predicate;
}
注意:QCompoundContainer是 QueryDSL 生成的“元模型”类。
最后,您可能需要将 QueryDSL 路径初始化Container为User:
@Entity
public abstract class CompoundContainer<T extends Compound>
//...
@QueryInit("readRole.users") // INITIALIZE QUERY PATH
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL,
targetEntity=Compound.class)
private T compound;
//...
}
省略这最后一步可能会导致NullPointerException.
进一步提示:CompoundService在保存时自动设置角色:
if (compound.getReadRole() == null) {
Role role = roleRepository.findByRoleName("read_" + getCompoundClassSimpleName());
if (role == null) {
role = new Role("read_" + getCompoundClassSimpleName());
role = roleRepository.save(role);
}
compound.setReadRole(role);
}
compound = getRepository().save(compound)
这行得通。缺点有点明显。这与同一类实现Role的每个实例相关联。Compound