mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-10-09 03:26:40 +08:00
Compare commits
5 Commits
c85f693ca6
...
554152635d
Author | SHA1 | Date | |
---|---|---|---|
|
554152635d | ||
|
b379574637 | ||
|
6a556cc6ff | ||
|
a6950275ad | ||
|
58b1bf5c33 |
@ -0,0 +1,54 @@
|
|||||||
|
package org.dromara.common.mybatis.aspect;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.dromara.common.mybatis.aspect;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.dromara.common.mybatis.annotation.DataPermission;
|
||||||
|
import org.springframework.aop.support.StaticMethodMatcherPointcut;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据权限匹配切点
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class DataPermissionPointcut extends StaticMethodMatcherPointcut {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(Method method, Class<?> targetClass) {
|
||||||
|
// 优先匹配方法
|
||||||
|
// 数据权限注解不对继承生效,所以检查当前方法是否有注解即可,不再往上匹配父类或接口
|
||||||
|
if (method.isAnnotationPresent(DataPermission.class)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.dromara.common.mybatis.aspect;
|
||||||
|
|
||||||
|
import org.aopalliance.aop.Advice;
|
||||||
|
import org.springframework.aop.Pointcut;
|
||||||
|
import org.springframework.aop.support.AbstractPointcutAdvisor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据权限注解切面定义
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class DataPermissionPointcutAdvisor extends AbstractPointcutAdvisor {
|
||||||
|
|
||||||
|
private final Advice advice;
|
||||||
|
private final Pointcut pointcut;
|
||||||
|
|
||||||
|
public DataPermissionPointcutAdvisor() {
|
||||||
|
this.advice = new DataPermissionAdvice();
|
||||||
|
this.pointcut = new DataPermissionPointcut();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pointcut getPointcut() {
|
||||||
|
return this.pointcut;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Advice getAdvice() {
|
||||||
|
return this.advice;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -11,15 +11,17 @@ 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.aspect.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.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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,6 +29,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||||
@EnableTransactionManagement(proxyTargetClass = true)
|
@EnableTransactionManagement(proxyTargetClass = true)
|
||||||
@MapperScan("${mybatis-plus.mapperPackage}")
|
@MapperScan("${mybatis-plus.mapperPackage}")
|
||||||
@PropertySource(value = "classpath:common-mybatis.yml", factory = YmlPropertySourceFactory.class)
|
@PropertySource(value = "classpath:common-mybatis.yml", factory = YmlPropertySourceFactory.class)
|
||||||
@ -54,15 +57,16 @@ public class MybatisPlusConfig {
|
|||||||
* 数据权限拦截器
|
* 数据权限拦截器
|
||||||
*/
|
*/
|
||||||
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
|
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
|
||||||
return new PlusDataPermissionInterceptor(SpringUtils.getProperty("mybatis-plus.mapperPackage"));
|
return new PlusDataPermissionInterceptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据权限切面处理器
|
* 数据权限切面处理器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public DataPermissionAspect dataPermissionAspect() {
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||||
return new DataPermissionAspect();
|
public DataPermissionPointcutAdvisor dataPermissionPointcutAdvisor() {
|
||||||
|
return new DataPermissionPointcutAdvisor();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,11 +49,6 @@ import java.util.function.Function;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class PlusDataPermissionHandler {
|
public class PlusDataPermissionHandler {
|
||||||
|
|
||||||
/**
|
|
||||||
* 类名称与注解的映射关系缓存(由于aop无法拦截mybatis接口类上的注解 只能通过启动预扫描的方式进行)
|
|
||||||
*/
|
|
||||||
private final Map<String, DataPermission> dataPermissionCacheMap = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* spel 解析器
|
* spel 解析器
|
||||||
*/
|
*/
|
||||||
@ -64,27 +59,17 @@ public class PlusDataPermissionHandler {
|
|||||||
*/
|
*/
|
||||||
private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory());
|
private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory());
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造方法,扫描指定包下的 Mapper 类并初始化缓存
|
|
||||||
*
|
|
||||||
* @param mapperPackage Mapper 类所在的包路径
|
|
||||||
*/
|
|
||||||
public PlusDataPermissionHandler(String mapperPackage) {
|
|
||||||
scanMapperClasses(mapperPackage);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数据过滤条件的 SQL 片段
|
* 获取数据过滤条件的 SQL 片段
|
||||||
*
|
*
|
||||||
* @param where 原始的查询条件表达式
|
* @param where 原始的查询条件表达式
|
||||||
* @param mappedStatementId Mapper 方法的 ID
|
|
||||||
* @param isSelect 是否为查询语句
|
* @param isSelect 是否为查询语句
|
||||||
* @return 数据过滤条件的 SQL 片段
|
* @return 数据过滤条件的 SQL 片段
|
||||||
*/
|
*/
|
||||||
public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
|
public Expression getSqlSegment(Expression where, boolean isSelect) {
|
||||||
try {
|
try {
|
||||||
// 获取数据权限配置
|
// 获取数据权限配置
|
||||||
DataPermission dataPermission = getDataPermission(mappedStatementId);
|
DataPermission dataPermission = getDataPermission();
|
||||||
// 获取当前登录用户信息
|
// 获取当前登录用户信息
|
||||||
LoginUser currentUser = DataPermissionHelper.getVariable("user");
|
LoginUser currentUser = DataPermissionHelper.getVariable("user");
|
||||||
if (ObjectUtil.isNull(currentUser)) {
|
if (ObjectUtil.isNull(currentUser)) {
|
||||||
@ -206,92 +191,22 @@ public class PlusDataPermissionHandler {
|
|||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 扫描指定包下的 Mapper 类,并查找其中带有特定注解的方法或类
|
|
||||||
*
|
|
||||||
* @param mapperPackage Mapper 类所在的包路径
|
|
||||||
*/
|
|
||||||
private void scanMapperClasses(String mapperPackage) {
|
|
||||||
// 创建资源解析器和元数据读取工厂
|
|
||||||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
||||||
CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
|
|
||||||
// 将 Mapper 包路径按分隔符拆分为数组
|
|
||||||
String[] packagePatternArray = StringUtils.splitPreserveAllTokens(mapperPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
|
|
||||||
String classpath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX;
|
|
||||||
try {
|
|
||||||
for (String packagePattern : packagePatternArray) {
|
|
||||||
// 将包路径转换为资源路径
|
|
||||||
String path = ClassUtils.convertClassNameToResourcePath(packagePattern);
|
|
||||||
// 获取指定路径下的所有 .class 文件资源
|
|
||||||
Resource[] resources = resolver.getResources(classpath + path + "/*.class");
|
|
||||||
for (Resource resource : resources) {
|
|
||||||
// 获取资源的类元数据
|
|
||||||
ClassMetadata classMetadata = factory.getMetadataReader(resource).getClassMetadata();
|
|
||||||
// 获取资源对应的类对象
|
|
||||||
Class<?> clazz = Resources.classForName(classMetadata.getClassName());
|
|
||||||
// 查找类中的特定注解
|
|
||||||
findAnnotation(clazz);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("初始化数据安全缓存时出错:{}", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在指定的类中查找特定的注解 DataPermission,并将带有这个注解的方法或类存储到 dataPermissionCacheMap 中
|
|
||||||
*
|
|
||||||
* @param clazz 要查找的类
|
|
||||||
*/
|
|
||||||
private void findAnnotation(Class<?> clazz) {
|
|
||||||
DataPermission dataPermission;
|
|
||||||
for (Method method : clazz.getMethods()) {
|
|
||||||
if (method.isDefault() || method.isVarArgs()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String mappedStatementId = clazz.getName() + "." + method.getName();
|
|
||||||
if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) {
|
|
||||||
dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class);
|
|
||||||
dataPermissionCacheMap.put(mappedStatementId, dataPermission);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) {
|
|
||||||
dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class);
|
|
||||||
dataPermissionCacheMap.put(clazz.getName(), dataPermission);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据映射语句 ID 或类名获取对应的 DataPermission 注解对象
|
* 根据映射语句 ID 或类名获取对应的 DataPermission 注解对象
|
||||||
*
|
*
|
||||||
* @param mapperId 映射语句 ID
|
|
||||||
* @return DataPermission 注解对象,如果不存在则返回 null
|
* @return DataPermission 注解对象,如果不存在则返回 null
|
||||||
*/
|
*/
|
||||||
public DataPermission getDataPermission(String mapperId) {
|
public DataPermission getDataPermission() {
|
||||||
// 检查上下文中是否包含映射语句 ID 对应的 DataPermission 注解对象
|
|
||||||
if (DataPermissionHelper.getPermission() != null) {
|
|
||||||
return DataPermissionHelper.getPermission();
|
return DataPermissionHelper.getPermission();
|
||||||
}
|
}
|
||||||
// 检查缓存中是否包含映射语句 ID 对应的 DataPermission 注解对象
|
|
||||||
if (dataPermissionCacheMap.containsKey(mapperId)) {
|
|
||||||
return dataPermissionCacheMap.get(mapperId);
|
|
||||||
}
|
|
||||||
// 如果缓存中不包含映射语句 ID 对应的 DataPermission 注解对象,则尝试使用类名作为键查找
|
|
||||||
String clazzName = mapperId.substring(0, mapperId.lastIndexOf("."));
|
|
||||||
if (dataPermissionCacheMap.containsKey(clazzName)) {
|
|
||||||
return dataPermissionCacheMap.get(clazzName);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查给定的映射语句 ID 是否有效,即是否能够找到对应的 DataPermission 注解对象
|
* 检查给定的映射语句 ID 是否有效,即是否能够找到对应的 DataPermission 注解对象
|
||||||
*
|
*
|
||||||
* @param mapperId 映射语句 ID
|
|
||||||
* @return 如果找到对应的 DataPermission 注解对象,则返回 false;否则返回 true
|
* @return 如果找到对应的 DataPermission 注解对象,则返回 false;否则返回 true
|
||||||
*/
|
*/
|
||||||
public boolean invalid(String mapperId) {
|
public boolean invalid() {
|
||||||
return getDataPermission(mapperId) == null;
|
return getDataPermission() == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,16 +35,7 @@ import java.util.List;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class PlusDataPermissionInterceptor extends BaseMultiTableInnerInterceptor implements InnerInterceptor {
|
public class PlusDataPermissionInterceptor extends BaseMultiTableInnerInterceptor implements InnerInterceptor {
|
||||||
|
|
||||||
private final PlusDataPermissionHandler dataPermissionHandler;
|
private final PlusDataPermissionHandler dataPermissionHandler = new PlusDataPermissionHandler();
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造函数,初始化 PlusDataPermissionHandler 实例
|
|
||||||
*
|
|
||||||
* @param mapperPackage 扫描的映射器包
|
|
||||||
*/
|
|
||||||
public PlusDataPermissionInterceptor(String mapperPackage) {
|
|
||||||
this.dataPermissionHandler = new PlusDataPermissionHandler(mapperPackage);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在执行查询之前,检查并处理数据权限相关逻辑
|
* 在执行查询之前,检查并处理数据权限相关逻辑
|
||||||
@ -64,7 +55,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 检查是否缺少有效的数据权限注解
|
// 检查是否缺少有效的数据权限注解
|
||||||
if (dataPermissionHandler.invalid(ms.getId())) {
|
if (dataPermissionHandler.invalid()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 解析 sql 分配对应方法
|
// 解析 sql 分配对应方法
|
||||||
@ -92,7 +83,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 检查是否缺少有效的数据权限注解
|
// 检查是否缺少有效的数据权限注解
|
||||||
if (dataPermissionHandler.invalid(ms.getId())) {
|
if (dataPermissionHandler.invalid()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
|
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
|
||||||
@ -128,7 +119,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void processUpdate(Update update, int index, String sql, Object obj) {
|
protected void processUpdate(Update update, int index, String sql, Object obj) {
|
||||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), (String) obj, false);
|
Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), false);
|
||||||
if (null != sqlSegment) {
|
if (null != sqlSegment) {
|
||||||
update.setWhere(sqlSegment);
|
update.setWhere(sqlSegment);
|
||||||
}
|
}
|
||||||
@ -144,7 +135,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void processDelete(Delete delete, int index, String sql, Object obj) {
|
protected void processDelete(Delete delete, int index, String sql, Object obj) {
|
||||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), (String) obj, false);
|
Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), false);
|
||||||
if (null != sqlSegment) {
|
if (null != sqlSegment) {
|
||||||
delete.setWhere(sqlSegment);
|
delete.setWhere(sqlSegment);
|
||||||
}
|
}
|
||||||
@ -157,7 +148,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
|||||||
* @param mappedStatementId 映射语句的 ID
|
* @param mappedStatementId 映射语句的 ID
|
||||||
*/
|
*/
|
||||||
protected void setWhere(PlainSelect plainSelect, String mappedStatementId) {
|
protected void setWhere(PlainSelect plainSelect, String mappedStatementId) {
|
||||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), mappedStatementId, true);
|
Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), true);
|
||||||
if (null != sqlSegment) {
|
if (null != sqlSegment) {
|
||||||
plainSelect.setWhere(sqlSegment);
|
plainSelect.setWhere(sqlSegment);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,146 @@
|
|||||||
|
package com.aizuda.snailjob.server.common.register;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.aizuda.snailjob.common.core.enums.NodeTypeEnum;
|
||||||
|
import com.aizuda.snailjob.common.core.util.JsonUtil;
|
||||||
|
import com.aizuda.snailjob.common.core.util.NetUtil;
|
||||||
|
import com.aizuda.snailjob.common.core.util.SnailJobVersion;
|
||||||
|
import com.aizuda.snailjob.common.core.util.StreamUtils;
|
||||||
|
import com.aizuda.snailjob.common.log.SnailJobLog;
|
||||||
|
import com.aizuda.snailjob.server.common.cache.CacheConsumerGroup;
|
||||||
|
import com.aizuda.snailjob.server.common.config.SystemProperties;
|
||||||
|
import com.aizuda.snailjob.server.common.convert.RegisterNodeInfoConverter;
|
||||||
|
import com.aizuda.snailjob.server.common.dto.ServerNodeExtAttrs;
|
||||||
|
import com.aizuda.snailjob.server.common.handler.InstanceManager;
|
||||||
|
import com.aizuda.snailjob.template.datasource.persistence.po.ServerNode;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务端注册
|
||||||
|
*
|
||||||
|
* @author opensnail
|
||||||
|
* @date 2023-06-07
|
||||||
|
* @since 1.6.0
|
||||||
|
*/
|
||||||
|
@Component(ServerRegister.BEAN_NAME)
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class ServerRegister extends AbstractRegister {
|
||||||
|
public static final String BEAN_NAME = "serverRegister";
|
||||||
|
private final ScheduledExecutorService serverRegisterNode = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "server-register-node"));
|
||||||
|
public static final int DELAY_TIME = 30;
|
||||||
|
public static final String CURRENT_CID;
|
||||||
|
public static final String GROUP_NAME = "DEFAULT_SERVER";
|
||||||
|
public static final String NAMESPACE_ID = "DEFAULT_SERVER_NAMESPACE_ID";
|
||||||
|
private final InstanceManager instanceManager;
|
||||||
|
private final SystemProperties systemProperties;
|
||||||
|
private final ServerProperties serverProperties;
|
||||||
|
|
||||||
|
static {
|
||||||
|
CURRENT_CID = IdUtil.getSnowflakeNextIdStr();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(int type) {
|
||||||
|
return getNodeType().equals(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void beforeProcessor(RegisterContext context) {
|
||||||
|
// 新增扩展参数
|
||||||
|
ServerNodeExtAttrs serverNodeExtAttrs = new ServerNodeExtAttrs();
|
||||||
|
serverNodeExtAttrs.setWebPort(serverProperties.getPort());
|
||||||
|
serverNodeExtAttrs.setSystemVersion(SnailJobVersion.getVersion());
|
||||||
|
|
||||||
|
context.setGroupName(GROUP_NAME);
|
||||||
|
context.setHostId(CURRENT_CID);
|
||||||
|
String serverHost = systemProperties.getServerHost();
|
||||||
|
if (StrUtil.isEmptyIfStr(serverHost)) {
|
||||||
|
serverHost = NetUtil.getLocalIpStr();
|
||||||
|
}
|
||||||
|
context.setHostIp(serverHost);
|
||||||
|
context.setHostPort(systemProperties.getServerPort());
|
||||||
|
context.setContextPath(Optional.ofNullable(serverProperties.getServlet().getContextPath()).orElse(StrUtil.EMPTY));
|
||||||
|
context.setNamespaceId(NAMESPACE_ID);
|
||||||
|
context.setExtAttrs(JsonUtil.toJsonString(serverNodeExtAttrs));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected LocalDateTime getExpireAt() {
|
||||||
|
return LocalDateTime.now().plusSeconds(DELAY_TIME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean doRegister(RegisterContext context, ServerNode serverNode) {
|
||||||
|
refreshExpireAt(Lists.newArrayList(serverNode));
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void afterProcessor(final ServerNode serverNode) {
|
||||||
|
try {
|
||||||
|
// 同步当前POD消费的组的节点信息
|
||||||
|
// netty的client只会注册到一个服务端,若组分配的和client连接的不是一个POD则会导致当前POD没有其他客户端的注册信息
|
||||||
|
ConcurrentMap<String /*groupName*/, Set<String>/*namespaceId*/> allConsumerGroupName = CacheConsumerGroup.getAllConsumerGroupName();
|
||||||
|
if (CollUtil.isNotEmpty(allConsumerGroupName)) {
|
||||||
|
Set<String> namespaceIdSets = StreamUtils.toSetByFlatMap(allConsumerGroupName.values(), Set::stream);
|
||||||
|
if (CollUtil.isEmpty(namespaceIdSets)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<ServerNode> serverNodes = serverNodeMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<ServerNode>()
|
||||||
|
.eq(ServerNode::getNodeType, NodeTypeEnum.CLIENT.getType())
|
||||||
|
.in(ServerNode::getNamespaceId, namespaceIdSets)
|
||||||
|
.in(ServerNode::getGroupName, allConsumerGroupName.keySet()));
|
||||||
|
for (final ServerNode node : serverNodes) {
|
||||||
|
// 刷新全量本地缓存
|
||||||
|
instanceManager.registerOrUpdate(RegisterNodeInfoConverter.INSTANCE.toRegisterNodeInfo(node));
|
||||||
|
// 刷新过期时间
|
||||||
|
CacheConsumerGroup.addOrUpdate(node.getGroupName(), node.getNamespaceId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
SnailJobLog.LOCAL.error("Client refresh failed", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Integer getNodeType() {
|
||||||
|
return NodeTypeEnum.SERVER.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {
|
||||||
|
SnailJobLog.LOCAL.info("ServerRegister start");
|
||||||
|
|
||||||
|
serverRegisterNode.scheduleAtFixedRate(() -> {
|
||||||
|
try {
|
||||||
|
this.register(new RegisterContext());
|
||||||
|
} catch (Exception e) {
|
||||||
|
SnailJobLog.LOCAL.error("Server-side registration failed", e);
|
||||||
|
}
|
||||||
|
}, 0, DELAY_TIME * 2 / 3, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
SnailJobLog.LOCAL.info("ServerRegister close");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user