注解类:

@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();
}
}
}
}
}

使用:

SpringBoot aop 注解 数据权限校验的更多相关文章

  1. Spring AOP 实现功能权限校验功能

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...

  2. springboot通过AOP和自定义注解实现权限校验

    自定义注解 PermissionCheck: package com.mgdd.sys.annotation; import java.lang.annotation.*; /** * @author ...

  3. SpringBoot —— AOP注解式拦截与方法规则拦截

    AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. SpringBoot中AOP的使用 ...

  4. SpringBoot AOP注解式拦截与方法规则拦截

    AOP的本质还是动态代理对方法调用进行增强. SpringBoot 提供了方便的注解实现自定义切面Aspect. 1.使用需要了解的几个概念: 切面.@Aspect 切点.@Pointcut. 通知. ...

  5. 基于Spring Aop实现类似shiro的简单权限校验功能

    在我们的web开发过程中,经常需要用到功能权限校验,验证用户是否有某个角色或者权限,目前有很多框架,如Shiro Shiro有基于自定义登录界面的版本,也有基于CAS登录的版本,目前我们的系统是基于C ...

  6. SpringSecurity权限管理系统实战—九、数据权限的配置

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  7. Salesforce的数据权限机制

    本文主要介绍了 Salesforce 对于系统中数据的访问控制是如何设计的,然后也了解了下 Alfresco 和 Oracle VPD 的数据权限机制.希望对一些业务系统的数据权限的访问控制设计能有所 ...

  8. 使用AOP+自定义注解完成spring boot的接口权限校验

    记使用AOP+自定义注解完成接口的权限校验,代码如下: pom文件添加所需依赖: 1 <dependency> 2 <groupId>org.aspectj</group ...

  9. Springboot学习06-Spring AOP封装接口自定义校验

    Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...

随机推荐

  1. VS2008安装“Visual Studio Web 创作组件”安装失败的解决方法

    VS2008安装“Visual Studio Web 创作组件”安装失败的解决方法 今天在单位电脑安装VS2008,当安装到“Visual Studio Web 创作组件”时出现错误. 准备手动安装 ...

  2. 多个DispatcherServlet有什么用处.md

    有看到一些应用在 web.xml 文件中配置了多个 DispatcherServlet,例如: <servlet>    <servlet-name>en</servle ...

  3. java高级进阶

  4. CSS背景样式和列表样式

    background-color 设置元素的背景颜色 background-image 把图像设置为背景 background-position 设置背景图像的起始位置 background-atta ...

  5. python 12

    #! /usr/bin/python a = 1 b = [2, 3] def func(): a = 2 print("in func a:", a) b[0] = 1 prin ...

  6. vue 项目搭建步骤

    环境搭建步骤: 打开git ,运行 npm install --global vue-cli 这是安装vue的命令行 vue init webpack vue-demo 这是vue基于webpack的 ...

  7. seriviceWorker 小结

    serviceWorker 的状态 install → activate. 1.初进页面,此前未加载过serviceWorker,直接进入install状态,随后进入activate状态,但是此时se ...

  8. Python 3.6print 出现 SyntaxError: invalid syntax

    开始使用sublime学习python,编写代码如图 Ctrl+B运行以后,报错   SyntaxError: invalid syntax 百度查询以后,大部分的回答都是说,python在3.0以后 ...

  9. windows 安装touch指令

    if you are using node.js just use npm to install it on Windows: C:\npm install touch-cli -g

  10. post表单翻页保存搜索条件

    问题:搜索条件下的数据,进行翻页行为后,搜索条件丢失 1.搜索表单 2.翻页 解决:既然点击页面跳转的a标签使用的方法是GET,而点击“搜索”按钮使用的方法是POST,那么可以让点击a标签实际上就是提 ...