mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-19 01:48:47 +08:00
update 把数据权限注解全部交给AOP处理,使用自定义动态方法匹配器匹配注解
This commit is contained in:
parent
2472359adb
commit
6979a0833e
@ -0,0 +1,54 @@
|
|||||||
|
package org.dromara.common.mybatis.aop;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aopalliance.intercept.MethodInterceptor;
|
||||||
|
import org.aopalliance.intercept.MethodInvocation;
|
||||||
|
import org.dromara.common.mybatis.annotation.DataPermission;
|
||||||
|
import org.dromara.common.mybatis.helper.DataPermissionHelper;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据权限注解Advice
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class DataPermissionAdvice implements MethodInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||||
|
Object target = invocation.getThis();
|
||||||
|
Method method = invocation.getMethod();
|
||||||
|
Object[] args = invocation.getArguments();
|
||||||
|
// 设置权限注解
|
||||||
|
DataPermissionHelper.setPermission(getDataPermissionAnnotation(target, method, args));
|
||||||
|
try {
|
||||||
|
// 执行代理方法
|
||||||
|
return invocation.proceed();
|
||||||
|
} finally {
|
||||||
|
// 清除权限注解
|
||||||
|
DataPermissionHelper.removePermission();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据权限注解
|
||||||
|
*/
|
||||||
|
private DataPermission getDataPermissionAnnotation(Object target, Method method,Object[] args){
|
||||||
|
DataPermission dataPermission = method.getAnnotation(DataPermission.class);
|
||||||
|
// 优先获取方法上的注解
|
||||||
|
if (dataPermission != null) {
|
||||||
|
return dataPermission;
|
||||||
|
}
|
||||||
|
// 方法上没有注解,则获取类上的注解
|
||||||
|
Class<?> targetClass = target.getClass();
|
||||||
|
// 如果是 JDK 动态代理,则获取真实的Class实例
|
||||||
|
if (Proxy.isProxyClass(targetClass)) {
|
||||||
|
targetClass = targetClass.getInterfaces()[0];
|
||||||
|
}
|
||||||
|
dataPermission = targetClass.getAnnotation(DataPermission.class);
|
||||||
|
return dataPermission;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
package org.dromara.common.mybatis.aop;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.dromara.common.mybatis.annotation.DataPermission;
|
||||||
|
import org.springframework.aop.support.DynamicMethodMatcher;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据权限动态方法匹配器
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class DataPermissionDynamicMethodMatcher extends DynamicMethodMatcher {
|
||||||
|
|
||||||
|
public DataPermissionDynamicMethodMatcher() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(Method method, Class<?> targetClass) {
|
||||||
|
// 优先匹配方法
|
||||||
|
// 数据权限注解不对继承生效,所以检查当前方法是否有注解即可,不再往上匹配父类或接口
|
||||||
|
if (method.isAnnotationPresent(DataPermission.class)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO MyBatis 的 Mapper 就是通过 JDK 动态代理实现的,所以这里需要检查是否匹配 JDK 的动态代理
|
||||||
|
Class<?> targetClassRef = targetClass;
|
||||||
|
if (Proxy.isProxyClass(targetClassRef)) {
|
||||||
|
// 数据权限注解不对继承生效,但由于 SpringIOC 容器拿到的实际上是 MyBatis 代理过后的 Mapper,而 targetClass.isAnnotationPresent 实际匹配的是 Proxy 类的注解,不会查找代理类。
|
||||||
|
// 所以这里不能用 targetClass.isAnnotationPresent,只能用 AnnotatedElementUtils.hasAnnotation 或 targetClass.getInterfaces()[0].isAnnotationPresent 去做匹配,以检查被代理的 MapperClass 是否具有注解
|
||||||
|
// 原理:JDK 动态代理本质上就是对接口进行实现然后对具体的接口实现做代理,所以直接通过接口可以拿到实际的 MapperClass
|
||||||
|
targetClassRef = targetClass.getInterfaces()[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
return targetClassRef.isAnnotationPresent(DataPermission.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(Method method, Class<?> targetClass, Object... args) {
|
||||||
|
return matches(method, targetClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package org.dromara.common.mybatis.aop;
|
||||||
|
|
||||||
|
import org.aopalliance.aop.Advice;
|
||||||
|
import org.dromara.common.mybatis.annotation.DataPermission;
|
||||||
|
import org.springframework.aop.Pointcut;
|
||||||
|
import org.springframework.aop.support.AbstractPointcutAdvisor;
|
||||||
|
import org.springframework.aop.support.ComposablePointcut;
|
||||||
|
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据权限注解切面定义
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class DataPermissionPointcutAdvisor extends AbstractPointcutAdvisor {
|
||||||
|
|
||||||
|
private final Advice advice;
|
||||||
|
private final Pointcut pointcut;
|
||||||
|
|
||||||
|
public DataPermissionPointcutAdvisor() {
|
||||||
|
this.advice = new DataPermissionAdvice();
|
||||||
|
AnnotationMatchingPointcut matchingPointcut = new AnnotationMatchingPointcut(DataPermission.class, true);
|
||||||
|
DataPermissionDynamicMethodMatcher matcher = new DataPermissionDynamicMethodMatcher();
|
||||||
|
this.pointcut = new ComposablePointcut(matcher).union(matchingPointcut);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pointcut getPointcut() {
|
||||||
|
return this.pointcut;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Advice getAdvice() {
|
||||||
|
return this.advice;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,50 +0,0 @@
|
|||||||
package org.dromara.common.mybatis.aspect;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
|
||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
|
||||||
import org.aspectj.lang.annotation.Before;
|
|
||||||
import org.dromara.common.mybatis.annotation.DataPermission;
|
|
||||||
import org.dromara.common.mybatis.helper.DataPermissionHelper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据权限处理
|
|
||||||
*
|
|
||||||
* @author Lion Li
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@Aspect
|
|
||||||
public class DataPermissionAspect {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理请求前执行
|
|
||||||
*/
|
|
||||||
@Before(value = "@annotation(dataPermission)")
|
|
||||||
public void doBefore(JoinPoint joinPoint, DataPermission dataPermission) {
|
|
||||||
DataPermissionHelper.setPermission(dataPermission);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理完请求后执行
|
|
||||||
*
|
|
||||||
* @param joinPoint 切点
|
|
||||||
*/
|
|
||||||
@AfterReturning(pointcut = "@annotation(dataPermission)")
|
|
||||||
public void doAfterReturning(JoinPoint joinPoint, DataPermission dataPermission) {
|
|
||||||
DataPermissionHelper.removePermission();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 拦截异常操作
|
|
||||||
*
|
|
||||||
* @param joinPoint 切点
|
|
||||||
* @param e 异常
|
|
||||||
*/
|
|
||||||
@AfterThrowing(value = "@annotation(dataPermission)", throwing = "e")
|
|
||||||
public void doAfterThrowing(JoinPoint joinPoint, DataPermission dataPermission, Exception e) {
|
|
||||||
DataPermissionHelper.removePermission();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -11,15 +11,18 @@ import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerIntercept
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||||
import org.dromara.common.core.factory.YmlPropertySourceFactory;
|
import org.dromara.common.core.factory.YmlPropertySourceFactory;
|
||||||
import org.dromara.common.core.utils.SpringUtils;
|
import org.dromara.common.core.utils.SpringUtils;
|
||||||
import org.dromara.common.mybatis.aspect.DataPermissionAspect;
|
import org.dromara.common.mybatis.aop.DataPermissionPointcutAdvisor;
|
||||||
import org.dromara.common.mybatis.handler.InjectionMetaObjectHandler;
|
import org.dromara.common.mybatis.handler.InjectionMetaObjectHandler;
|
||||||
import org.dromara.common.mybatis.handler.MybatisExceptionHandler;
|
import org.dromara.common.mybatis.handler.MybatisExceptionHandler;
|
||||||
import org.dromara.common.mybatis.handler.PlusPostInitTableInfoHandler;
|
import org.dromara.common.mybatis.handler.PlusPostInitTableInfoHandler;
|
||||||
import org.dromara.common.mybatis.interceptor.PlusDataPermissionInterceptor;
|
import org.dromara.common.mybatis.interceptor.PlusDataPermissionInterceptor;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.context.annotation.Role;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,8 +64,10 @@ public class MybatisPlusConfig {
|
|||||||
* 数据权限切面处理器
|
* 数据权限切面处理器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public DataPermissionAspect dataPermissionAspect() {
|
@ConditionalOnMissingBean
|
||||||
return new DataPermissionAspect();
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||||
|
public DataPermissionPointcutAdvisor dataPermissionPointcutAdvisor() {
|
||||||
|
return new DataPermissionPointcutAdvisor();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user