-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
disjob-admin/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Sensitive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ruoyi.common.annotation; | ||
|
||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.ruoyi.common.config.serializer.SensitiveJsonSerializer; | ||
import com.ruoyi.common.enums.DesensitizedType; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* 数据脱敏注解 | ||
* | ||
* @author ruoyi | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
@JacksonAnnotationsInside | ||
@JsonSerialize(using = SensitiveJsonSerializer.class) | ||
public @interface Sensitive | ||
{ | ||
DesensitizedType desensitizedType(); | ||
} |
65 changes: 65 additions & 0 deletions
65
...uoyi-common/src/main/java/com/ruoyi/common/config/serializer/SensitiveJsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.ruoyi.common.config.serializer; | ||
|
||
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.ruoyi.common.annotation.Sensitive; | ||
import com.ruoyi.common.core.domain.entity.SysUser; | ||
import com.ruoyi.common.enums.DesensitizedType; | ||
import com.ruoyi.common.utils.ShiroUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* 数据脱敏序列化过滤 | ||
* | ||
* @author ruoyi | ||
*/ | ||
public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer | ||
{ | ||
private DesensitizedType desensitizedType; | ||
|
||
@Override | ||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException | ||
{ | ||
if (desensitization()) | ||
{ | ||
gen.writeString(desensitizedType.desensitizer().apply(value)); | ||
} | ||
else | ||
{ | ||
gen.writeString(value); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) | ||
throws JsonMappingException | ||
{ | ||
Sensitive annotation = property.getAnnotation(Sensitive.class); | ||
if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) | ||
{ | ||
this.desensitizedType = annotation.desensitizedType(); | ||
return this; | ||
} | ||
return prov.findValueSerializer(property.getType(), property); | ||
} | ||
|
||
/** | ||
* 是否需要脱敏处理 | ||
*/ | ||
private boolean desensitization() | ||
{ | ||
SysUser securityUser = ShiroUtils.getSysUser(); | ||
if (securityUser == null) | ||
{ | ||
return true; | ||
} | ||
// 管理员不脱敏 | ||
return !securityUser.isAdmin(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
disjob-admin/ruoyi-common/src/main/java/com/ruoyi/common/enums/DesensitizedType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ruoyi.common.enums; | ||
|
||
import com.ruoyi.common.utils.DesensitizedUtil; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* 脱敏类型 | ||
* | ||
* @author ruoyi | ||
*/ | ||
public enum DesensitizedType | ||
{ | ||
/** | ||
* 姓名,第2位星号替换 | ||
*/ | ||
USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")), | ||
|
||
/** | ||
* 密码,全部字符都用*代替 | ||
*/ | ||
PASSWORD(DesensitizedUtil::password), | ||
|
||
/** | ||
* 身份证,中间10位星号替换 | ||
*/ | ||
ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1** **** ****$2")), | ||
|
||
/** | ||
* 手机号,中间4位星号替换 | ||
*/ | ||
PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")), | ||
|
||
/** | ||
* 电子邮箱,仅显示第一个字母和@后面的地址显示,其他星号替换 | ||
*/ | ||
EMAIL(s -> s.replaceAll("(^.)[^@]*(@.*$)", "$1****$2")), | ||
|
||
/** | ||
* 银行卡号,保留最后4位,其他星号替换 | ||
*/ | ||
BANK_CARD(s -> s.replaceAll("\\d{15}(\\d{3})", "**** **** **** **** $1")), | ||
|
||
/** | ||
* 车牌号码,包含普通车辆、新能源车辆 | ||
*/ | ||
CAR_LICENSE(DesensitizedUtil::carLicense); | ||
|
||
private final Function<String, String> desensitizer; | ||
|
||
DesensitizedType(Function<String, String> desensitizer) | ||
{ | ||
this.desensitizer = desensitizer; | ||
} | ||
|
||
public Function<String, String> desensitizer() | ||
{ | ||
return desensitizer; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
disjob-admin/ruoyi-common/src/main/java/com/ruoyi/common/utils/DesensitizedUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.ruoyi.common.utils; | ||
|
||
/** | ||
* 脱敏工具类 | ||
* | ||
* @author ruoyi | ||
*/ | ||
public class DesensitizedUtil | ||
{ | ||
/** | ||
* 密码的全部字符都用*代替,比如:****** | ||
* | ||
* @param password 密码 | ||
* @return 脱敏后的密码 | ||
*/ | ||
public static String password(String password) | ||
{ | ||
if (StringUtils.isBlank(password)) | ||
{ | ||
return StringUtils.EMPTY; | ||
} | ||
return StringUtils.repeat('*', password.length()); | ||
} | ||
|
||
/** | ||
* 车牌中间用*代替,如果是错误的车牌,不处理 | ||
* | ||
* @param carLicense 完整的车牌号 | ||
* @return 脱敏后的车牌 | ||
*/ | ||
public static String carLicense(String carLicense) | ||
{ | ||
if (StringUtils.isBlank(carLicense)) | ||
{ | ||
return StringUtils.EMPTY; | ||
} | ||
// 普通车牌 | ||
if (carLicense.length() == 7) | ||
{ | ||
carLicense = StringUtils.hide(carLicense, 3, 6); | ||
} | ||
else if (carLicense.length() == 8) | ||
{ | ||
// 新能源车牌 | ||
carLicense = StringUtils.hide(carLicense, 3, 7); | ||
} | ||
return carLicense; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters