完善脱敏组件的注释
This commit is contained in:
parent
d5e54288c1
commit
b43813ab96
@ -29,6 +29,7 @@ public @interface SliderDesensitize {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换规则,会将前缀后缀保留后,全部替换成 replacer
|
* 替换规则,会将前缀后缀保留后,全部替换成 replacer
|
||||||
|
*
|
||||||
* 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*";
|
* 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*";
|
||||||
* 原始字符串 123456
|
* 原始字符串 123456
|
||||||
* 脱敏后 1***56
|
* 脱敏后 1***56
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 滑动脱敏处理器抽象类,已实现通用的方法
|
||||||
|
*
|
||||||
|
* @author gaibu
|
||||||
|
*/
|
||||||
|
public abstract class AbstractSliderDesensitizationHandler<T extends Annotation>
|
||||||
|
implements DesensitizationHandler<T> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String desensitize(String origin, T annotation) {
|
||||||
|
int prefixKeep = getPrefixKeep(annotation);
|
||||||
|
int suffixKeep = getSuffixKeep(annotation);
|
||||||
|
String replacer = getReplacer(annotation);
|
||||||
|
int length = origin.length();
|
||||||
|
|
||||||
|
// 情况一:原始字符串长度小于等于保留长度,则原始字符串全部替换
|
||||||
|
if (prefixKeep >= length || suffixKeep >= length) {
|
||||||
|
return buildReplacerByLength(replacer, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 情况二:原始字符串长度小于等于前后缀保留字符串长度,则原始字符串全部替换
|
||||||
|
if ((prefixKeep + suffixKeep) >= length) {
|
||||||
|
return buildReplacerByLength(replacer, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 情况三:原始字符串长度大于前后缀保留字符串长度,则替换中间字符串
|
||||||
|
int interval = length - prefixKeep - suffixKeep;
|
||||||
|
return origin.substring(0, prefixKeep) +
|
||||||
|
buildReplacerByLength(replacer, interval) +
|
||||||
|
origin.substring(prefixKeep + interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据长度循环构建替换符
|
||||||
|
*
|
||||||
|
* @param replacer 替换符
|
||||||
|
* @param length 长度
|
||||||
|
* @return 构建后的替换符
|
||||||
|
*/
|
||||||
|
private String buildReplacerByLength(String replacer, int length) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
builder.append(replacer);
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前缀保留长度
|
||||||
|
*
|
||||||
|
* @param annotation 注解信息
|
||||||
|
* @return 前缀保留长度
|
||||||
|
*/
|
||||||
|
abstract Integer getPrefixKeep(T annotation);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后缀保留长度
|
||||||
|
*
|
||||||
|
* @param annotation 注解信息
|
||||||
|
* @return 后缀保留长度
|
||||||
|
*/
|
||||||
|
abstract Integer getSuffixKeep(T annotation);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 替换符
|
||||||
|
*
|
||||||
|
* @param annotation 注解信息
|
||||||
|
* @return 替换符
|
||||||
|
*/
|
||||||
|
abstract String getReplacer(T annotation);
|
||||||
|
|
||||||
|
}
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCardDes
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class BankCardDesensitization extends AbstractDesensitizationHandler<BankCardDesensitize> {
|
public class BankCardDesensitization extends AbstractSliderDesensitizationHandler<BankCardDesensitize> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(BankCardDesensitize annotation) {
|
Integer getPrefixKeep(BankCardDesensitize annotation) {
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicenseD
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class CarLicenseDesensitization extends AbstractDesensitizationHandler<CarLicenseDesensitize> {
|
public class CarLicenseDesensitization extends AbstractSliderDesensitizationHandler<CarLicenseDesensitize> {
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(CarLicenseDesensitize annotation) {
|
Integer getPrefixKeep(CarLicenseDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseNameDesensitize> {
|
public class ChineseNameDesensitization extends AbstractSliderDesensitizationHandler<ChineseNameDesensitize> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(ChineseNameDesensitize annotation) {
|
Integer getPrefixKeep(ChineseNameDesensitize annotation) {
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesen
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler<SliderDesensitize> {
|
public class DefaultDesensitizationHandler extends AbstractSliderDesensitizationHandler<SliderDesensitize> {
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(SliderDesensitize annotation) {
|
Integer getPrefixKeep(SliderDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhoneD
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class FixedPhoneDesensitization extends AbstractDesensitizationHandler<FixedPhoneDesensitize> {
|
public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHandler<FixedPhoneDesensitize> {
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(FixedPhoneDesensitize annotation) {
|
Integer getPrefixKeep(FixedPhoneDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCardDesen
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class IdCardDesensitization extends AbstractDesensitizationHandler<IdCardDesensitize> {
|
public class IdCardDesensitization extends AbstractSliderDesensitizationHandler<IdCardDesensitize> {
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(IdCardDesensitize annotation) {
|
Integer getPrefixKeep(IdCardDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.MobileDesen
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class MobileDesensitization extends AbstractDesensitizationHandler<MobileDesensitize> {
|
public class MobileDesensitization extends AbstractSliderDesensitizationHandler<MobileDesensitize> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(MobileDesensitize annotation) {
|
Integer getPrefixKeep(MobileDesensitize annotation) {
|
||||||
|
@ -7,7 +7,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PasswordDes
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class PasswordDesensitization extends AbstractDesensitizationHandler<PasswordDesensitize> {
|
public class PasswordDesensitization extends AbstractSliderDesensitizationHandler<PasswordDesensitize> {
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(PasswordDesensitize annotation) {
|
Integer getPrefixKeep(PasswordDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
|
Loading…
Reference in New Issue
Block a user