forked from plusone/plusone-commons
feat: 新增字符串脱敏方法 StringTools#desensitize
This commit is contained in:
parent
aecf467a95
commit
4154fda36e
@ -184,6 +184,42 @@ public class StringTools {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脱敏
|
||||||
|
*
|
||||||
|
* @param src 原字符串
|
||||||
|
* @param front 前面保留的字符数
|
||||||
|
* @param end 后面保留的字符数
|
||||||
|
* @return 脱敏结果
|
||||||
|
* @since 1.1.0
|
||||||
|
*/
|
||||||
|
public static String desensitize(@Nullable final String src, int front, int end) {
|
||||||
|
return desensitize(src, '*', front, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脱敏
|
||||||
|
*
|
||||||
|
* @param src 原字符串
|
||||||
|
* @param replacedChar 用于替换的字符
|
||||||
|
* @param front 前面保留的字符数
|
||||||
|
* @param end 后面保留的字符数
|
||||||
|
* @return 脱敏结果
|
||||||
|
* @since 1.1.0
|
||||||
|
*/
|
||||||
|
public static String desensitize(@Nullable final String src, char replacedChar, int front, int end) {
|
||||||
|
if (src == null || src.isEmpty()) {
|
||||||
|
return EMPTY_STRING;
|
||||||
|
}
|
||||||
|
AssertTools.checkArgument(front >= 0 && end >= 0);
|
||||||
|
AssertTools.checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
|
||||||
|
final char[] charArray = src.toCharArray();
|
||||||
|
for (int i = front; i < charArray.length - end; i++) {
|
||||||
|
charArray[i] = replacedChar;
|
||||||
|
}
|
||||||
|
return String.valueOf(charArray);
|
||||||
|
}
|
||||||
|
|
||||||
private StringTools() {
|
private StringTools() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@SuppressWarnings("null")
|
@SuppressWarnings("null")
|
||||||
@ -184,7 +185,7 @@ class StringToolsTests {
|
|||||||
// #region - isURL
|
// #region - isURL
|
||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TC1: 验证标准HTTP协议URL
|
* TC1: 验证标准HTTP协议URL
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@ -298,6 +299,77 @@ class StringToolsTests {
|
|||||||
// #endregion - repeat
|
// #endregion - repeat
|
||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - desensitize
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_NullInput_ReturnsEmptyString() {
|
||||||
|
String result = StringTools.desensitize(null, '#', 2, 2);
|
||||||
|
Assertions.assertEquals("", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_EmptyString_ReturnsEmptyString() {
|
||||||
|
String result = StringTools.desensitize("", '#', 2, 2);
|
||||||
|
Assertions.assertEquals("", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_NegativeFront_ThrowsException() {
|
||||||
|
Assertions.assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
StringTools.desensitize("123456", '#', -1, 2);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_NegativeEnd_ThrowsException() {
|
||||||
|
Assertions.assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
StringTools.desensitize("123456", '#', 2, -1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_FrontPlusEndExceedsLength_ThrowsException() {
|
||||||
|
Assertions.assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
StringTools.desensitize("123456", '#', 3, 4);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_ValidInput_ReturnsDesensitizedString() {
|
||||||
|
String result = StringTools.desensitize("123456", '#', 2, 2);
|
||||||
|
Assertions.assertEquals("12##56", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_FrontZero_ReturnsDesensitizedString() {
|
||||||
|
String result = StringTools.desensitize("123456", '#', 0, 2);
|
||||||
|
Assertions.assertEquals("####56", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_EndZero_ReturnsDesensitizedString() {
|
||||||
|
String result = StringTools.desensitize("123456", '#', 2, 0);
|
||||||
|
Assertions.assertEquals("12####", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_FrontAndEndZero_ReturnsDesensitizedString() {
|
||||||
|
String result = StringTools.desensitize("123456", '#', 0, 0);
|
||||||
|
Assertions.assertEquals("######", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void desensitize_ValidInput_DefaultReplacedChar_ReturnsDesensitizedString() {
|
||||||
|
String result = StringTools.desensitize("123456", 2, 2);
|
||||||
|
Assertions.assertEquals("12**56", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - desensitize
|
||||||
|
// ================================
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||||
Constructor<?>[] constructors = StringTools.class.getDeclaredConstructors();
|
Constructor<?>[] constructors = StringTools.class.getDeclaredConstructors();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user