zoukankan      html  css  js  c++  java
  • aop 使用

     定义注解
    
    @Retention(RetentionPolicy.RUNTIME)//运行时有效
    @Target(ElementType.METHOD)//作用于方法
    public @interface PermissionLog {
    // 方法名
       String openId() default "";
    
       String tournamentId() default "";
    
       String applicationType() default "";
    
       boolean isEntity() default true;
    
    }
    定义传实体需要的对象
    @Data
    @ApiModel(value = "权限管理VO")
    public class AopPermissionLogVO<T> {
    
       @ApiModelProperty(value = "数据实体")
       private T entity;
       @ApiModelProperty(value = "登录人openID")
       private String openId;
       @ApiModelProperty(value = "赛事ID")
       private String tournamentId;
    
    
    }
    定义解析注解的切点
    @Aspect
    @Component
    public class AnnotationAspect {
    
       @Autowired
       private ICompetitionClient competitionClient;
    
       @Around(value = "@annotation(around)")
       @ResponseBody
       public Object processAuthority(ProceedingJoinPoint point, PermissionLog around) throws Throwable{
          if (true){return point.proceed();}
          int count =0;
          if (around.isEntity()){
             AopPermissionLogVO arg = (AopPermissionLogVO) point.getArgs()[0];
             count = competitionClient.getPermissionLogCount(arg.getOpenId(),arg.getTournamentId(),around.applicationType());
          }else {
             MethodSignature signature = (MethodSignature) point.getSignature();
             String fieldName;
             Map<String,String> fieldValues = new HashMap(16);
             String[] all = new String[]{around.openId(),around.tournamentId(),around.applicationType()};
             for (String str:all){
                if(!StringUtils.isBlank(fieldName = str)){
                   String[] parameterNames = signature.getParameterNames();
                   for (int i = 0; i < parameterNames.length; i++) {
                      String parameterName = parameterNames[i];
                      if (fieldName.equals(parameterName)) {
                         fieldValues.put(fieldName, String.valueOf(point.getArgs()[i]));
                      }
                   }
                }
             }
             count = competitionClient.getPermissionLogCount(fieldValues.get(around.openId()),fieldValues.get(around.tournamentId()), around.applicationType());
          }
          if (count>0){
             return point.proceed();
          }else {
             return R.fail("没有权限!");
          }
    
       }
    }
  • 相关阅读:
    【排序】快速排序代码实现及优化
    【SpringMVC】重定向和转发
    RESTful风格
    【SpringMVC】用demo了解执行流程(xml配置)
    【Spring】声明式事务aop
    【Spring】整合Mybatis两种方式
    MongoDB语法与现有关系型数据库SQL语法比较
    Oracle查看哪些表被锁住了
    Mongodb分布式集群搭建
    四大MQ比较及MQ详解
  • 原文地址:https://www.cnblogs.com/-mzh/p/12801961.html
Copyright © 2011-2022 走看看