SpringBoot aop 注解 数据权限校验
2024-09-25 08:40:03
注解类:
@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();
}
}
}
}
}
使用:
最新文章
- mysql基础知识扫盲
- Linux 下复制(cp)目录时排除一个或者多个目录的方法
- swiper 内容超出纵向滚动 解决办法
- 【转载】JMeter学习(一)工具简单介绍
- 网站性能Web压力测试工具webbench
- java.util.concurrent包
- BZOJ 3159决战
- apiCloud结合layer实现动态数据弹出层
- 关于VS2010error RC2170 : bitmap file res\tmp1.bmp is not in 3.00 format
- chmod u+x ./j2sdk-1_4_2_04-linux-i586.bin的含义
- adb 启动失败的原因和修改adb端口号
- Windows系统创建硬链接文件
- 检测客户端显示器分辨率、浏览器类型和客户端IP
- Linux下安装memcached图文版
- C#获取友好时间差
- Redis入门---字符串类型
- 1、在Centos上安装Grafana
- Ubuntu apt-get和pip国内源更换
- modsign: could&#39;t get uefi db list
- NOIP2017提高组Day2T3 列队 洛谷P3960 线段树
热门文章
- 【论文阅读】Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks
- 【软件安装与环境配置】ubuntu16.04+caffe+nvidia+CUDA+cuDNN安装配置
- 阿里云ecs使用补充说明
- js 数字随机滚动(数字递增) 每日凌晨回到原点,重新计算
- 整合spring+springmvc+mybatis
- classic code review
- uva 202
- 2019OO第一单元作业总结
- ubuntu 终端作死体验
- Python基础:七、注释