1つのオプションは、カスタムのSpringセキュリティ PermissionEvaluator
を作成し、 hasPermission(認証認証、オブジェクトtargetDomainObject、オブジェクトアクセス許可)
メソッドで独自のチェックを実装することです。
保護するメソッドのシグネチャは次のようになります。
@PreAuthorize("hasRole('ROLE_USER') and hasPermission(#_dept, 'deptAndSubs')")
public String methodToProtect(String _dept)throws Exception {
;
}
hasPermission
式の最初の引数は、ユーザーが変更したい部署で、2番目はアクセス許可です。私たちのために、deptAndSubsアクセス権は、変更される部門が、ユーザーが割り当てた部門またはその部門のいずれかのサブ部門(他の権限は 'deptOnly'および 'subsOnly')と等しい場合にのみ、ユーザーがメソッドを実行できることを示します。
アプリケーションでは、Springがメソッドに渡すAuthenticationオブジェクトから直接ログインしているユーザーの部門を取得できるように、ユーザー部門コードを含むカスタムSpring Security UserDetails
オブジェクトがあります。カスタムエバリュエーターが最終的に次のようになります。
public class CustomPermissionEvaluator implements PermissionEvaluator {
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
AppUser appUser = (AppUser)authentication.getPrincipal();
if(permission instanceof String){
if("deptAndSubs".equals(permission)){
return isTargetDeptInUserDeptTree((String)targetDomainObject, appUser.getDeptCode());
}else if(.... other permission checks){}
}
return false;
}
メソッドisTargetDeptInUserDeptTreeは、ユーザーの部門ツリーを抽出し、対象部門がその部門内にあることを確認するためのカスタムコードです。
最後に、xml設定を設定する必要があります。
がんばろう!