-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat-IDesensitizationAbility-实现数据脱敏相关算法
- Loading branch information
Showing
7 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
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
87 changes: 87 additions & 0 deletions
87
my-boot/src/test/java/net/ximatai/muyun/test/core/TestDesensitizationAbility.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 net.ximatai.muyun.test.core; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.Path; | ||
import net.ximatai.muyun.ability.IDesensitizationAbility; | ||
import net.ximatai.muyun.ability.ITableCreateAbility; | ||
import net.ximatai.muyun.ability.curd.std.ICURDAbility; | ||
import net.ximatai.muyun.core.Scaffold; | ||
import net.ximatai.muyun.core.desensitization.Desensitizer; | ||
import net.ximatai.muyun.core.desensitization.MaskMiddleAlgorithm; | ||
import net.ximatai.muyun.core.security.SMEncryptor; | ||
import net.ximatai.muyun.database.IDatabaseAccess; | ||
import net.ximatai.muyun.database.builder.Column; | ||
import net.ximatai.muyun.database.builder.TableWrapper; | ||
import net.ximatai.muyun.test.testcontainers.PostgresTestResource; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) | ||
class TestDesensitizationAbility { | ||
|
||
private String path = "/TestSecurityAbility"; | ||
|
||
@Inject | ||
SMEncryptor smEncryptor; | ||
|
||
@Inject | ||
IDatabaseAccess databaseAccess; | ||
|
||
@Inject | ||
TestDesensitizationAbilityController testController; | ||
|
||
@Test | ||
void test() { | ||
String text = "hello world!"; | ||
String id = testController.create(Map.of( | ||
"v_name", text | ||
)); | ||
Map<String, ?> response = testController.view(id); | ||
|
||
String responseVName = (String) response.get("v_name"); | ||
assertEquals(text.length(), responseVName.length()); | ||
assertNotEquals(text, responseVName); | ||
assertEquals("h**********!", responseVName); | ||
assertNull(response.get("v_name2")); | ||
} | ||
|
||
} | ||
|
||
@Path("/TestDesensitizationAbility") | ||
class TestDesensitizationAbilityController extends Scaffold implements ICURDAbility, ITableCreateAbility, IDesensitizationAbility { | ||
|
||
@Inject | ||
SMEncryptor smEncryptor; | ||
|
||
@Override | ||
public String getSchemaName() { | ||
return "test"; | ||
} | ||
|
||
@Override | ||
public String getMainTable() { | ||
return "testdesensitizationability"; | ||
} | ||
|
||
@Override | ||
public TableWrapper fitOutTable() { | ||
return TableWrapper.withName(getMainTable()) | ||
.setSchema(getSchemaName()) | ||
.setPrimaryKey(Column.ID_POSTGRES) | ||
.addColumn(Column.of("v_name").setType("varchar")) | ||
.addColumn(Column.of("v_name2").setType("varchar")) | ||
.addColumn(Column.of("t_create").setDefaultValue("now()")); | ||
} | ||
|
||
|
||
@Override | ||
public Desensitizer getDesensitizer() { | ||
return new Desensitizer().registerAlgorithm("v_name", new MaskMiddleAlgorithm()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
my-core/src/main/java/net/ximatai/muyun/ability/IDesensitizationAbility.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,17 @@ | ||
package net.ximatai.muyun.ability; | ||
|
||
import net.ximatai.muyun.core.desensitization.Desensitizer; | ||
|
||
import java.util.Map; | ||
|
||
public interface IDesensitizationAbility { | ||
|
||
Desensitizer getDesensitizer(); | ||
|
||
default void desensitize(Map<String, Object> map) { | ||
Desensitizer desensitizer = getDesensitizer(); | ||
if (desensitizer == null) return; | ||
|
||
map.replaceAll((k, v) -> v != null ? desensitizer.desensitize(k, v.toString()) : 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
45 changes: 45 additions & 0 deletions
45
my-core/src/main/java/net/ximatai/muyun/core/desensitization/Desensitizer.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,45 @@ | ||
package net.ximatai.muyun.core.desensitization; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Desensitizer { | ||
private final Map<String, IDesensitizationAlgorithm> columnAlgorithmMap = new HashMap<>(); | ||
|
||
/** | ||
* 注册列名及对应的脱敏算法 | ||
* | ||
* @param columnName 列名 | ||
* @param algorithm 脱敏算法 | ||
*/ | ||
public Desensitizer registerAlgorithm(String columnName, IDesensitizationAlgorithm algorithm) { | ||
columnAlgorithmMap.put(columnName, algorithm); | ||
return this; | ||
} | ||
|
||
/** | ||
* 根据列名获取对应的脱敏算法 | ||
* | ||
* @param columnName 列名 | ||
* @return 对应的脱敏算法 | ||
*/ | ||
public IDesensitizationAlgorithm getAlgorithm(String columnName) { | ||
return columnAlgorithmMap.get(columnName); | ||
} | ||
|
||
/** | ||
* 根据列名和原始值进行脱敏 | ||
* | ||
* @param columnName 列名 | ||
* @param source 原始数据 | ||
* @return 脱敏后的数据 | ||
*/ | ||
public String desensitize(String columnName, String source) { | ||
IDesensitizationAlgorithm algorithm = getAlgorithm(columnName); | ||
if (algorithm != null) { | ||
return algorithm.desensitize(source); | ||
} | ||
return source; // 没有注册脱敏算法则返回原始数据 | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
my-core/src/main/java/net/ximatai/muyun/core/desensitization/IDesensitizationAlgorithm.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,7 @@ | ||
package net.ximatai.muyun.core.desensitization; | ||
|
||
public interface IDesensitizationAlgorithm { | ||
|
||
String desensitize(String source); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
my-core/src/main/java/net/ximatai/muyun/core/desensitization/MaskMiddleAlgorithm.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,13 @@ | ||
package net.ximatai.muyun.core.desensitization; | ||
|
||
public class MaskMiddleAlgorithm implements IDesensitizationAlgorithm { | ||
@Override | ||
public String desensitize(String source) { | ||
if (source == null || source.length() <= 2) { | ||
return source; // 短字符串不进行脱敏 | ||
} | ||
// 只显示首尾字符,中间使用 * 号代替 | ||
int length = source.length(); | ||
return source.charAt(0) + "*".repeat(length - 2) + source.charAt(length - 1); | ||
} | ||
} |