mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
✨ Introducing new features. druid log 支持根据表名跳过输出
This commit is contained in:
1 parent
0a03ea4b80
commit
3244347b99
7 files changed
+90
-11
No files matched your search
+3
-2
@@ -183,8 +183,9 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
|
||||
wasLocked = true;
|
||||
}
|
||||
|
||||
byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet())
|
||||
.toArray(new byte[0][]);
|
||||
byte[][] keys = Optional.ofNullable(connection.keys(pattern))
|
||||
.orElse(Collections.emptySet())
|
||||
.toArray(new byte[0][]);
|
||||
|
||||
if (keys.length > 0) {
|
||||
connection.del(keys);
|
||||
|
||||
+8
-5
@@ -60,9 +60,12 @@ public class PigxDefaultDatascopeHandle implements DataScopeHandle {
|
||||
return true;
|
||||
}
|
||||
|
||||
List<String> roleIdList = user.getAuthorities().stream().map(GrantedAuthority::getAuthority)
|
||||
.filter(authority -> authority.startsWith(SecurityConstants.ROLE))
|
||||
.map(authority -> authority.split(StrUtil.UNDERLINE)[1]).collect(Collectors.toList());
|
||||
List<String> roleIdList = user.getAuthorities()
|
||||
.stream()
|
||||
.map(GrantedAuthority::getAuthority)
|
||||
.filter(authority -> authority.startsWith(SecurityConstants.ROLE))
|
||||
.map(authority -> authority.split(StrUtil.UNDERLINE)[1])
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<Long> deptList = dataScope.getDeptList();
|
||||
|
||||
@@ -89,8 +92,8 @@ public class PigxDefaultDatascopeHandle implements DataScopeHandle {
|
||||
// 自定义
|
||||
if (DataScopeTypeEnum.CUSTOM.getType() == dsType && StrUtil.isNotBlank(role.getDsScope())) {
|
||||
String dsScope = role.getDsScope();
|
||||
deptList.addAll(
|
||||
Arrays.stream(dsScope.split(StrUtil.COMMA)).map(Long::parseLong).collect(Collectors.toList()));
|
||||
deptList
|
||||
.addAll(Arrays.stream(dsScope.split(StrUtil.COMMA)).map(Long::parseLong).collect(Collectors.toList()));
|
||||
}
|
||||
// 查询本级及其下级
|
||||
if (DataScopeTypeEnum.OWN_CHILD_LEVEL.getType() == dsType) {
|
||||
|
||||
+44
-2
@@ -16,6 +16,9 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.data.mybatis;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.filter.FilterEventAdapter;
|
||||
@@ -23,15 +26,19 @@ import com.alibaba.druid.proxy.jdbc.JdbcParameter;
|
||||
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.StatementProxy;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import com.pig4cloud.pigx.common.data.config.PigxMybatisProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 打印可执行的 sql 日志
|
||||
@@ -110,13 +117,27 @@ public class DruidSqlLogFilter extends FilterEventAdapter {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
|
||||
|
||||
// 判断表名是配置了匹配过滤
|
||||
if (CollUtil.isNotEmpty(properties.getSkipTable())) {
|
||||
List<String> skipTableList = properties.getSkipTable();
|
||||
|
||||
List<String> tableNameList = getTablesBydruid(sql, dbType);
|
||||
if (tableNameList.stream()
|
||||
.anyMatch(tableName -> StrUtil.containsAnyIgnoreCase(tableName,
|
||||
ArrayUtil.toArray(skipTableList, String.class)))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int parametersSize = statement.getParametersSize();
|
||||
List<Object> parameters = new ArrayList<>(parametersSize);
|
||||
for (int i = 0; i < parametersSize; ++i) {
|
||||
// 转换参数,处理 java8 时间
|
||||
parameters.add(getJdbcParameter(statement.getParameter(i)));
|
||||
}
|
||||
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
|
||||
String formattedSql = SQLUtils.format(sql, DbType.of(dbType), parameters, FORMAT_OPTION);
|
||||
printSql(formattedSql, statement);
|
||||
}
|
||||
@@ -159,4 +180,25 @@ public class DruidSqlLogFilter extends FilterEventAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从SQL中提取表名(sql中出现的所有表)
|
||||
* @param sql sql语句
|
||||
* @param dbType dbType
|
||||
* @return List<String>
|
||||
*/
|
||||
public static List<String> getTablesBydruid(String sql, String dbType) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
// 也可以用更精确的解析器,如MySqlSchemaStatVisitor
|
||||
SchemaStatVisitor visitor = new SchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
Map<TableStat.Name, TableStat> tables = visitor.getTables();
|
||||
for (TableStat.Name name : tables.keySet()) {
|
||||
result.add(name.getName());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
-1
@@ -22,7 +22,6 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService;
|
||||
import com.pig4cloud.pigx.common.data.config.PigxMybatisProperties;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeInnerInterceptor;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeInterceptor;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeSqlInjector;
|
||||
|
||||
+8
@@ -3,8 +3,10 @@ package com.pig4cloud.pigx.common.data.mybatis;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -78,9 +80,15 @@ public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
|
||||
*/
|
||||
private String getUserName() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// 匿名接口直接返回
|
||||
if (authentication instanceof AnonymousAuthenticationToken) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Optional.ofNullable(authentication).isPresent()) {
|
||||
return authentication.getName();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -1,9 +1,12 @@
|
||||
package com.pig4cloud.pigx.common.data.config;
|
||||
package com.pig4cloud.pigx.common.data.mybatis;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mybatis 配置
|
||||
*
|
||||
@@ -20,4 +23,9 @@ public class PigxMybatisProperties {
|
||||
*/
|
||||
private boolean showSql = true;
|
||||
|
||||
/**
|
||||
* 跳过表
|
||||
*/
|
||||
private List<String> skipTable = new ArrayList<>();
|
||||
|
||||
}
|
||||
+18
@@ -7,6 +7,9 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/5/12
|
||||
@@ -16,6 +19,21 @@ import lombok.experimental.UtilityClass;
|
||||
@UtilityClass
|
||||
public class ParamResolver {
|
||||
|
||||
/**
|
||||
* 根据多个key 查询value 配置 结果使用hutool 的maputil 进行包装处理 MapUtil.getBool(result,key)
|
||||
* @param key key
|
||||
* @return Map<String,Object>
|
||||
*/
|
||||
public Map<String, Object> getMap(String... key) {
|
||||
// 校验入参是否合法
|
||||
if (Objects.isNull(key)) {
|
||||
throw new IllegalArgumentException("参数不合法");
|
||||
}
|
||||
|
||||
RemoteParamService remoteParamService = SpringContextHolder.getBean(RemoteParamService.class);
|
||||
return remoteParamService.getByKeys(key, SecurityConstants.FROM_IN).getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key 查询value 配置
|
||||
* @param key key
|
||||
|
||||
Reference in new issue
Block a user