当面、以下の解決策が出ました。私のプロジェクトはかなり単純なので、これはもっと複雑なプロジェクトではうまくいかないかもしれません。
- ユーザは、すべてのまたは特定のクラスのエンティティしたがって
のいずれも任意のクエリ方法はhasRole
を含む@PreAuthorize
で注釈することができる読み取ることができます。
例外は、私のプロジェクトのContainer
エンティティです。 Compound
のサブクラスを含めることができ、ユーザーはそれらのすべてを表示する特権を持たない可能性があります。フィルターにする必要があります。
私はUser
とRole
エンティティを作成しました。 Compound
は、Role
へのOneToOneの関係を持ち、その役割は、そのCompound
の "read_role"です。 User
とRole
にはManyToMany関係があります。
@Entity
public abstract class Compound {
//...
@OneToOne
private Role readRole;
//...
}
すべての私のリポジトリはQueryDSLPredicateExecutor
を実装し、それはここでは非常に手になります。リポジトリにカスタムのfindByメソッドを作成するのではなく、サービスレイヤでのみ作成し、repositry.findAll(predicate)
と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によって生成される「メタモデル」クラスです。
で最後のあなたはおそらくContainer
からUser
にQueryDSLパスを初期化する必要があります。
@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
につながることができます。
さらにヒント:
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)
これは動作します:CompoundService
は自動的に保存上の役割を設定します。欠点は少し明らかです。同じRole
は、同じCompound
クラス実装のすべての単一インスタンスに関連付けられています。
質問については、[User Schema](http://static.springsource.org/spring-security/site/docs/3.2.x/reference/springsecurity-single.html#d0e8380)セクションを参照してください。 –
現在のユーザーの役割を考慮してクエリを調整することができます(例:特定のエンティティからロールへの関係とロールからユーザーへの関係が必要です。 –
次に、ACL http://static.springsource.org/spring-security/site/docs/3.2.x/reference/springsecurity-single.html#domain-aclsと適切なDBスキーマhttp:// staticを見てください。 springsource.org/spring-security/site/docs/3.2.x/reference/springsecurity-single.html#dbschema-acl –