注解类:

@Retention(RetentionPolicy.RUNTIME)
public @interface DataAuthValid
{
//位置
public int index() default 0; //字段 id
//public String id() default "id"; //字段 id
public String orgId() default "org_id"; //mapper
@SuppressWarnings("rawtypes")
public Class<? extends Mapper> mapper();
}

AOP切面:

@Aspect
@Component
@Order(1)
public class DataAuthAop { private static String types = "java.lang.String,java.lang.Long,long"; @Before("@annotation(dataAuth)")
public void beforeMethod(JoinPoint point,DataAuthValid dataAuth) throws Exception { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Map<String, Object> payloadMap = (Map<String, Object>) request.getAttribute("payloadMap");
Long companyid = Long.parseLong(payloadMap.get("companyid")+"");
if(companyid != 1) {
Object[] args = point.getArgs();
Object obj = args[dataAuth.index()];
String ids = null;
String typeName = obj.getClass().getTypeName();
if(types.contains(typeName)) {
ids = obj + "";
}else {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
if("id".equals(f.getName())) {
Long id = (Long) f.get(obj);
ids = id + "";
}
}
}
String[] idArr = ids.split(",");
for (String id : idArr) {
Class cla = dataAuth.mapper();
Mapper mapper = (Mapper) SpringBeanFactoryUtils.getApplicationContext().getBean(cla);
Object object = mapper.selectByPrimaryKey(Long.valueOf(id));
Field field = obj.getClass().getDeclaredField(dataAuth.orgId());
field.setAccessible(true);
Long orgId = (Long)field.get(obj);
if(!companyid.equals(orgId)) {
throw new RuntimeException();
}
}
}
}
}

使用:

最新文章

  1. mysql基础知识扫盲
  2. Linux 下复制(cp)目录时排除一个或者多个目录的方法
  3. swiper 内容超出纵向滚动 解决办法
  4. 【转载】JMeter学习(一)工具简单介绍
  5. 网站性能Web压力测试工具webbench
  6. java.util.concurrent包
  7. BZOJ 3159决战
  8. apiCloud结合layer实现动态数据弹出层
  9. 关于VS2010error RC2170 : bitmap file res\tmp1.bmp is not in 3.00 format
  10. chmod u+x ./j2sdk-1_4_2_04-linux-i586.bin的含义
  11. adb 启动失败的原因和修改adb端口号
  12. Windows系统创建硬链接文件
  13. 检测客户端显示器分辨率、浏览器类型和客户端IP
  14. Linux下安装memcached图文版
  15. C#获取友好时间差
  16. Redis入门---字符串类型
  17. 1、在Centos上安装Grafana
  18. Ubuntu apt-get和pip国内源更换
  19. modsign: could&#39;t get uefi db list
  20. NOIP2017提高组Day2T3 列队 洛谷P3960 线段树

热门文章

  1. 【论文阅读】Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks
  2. 【软件安装与环境配置】ubuntu16.04+caffe+nvidia+CUDA+cuDNN安装配置
  3. 阿里云ecs使用补充说明
  4. js 数字随机滚动(数字递增) 每日凌晨回到原点,重新计算
  5. 整合spring+springmvc+mybatis
  6. classic code review
  7. uva 202
  8. 2019OO第一单元作业总结
  9. ubuntu 终端作死体验
  10. Python基础:七、注释