com.pig4cloud
pigx-common-sentinel
diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java
deleted file mode 100644
index ca72505f1..000000000
--- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/*
- * Copyright (c) 2018-2025, lengleng 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 pig4cloud.com 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: lengleng (wangiegie@gmail.com)
- */
-
-package com.pig4cloud.pigx.common.core.sensitive;
-
-import cn.hutool.core.util.DesensitizedUtil;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.databind.BeanProperty;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.JsonSerializer;
-import com.fasterxml.jackson.databind.SerializerProvider;
-import com.fasterxml.jackson.databind.ser.ContextualSerializer;
-import com.pig4cloud.pigx.common.core.util.DesensitizedUtils;
-import lombok.AllArgsConstructor;
-import lombok.NoArgsConstructor;
-
-import java.io.IOException;
-import java.util.Objects;
-
-/**
- * @author lengleng
- * @date 2019-08-13
- *
- * 脱敏序列化
- */
-@NoArgsConstructor
-@AllArgsConstructor
-public class SensitiveSerialize extends JsonSerializer implements ContextualSerializer {
-
- private SensitiveTypeEnum type;
-
- private Integer prefixNoMaskLen;
-
- private Integer suffixNoMaskLen;
-
- private String maskStr;
-
- @Override
- public void serialize(final String origin, final JsonGenerator jsonGenerator,
- final SerializerProvider serializerProvider) throws IOException {
- switch (type) {
- case CHINESE_NAME:
- jsonGenerator.writeString(DesensitizedUtils.chineseName(origin));
- break;
- case ID_CARD:
- jsonGenerator.writeString(DesensitizedUtils.idCardNum(origin));
- break;
- case FIXED_PHONE:
- jsonGenerator.writeString(DesensitizedUtils.fixedPhone(origin));
- break;
- case MOBILE_PHONE:
- jsonGenerator.writeString(DesensitizedUtils.mobilePhone(origin));
- break;
- case ADDRESS:
- jsonGenerator.writeString(DesensitizedUtils.address(origin));
- break;
- case EMAIL:
- jsonGenerator.writeString(DesensitizedUtils.email(origin));
- break;
- case BANK_CARD:
- jsonGenerator.writeString(DesensitizedUtils.bankCard(origin));
- break;
- case PASSWORD:
- jsonGenerator.writeString(DesensitizedUtils.password(origin));
- break;
- case KEY:
- jsonGenerator.writeString(DesensitizedUtils.key(origin));
- break;
- case IPV4:
- jsonGenerator.writeString(DesensitizedUtils.ipv4(origin));
- break;
- case CAR_LICENSE:
- jsonGenerator.writeString(DesensitizedUtil.carLicense(origin));
- break;
- case CUSTOMER:
- jsonGenerator
- .writeString(DesensitizedUtils.desValue(origin, prefixNoMaskLen, suffixNoMaskLen, maskStr));
- break;
- default:
- throw new IllegalArgumentException("Unknow sensitive type enum " + type);
- }
- }
-
- @Override
- public JsonSerializer> createContextual(final SerializerProvider serializerProvider,
- final BeanProperty beanProperty) throws JsonMappingException {
- if (beanProperty != null) {
- if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
- Sensitive sensitive = beanProperty.getAnnotation(Sensitive.class);
- if (sensitive == null) {
- sensitive = beanProperty.getContextAnnotation(Sensitive.class);
- }
- if (sensitive != null) {
- return new SensitiveSerialize(sensitive.type(), sensitive.prefixNoMaskLen(),
- sensitive.suffixNoMaskLen(), sensitive.maskStr());
- }
- }
- return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
- }
- return serializerProvider.findNullValueSerializer(null);
- }
-
-}
diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveTypeEnum.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveTypeEnum.java
deleted file mode 100644
index 3ffda7ee0..000000000
--- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveTypeEnum.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright (c) 2018-2025, lengleng 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 pig4cloud.com 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: lengleng (wangiegie@gmail.com)
- */
-
-package com.pig4cloud.pigx.common.core.sensitive;
-
-/**
- * 敏感信息枚举类
- *
- * @author mayee
- * @version v1.0
- **/
-public enum SensitiveTypeEnum {
-
- /**
- * 自定义
- */
- CUSTOMER,
- /**
- * 用户名, 刘*华, 徐*
- */
- CHINESE_NAME,
- /**
- * 身份证号, 110110********1234
- */
- ID_CARD,
- /**
- * 座机号, ****1234
- */
- FIXED_PHONE,
- /**
- * 手机号, 176****1234
- */
- MOBILE_PHONE,
- /**
- * 地址, 北京********
- */
- ADDRESS,
- /**
- * 电子邮件, s*****o@xx.com
- */
- EMAIL,
- /**
- * 银行卡, 622202************1234
- */
- BANK_CARD,
- /**
- * 密码, 永远是 ******, 与长度无关
- */
- PASSWORD,
- /**
- * 密钥, 【密钥】密钥除了最后三位其他都是***, 与长度无关
- */
- KEY,
- /**
- * IPV4 类型 113.123.198.176 主机部分打码 113.123.*.123
- */
- IPV4,
-
- /**
- * 中国大陆车牌,包含普通车辆、新能源车辆
- */
- CAR_LICENSE
-
-}
diff --git a/pigx-common/pigx-common-sensitive/pom.xml b/pigx-common/pigx-common-sensitive/pom.xml
new file mode 100644
index 000000000..cab910896
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/pom.xml
@@ -0,0 +1,64 @@
+
+
+
+
+ 4.0.0
+
+ com.pig4cloud
+ pigx-common
+ 5.5.0
+
+
+ pigx-common-sensitive
+ jar
+
+ pigx 输出脱敏工具类
+
+
+
+
+
+ org.springframework
+ spring-webmvc
+ provided
+
+
+ cn.hutool
+ hutool-core
+
+
+ cn.hutool
+ hutool-extra
+
+
+ com.fasterxml.jackson.core
+ jackson-core
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+
+
+ org.springframework.security
+ spring-security-core
+
+
+
diff --git a/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/SensitiveAutoConfiguration.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/SensitiveAutoConfiguration.java
new file mode 100644
index 000000000..5f7665087
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/SensitiveAutoConfiguration.java
@@ -0,0 +1,48 @@
+package com.pig4cloud.pigx.common.sensitive;
+
+import com.pig4cloud.pigx.common.sensitive.core.SensitiveService;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
+
+import java.util.Objects;
+
+/**
+ * 脱敏配置类
+ *
+ * @author lengleng
+ * @date 2024/6/27
+ */
+@AutoConfiguration
+@Configuration(proxyBeanMethods = false)
+public class SensitiveAutoConfiguration {
+ /**
+ * 不脱敏的权限
+ */
+ public static String NO_MASK = "no_mask";
+
+
+ /**
+ * 注入默认的脱敏权限判断
+ *
+ * @return SensitiveService
+ */
+ @Bean
+ @ConditionalOnMissingBean
+ public SensitiveService sensitiveService() {
+ return sensitive -> {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ if (Objects.isNull(authentication)) {
+ return true;
+ }
+
+ // 判断角色是否包含 no_mask 的权限
+ return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority)
+ .noneMatch(s -> s.equals(NO_MASK));
+ };
+ }
+}
diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/Sensitive.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/annotation/Sensitive.java
similarity index 90%
rename from pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/Sensitive.java
rename to pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/annotation/Sensitive.java
index a78f4422a..68a2719a9 100644
--- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/Sensitive.java
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/annotation/Sensitive.java
@@ -15,10 +15,12 @@
* Author: lengleng (wangiegie@gmail.com)
*/
-package com.pig4cloud.pigx.common.core.sensitive;
+package com.pig4cloud.pigx.common.sensitive.annotation;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.pig4cloud.pigx.common.sensitive.core.SensitiveSerialize;
+import com.pig4cloud.pigx.common.sensitive.core.SensitiveTypeEnum;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
diff --git a/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveSerialize.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveSerialize.java
new file mode 100644
index 000000000..a5649d665
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveSerialize.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2018-2025, lengleng 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 pig4cloud.com 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: lengleng (wangiegie@gmail.com)
+ */
+
+package com.pig4cloud.pigx.common.sensitive.core;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.BeanProperty;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.ContextualSerializer;
+import com.pig4cloud.pigx.common.sensitive.annotation.Sensitive;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.Objects;
+
+/**
+ * @author lengleng
+ * @date 2019-08-13
+ *
+ * 脱敏序列化
+ */
+@Slf4j
+@NoArgsConstructor
+@AllArgsConstructor
+public class SensitiveSerialize extends JsonSerializer implements ContextualSerializer {
+
+ private Sensitive sensitive;
+
+ @Override
+ public void serialize(final String origin, final JsonGenerator jsonGenerator,
+ final SerializerProvider serializerProvider) throws IOException {
+
+ if (StrUtil.isBlank(origin)) {
+ jsonGenerator.writeString(origin);
+ return;
+ }
+
+ // 判断是否有可以查看原文
+ SensitiveService sensitiveService = SpringUtil.getBean(SensitiveService.class);
+ if (!sensitiveService.isSensitive(sensitive)) {
+ jsonGenerator.writeString(origin);
+ return;
+ }
+
+ // 根据配置注解获取脱敏类型并处理
+ String result = sensitive.type().getStrategy().apply(sensitive, origin);
+ jsonGenerator.writeString(result);
+ }
+
+ @Override
+ public JsonSerializer> createContextual(final SerializerProvider serializerProvider,
+ final BeanProperty beanProperty) throws JsonMappingException {
+ if (beanProperty != null) {
+ if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
+ Sensitive sensitive = beanProperty.getAnnotation(Sensitive.class);
+ if (sensitive == null) {
+ sensitive = beanProperty.getContextAnnotation(Sensitive.class);
+ }
+ if (sensitive != null) {
+ this.sensitive = sensitive;
+ return this;
+ }
+ }
+ return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
+ }
+ return serializerProvider.findNullValueSerializer(null);
+ }
+
+}
diff --git a/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveService.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveService.java
new file mode 100644
index 000000000..399f9ce35
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveService.java
@@ -0,0 +1,16 @@
+package com.pig4cloud.pigx.common.sensitive.core;
+
+import com.pig4cloud.pigx.common.sensitive.annotation.Sensitive;
+
+/**
+ * 脱敏逻辑增强处理
+ *
+ * @author lengleng
+ * @date 2024/6/27
+ */
+public interface SensitiveService {
+ /**
+ * 是否脱敏
+ */
+ boolean isSensitive(Sensitive sensitive);
+}
diff --git a/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveStrategyFunction.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveStrategyFunction.java
new file mode 100644
index 000000000..528176122
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveStrategyFunction.java
@@ -0,0 +1,11 @@
+package com.pig4cloud.pigx.common.sensitive.core;
+
+/**
+ * @author lengleng
+ * @date 2024/6/27
+ */
+@FunctionalInterface
+public interface SensitiveStrategyFunction {
+
+ R apply(T t, U u);
+}
diff --git a/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveTypeEnum.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveTypeEnum.java
new file mode 100644
index 000000000..4b0b3b0d6
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/core/SensitiveTypeEnum.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2018-2025, lengleng 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 pig4cloud.com 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: lengleng (wangiegie@gmail.com)
+ */
+
+package com.pig4cloud.pigx.common.sensitive.core;
+
+import cn.hutool.core.util.DesensitizedUtil;
+import com.pig4cloud.pigx.common.sensitive.annotation.Sensitive;
+import com.pig4cloud.pigx.common.sensitive.util.DesensitizedUtils;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+/**
+ * 敏感信息枚举类
+ *
+ * @author mayee
+ * @version v1.0
+ **/
+@RequiredArgsConstructor
+public enum SensitiveTypeEnum {
+
+ /**
+ * 自定义
+ */
+ CUSTOMER((sensitive, input) -> DesensitizedUtils.desValue(input, sensitive.prefixNoMaskLen(), sensitive.suffixNoMaskLen(), sensitive.maskStr())),
+ /**
+ * 用户名, 刘*华, 徐*
+ */
+ CHINESE_NAME((sensitive, input) -> DesensitizedUtils.chineseName(input)),
+ /**
+ * 身份证号, 110110********1234
+ */
+ ID_CARD((sensitive, input) -> DesensitizedUtils.idCardNum(input)),
+ /**
+ * 座机号, ****1234
+ */
+ FIXED_PHONE((sensitive, input) -> DesensitizedUtils.fixedPhone(input)),
+ /**
+ * 手机号, 176****1234
+ */
+ MOBILE_PHONE((sensitive, input) -> DesensitizedUtils.mobilePhone(input)),
+ /**
+ * 地址, 北京********
+ */
+ ADDRESS((sensitive, input) -> DesensitizedUtils.address(input)),
+ /**
+ * 电子邮件, s*****o@xx.com
+ */
+ EMAIL((sensitive, input) -> DesensitizedUtils.email(input)),
+ /**
+ * 银行卡, 622202************1234
+ */
+ BANK_CARD((sensitive, input) -> DesensitizedUtils.bankCard(input)),
+ /**
+ * 密码, 永远是 ******, 与长度无关
+ */
+ PASSWORD((sensitive, input) -> DesensitizedUtils.password(input)),
+ /**
+ * 密钥, 【密钥】密钥除第一位其他都是***, 与长度无关
+ */
+ KEY((sensitive, input) -> DesensitizedUtils.key(input)),
+ /**
+ * IPV4 类型 113.123.198.176 主机部分打码 113.123.*.123
+ */
+ IPV4((sensitive, input) -> DesensitizedUtils.ipv4(input)),
+
+ /**
+ * 中国大陆车牌,包含普通车辆、新能源车辆
+ */
+ CAR_LICENSE((sensitive, input) -> DesensitizedUtil.carLicense(input));
+
+
+ /**
+ * 获取脱敏策略
+ */
+ @Getter
+ private final SensitiveStrategyFunction strategy;
+
+}
diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/util/DesensitizedUtils.java
similarity index 98%
rename from pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java
rename to pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/util/DesensitizedUtils.java
index 87665dcd3..6f50ba602 100644
--- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java
+++ b/pigx-common/pigx-common-sensitive/src/main/java/com/pig4cloud/pigx/common/sensitive/util/DesensitizedUtils.java
@@ -15,7 +15,7 @@
* Author: lengleng (wangiegie@gmail.com)
*/
-package com.pig4cloud.pigx.common.core.util;
+package com.pig4cloud.pigx.common.sensitive.util;
import cn.hutool.core.util.StrUtil;
diff --git a/pigx-common/pigx-common-sensitive/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/pigx-common/pigx-common-sensitive/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 000000000..000496c98
--- /dev/null
+++ b/pigx-common/pigx-common-sensitive/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+com.pig4cloud.pigx.common.sensitive.SensitiveAutoConfiguration
diff --git a/pigx-common/pom.xml b/pigx-common/pom.xml
index 82dc9f322..46a61d553 100644
--- a/pigx-common/pom.xml
+++ b/pigx-common/pom.xml
@@ -49,6 +49,7 @@
pigx-common-oss
pigx-common-seata
pigx-common-security
+ pigx-common-sensitive
pigx-common-sentinel
pigx-common-sequence
pigx-common-swagger
diff --git a/pigx-upms/pigx-upms-api/pom.xml b/pigx-upms/pigx-upms-api/pom.xml
index 3e8c27c51..7f553deb2 100644
--- a/pigx-upms/pigx-upms-api/pom.xml
+++ b/pigx-upms/pigx-upms-api/pom.xml
@@ -64,5 +64,10 @@
org.javers
javers-core
+
+