2011-07-12 9 views
0

私たちのプロジェクトでMyBatis 3.0.3 & Spring 3.0.5を使用しています。データベースからwhere句を追加する

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

したがって、実行時に選択クエリに句を追加するときに問題が発生します。

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

あなたの考えをお伝えください。

ありがとうございます。

シルビア。

答えて

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."); 
} 
関連する問題