From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001
From: 田源 <tianyuan@vci-tech.com>
Date: 星期四, 03 四月 2025 14:35:02 +0800
Subject: [PATCH] 添加非密字段显示
---
Source/BladeX-Tool/blade-starter-datascope/src/main/java/org/springblade/core/datascope/interceptor/DataScopeInterceptor.java | 139 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 139 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-starter-datascope/src/main/java/org/springblade/core/datascope/interceptor/DataScopeInterceptor.java b/Source/BladeX-Tool/blade-starter-datascope/src/main/java/org/springblade/core/datascope/interceptor/DataScopeInterceptor.java
new file mode 100644
index 0000000..992f4a3
--- /dev/null
+++ b/Source/BladeX-Tool/blade-starter-datascope/src/main/java/org/springblade/core/datascope/interceptor/DataScopeInterceptor.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the dreamlu.net developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: Chill 搴勯獮 (smallchill@163.com)
+ */
+package org.springblade.core.datascope.interceptor;
+
+import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlCommandType;
+import org.apache.ibatis.mapping.StatementType;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.springblade.core.datascope.annotation.DataAuth;
+import org.springblade.core.datascope.handler.DataScopeHandler;
+import org.springblade.core.datascope.model.DataScopeModel;
+import org.springblade.core.datascope.props.DataScopeProperties;
+import org.springblade.core.mp.intercept.QueryInterceptor;
+import org.springblade.core.secure.BladeUser;
+import org.springblade.core.secure.utils.AuthUtil;
+import org.springblade.core.tool.utils.ClassUtil;
+import org.springblade.core.tool.utils.SpringUtil;
+import org.springblade.core.tool.utils.StringUtil;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+
+/**
+ * mybatis 鏁版嵁鏉冮檺鎷︽埅鍣�
+ *
+ * @author L.cm, Chill
+ */
+@Slf4j
+@RequiredArgsConstructor
+@SuppressWarnings({"rawtypes"})
+public class DataScopeInterceptor implements QueryInterceptor {
+
+ private final ConcurrentMap<String, DataAuth> dataAuthMap = new ConcurrentHashMap<>(8);
+
+ private final DataScopeHandler dataScopeHandler;
+ private final DataScopeProperties dataScopeProperties;
+
+ @Override
+ public void intercept(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
+ //鏈惎鐢ㄥ垯鏀捐
+ if (!dataScopeProperties.getEnabled()) {
+ return;
+ }
+
+ //鏈彇鍒扮敤鎴峰垯鏀捐
+ BladeUser bladeUser = AuthUtil.getUser();
+ if (bladeUser == null) {
+ return;
+ }
+
+ if (SqlCommandType.SELECT != ms.getSqlCommandType() || StatementType.CALLABLE == ms.getStatementType()) {
+ return;
+ }
+
+ String originalSql = boundSql.getSql();
+
+ //鏌ユ壘娉ㄨВ涓寘鍚獶ataAuth绫诲瀷鐨勫弬鏁�
+ DataAuth dataAuth = findDataAuthAnnotation(ms);
+
+ //娉ㄨВ涓虹┖骞朵笖鏁版嵁鏉冮檺鏂规硶鍚嶆湭鍖归厤鍒�,鍒欐斁琛�
+ String mapperId = ms.getId();
+ String className = mapperId.substring(0, mapperId.lastIndexOf(StringPool.DOT));
+ String mapperName = ClassUtil.getShortName(className);
+ String methodName = mapperId.substring(mapperId.lastIndexOf(StringPool.DOT) + 1);
+ boolean mapperSkip = dataScopeProperties.getMapperKey().stream().noneMatch(methodName::contains)
+ || dataScopeProperties.getMapperExclude().stream().anyMatch(mapperName::contains);
+ if (dataAuth == null && mapperSkip) {
+ return;
+ }
+
+ //鍒涘缓鏁版嵁鏉冮檺妯″瀷
+ DataScopeModel dataScope = new DataScopeModel();
+
+ //鑻ユ敞瑙d笉涓虹┖,鍒欓厤缃敞瑙i」
+ if (dataAuth != null) {
+ dataScope.setResourceCode(dataAuth.code());
+ dataScope.setScopeColumn(dataAuth.column());
+ dataScope.setScopeType(dataAuth.type().getType());
+ dataScope.setScopeField(dataAuth.field());
+ dataScope.setScopeValue(dataAuth.value());
+ }
+
+ //鑾峰彇鏁版嵁鏉冮檺瑙勫垯瀵瑰簲鐨勭瓫閫塖ql
+ String sqlCondition = dataScopeHandler.sqlCondition(mapperId, dataScope, bladeUser, originalSql);
+ if (!StringUtil.isBlank(sqlCondition)) {
+ PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
+ mpBoundSql.sql(sqlCondition);
+ }
+ }
+
+ /**
+ * 鑾峰彇鏁版嵁鏉冮檺娉ㄨВ淇℃伅
+ *
+ * @param mappedStatement mappedStatement
+ * @return DataAuth
+ */
+ private DataAuth findDataAuthAnnotation(MappedStatement mappedStatement) {
+ String id = mappedStatement.getId();
+ return dataAuthMap.computeIfAbsent(id, (key) -> {
+ String className = key.substring(0, key.lastIndexOf(StringPool.DOT));
+ String mapperBean = StringUtil.firstCharToLower(ClassUtil.getShortName(className));
+ Object mapper = SpringUtil.getBean(mapperBean);
+ String methodName = key.substring(key.lastIndexOf(StringPool.DOT) + 1);
+ Class<?>[] interfaces = ClassUtil.getAllInterfaces(mapper);
+ for (Class<?> mapperInterface : interfaces) {
+ for (Method method : mapperInterface.getDeclaredMethods()) {
+ if (methodName.equals(method.getName()) && method.isAnnotationPresent(DataAuth.class)) {
+ return method.getAnnotation(DataAuth.class);
+ }
+ }
+ }
+ return null;
+ });
+ }
+
+}
--
Gitblit v1.9.3