mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-19 01:48:47 +08:00
remove 移除冗余代码
This commit is contained in:
parent
6979a0833e
commit
984f07d9b2
@ -57,7 +57,7 @@ public class MybatisPlusConfig {
|
|||||||
* 数据权限拦截器
|
* 数据权限拦截器
|
||||||
*/
|
*/
|
||||||
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
|
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
|
||||||
return new PlusDataPermissionInterceptor(SpringUtils.getProperty("mybatis-plus.mapperPackage"));
|
return new PlusDataPermissionInterceptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user