-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6332 from EightMonth/master
优化数据脱敏功能
- Loading branch information
Showing
5 changed files
with
107 additions
and
13 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...oot-base-core/src/main/java/org/jeecg/common/desensitization/SensitiveFieldSerialize.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,87 @@ | ||
package org.jeecg.common.desensitization; | ||
|
||
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 lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jeecg.common.desensitization.annotation.SensitiveField; | ||
import org.jeecg.common.desensitization.enums.SensitiveEnum; | ||
import org.jeecg.common.desensitization.util.SensitiveInfoUtil; | ||
import org.jeecg.common.util.encryption.AesEncryptUtil; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/6/19 10:43 | ||
*/ | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class SensitiveFieldSerialize extends JsonSerializer<String> implements ContextualSerializer { | ||
|
||
private SensitiveEnum type; | ||
|
||
@Override | ||
public void serialize(String data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | ||
switch (type){ | ||
case ENCODE: | ||
try { | ||
jsonGenerator.writeString(AesEncryptUtil.encrypt(data)); | ||
} catch (Exception exception) { | ||
log.error("数据加密错误", exception.getMessage()); | ||
jsonGenerator.writeString(data); | ||
} | ||
break; | ||
case CHINESE_NAME: | ||
jsonGenerator.writeString(SensitiveInfoUtil.chineseName(data)); | ||
break; | ||
case ID_CARD: | ||
jsonGenerator.writeString(SensitiveInfoUtil.idCardNum(data)); | ||
break; | ||
case FIXED_PHONE: | ||
jsonGenerator.writeString(SensitiveInfoUtil.fixedPhone(data)); | ||
break; | ||
case MOBILE_PHONE: | ||
jsonGenerator.writeString(SensitiveInfoUtil.mobilePhone(data)); | ||
break; | ||
case ADDRESS: | ||
jsonGenerator.writeString(SensitiveInfoUtil.address(data, 3)); | ||
break; | ||
case EMAIL: | ||
jsonGenerator.writeString(SensitiveInfoUtil.email(data)); | ||
break; | ||
case BANK_CARD: | ||
jsonGenerator.writeString(SensitiveInfoUtil.bankCard(data)); | ||
break; | ||
case CNAPS_CODE: | ||
jsonGenerator.writeString(SensitiveInfoUtil.cnapsCode(data)); | ||
break; | ||
default: | ||
jsonGenerator.writeString(data); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { | ||
if (beanProperty != null) { | ||
if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) { | ||
SensitiveField sensitive = beanProperty.getAnnotation(SensitiveField.class); | ||
if (sensitive == null) { | ||
sensitive = beanProperty.getContextAnnotation(SensitiveField.class); | ||
} | ||
if (sensitive != null) { | ||
return new SensitiveFieldSerialize(sensitive.type()); | ||
} | ||
} | ||
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty); | ||
} | ||
return serializerProvider.findNullValueSerializer(null); | ||
} | ||
} |
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
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
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
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