0

プロジェクトでは MyBatis 3.0.3 と Spring 3.0.5 を使用しています。

where句を格納するテーブルを介してデータレベルのセキュリティを実装しようとしています(userid < 200、active == true、...)。

そのため、実行時に選択クエリに動的に句を追加したい場合に問題が発生します。

Spring AOP を使用する可能性について考えていますが、それが可能かどうか、それが良い解決策であるかどうか、また実装方法がわかりません。

あなたの考えを共有してください。

前もって感謝します、

シルビア。

4

1 に答える 1

0

これは可能だと思います。サービス層を実装して、セキュリティ コンテキスト (Spring セキュリティ) を介してユーザー (userID) を取得できるようにするか、サービス層メソッドのパラメーターとしてユーザーを与えることもできます。

可能なアスペクト実装の例:

@Aspect
public class MyAspect {

   @Pointcut("within(my.package.MyClass)")
   public void pointcutClass() {
   }

   @Pointcut("execution(* create*(..))")
   public void pointcutMethod() {
   }

   @Pointcut("pointcutClass() && pointcutMethod()")
   public void myPointcut() {
   }

   @Before("myPointcut()")
   public void checkAuthorization(final JoinPoint joinPoint) {

    // Fetch User via spring security:
     Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     if (principal == null)
        throw new Exception("No principal found");
     if (!(principal instanceof User)) {
        throw new Exception("principal is not a User");
     }
     User user = (User) principal;

     // Or fetch User via the joinPoint:
     Object[] args = joinPoint.getArgs();
     User user = (User) args[0]

     if (user.getUserId == null || user.getUserId > 200)
        throw new Exception("User is not authorised.");
}
于 2012-02-13T15:04:32.967 に答える