通过注解@做细粒度权限控制

先定义一个注解

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationLimit {
    String mid();
    String pid();
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
        Method method = TestExc.class.getDeclaredMethod("hehe", String.class);
        if(method.isAnnotationPresent(AnnotationLimit.class)){
            AnnotationLimit an = method.getAnnotation(AnnotationLimit.class);
            System.out.println(an.mid()+"#" + an.pid());
        }else{
            System.out.println("no limit");
        }
    }
    
    @AnnotationLimit(mid="1", pid="2")
    static void hehe(String s){
        System.out.println("nnn");
    }

粗粒度权限控制,一般是通过session进行,session的话因为用户登录后修改数据库状态后,用户不能通过数据库实时查询进行控制。

通过注解可以得到配置信息再结合用户id进行实时查询看是否能访问url所对应的方法达到细粒度权限控制。

posted on 2016-04-14 12:40  颓废的悠然  阅读(237)  评论(0编辑  收藏  举报

导航