日志脱敏
This commit is contained in:
@@ -0,0 +1,84 @@
|
|||||||
|
package com.orion.ops.framework.common.utils;
|
||||||
|
|
||||||
|
import com.orion.lang.utils.Strings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脱敏工具类
|
||||||
|
* <p>
|
||||||
|
* // FIXME KIT
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2019/9/10 9:45
|
||||||
|
*/
|
||||||
|
public class Desensitizes {
|
||||||
|
|
||||||
|
public static final String REPLACER = "*";
|
||||||
|
|
||||||
|
public static final char REPLACER_CHAR = '*';
|
||||||
|
|
||||||
|
private Desensitizes() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String mix(String s, int keepStart, int keepEnd) {
|
||||||
|
return mix(s, keepStart, keepEnd, REPLACER_CHAR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串脱敏
|
||||||
|
* 脱敏后的长度和原先的长度一样
|
||||||
|
*
|
||||||
|
* @param s 原字符
|
||||||
|
* @param keepStart 开始保留长度
|
||||||
|
* @param keepEnd 结束保留长度
|
||||||
|
* @param replacer 脱敏字符
|
||||||
|
* @return 脱敏字符串
|
||||||
|
*/
|
||||||
|
public static String mix(String s, int keepStart, int keepEnd, char replacer) {
|
||||||
|
int length = Strings.length(s);
|
||||||
|
if (length == 0) {
|
||||||
|
return Strings.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mix(s, keepStart, keepEnd, Strings.repeat(replacer, length - keepStart - keepEnd), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String mix(String s, int keepStart, int keepEnd, String replacer) {
|
||||||
|
return mix(s, keepStart, keepEnd, replacer, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串脱敏
|
||||||
|
*
|
||||||
|
* @param s 原字符
|
||||||
|
* @param keepStart 开始保留长度
|
||||||
|
* @param keepEnd 结束保留长度
|
||||||
|
* @param replacer 脱敏字符串
|
||||||
|
* @param repeat 脱敏字符串重复次数
|
||||||
|
* @return 脱敏字符串
|
||||||
|
*/
|
||||||
|
public static String mix(String s, int keepStart, int keepEnd, String replacer, int repeat) {
|
||||||
|
int length = Strings.length(s);
|
||||||
|
if (length == 0) {
|
||||||
|
return Strings.EMPTY;
|
||||||
|
}
|
||||||
|
if (keepStart < 0) {
|
||||||
|
keepStart = 0;
|
||||||
|
}
|
||||||
|
if (keepEnd < 0) {
|
||||||
|
keepEnd = 0;
|
||||||
|
}
|
||||||
|
// 保留的长度大于等于文本的长度则不脱敏
|
||||||
|
if (keepStart + keepEnd >= length) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
char[] chars = s.toCharArray();
|
||||||
|
char[] replacerArr = Strings.repeat(replacer, repeat).toCharArray();
|
||||||
|
char[] res = new char[keepStart + keepEnd + replacerArr.length];
|
||||||
|
System.arraycopy(chars, 0, res, 0, keepStart);
|
||||||
|
System.arraycopy(replacerArr, 0, res, keepStart, replacerArr.length);
|
||||||
|
System.arraycopy(chars, chars.length - keepEnd, res, keepStart + replacerArr.length, keepEnd);
|
||||||
|
return new String(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -53,7 +53,7 @@ public class CacheQuery<T> {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <R> R get(Class<R> c) {
|
public <R> R get(Class<R> c) {
|
||||||
T row = this.get();
|
T row = this.get();
|
||||||
// TODO FIXME mapstruct
|
// TODO mapstruct
|
||||||
return (R) row;
|
return (R) row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,14 +23,14 @@ public class LogPrinterFieldConfig {
|
|||||||
/**
|
/**
|
||||||
* 脱敏的字段
|
* 脱敏的字段
|
||||||
*/
|
*/
|
||||||
private List<String> desensitization;
|
private List<String> desensitize;
|
||||||
|
|
||||||
public void setIgnore(List<String> ignore) {
|
public void setIgnore(List<String> ignore) {
|
||||||
this.ignore = Utils.parseStringList(ignore);
|
this.ignore = Utils.parseStringList(ignore);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDesensitization(List<String> desensitization) {
|
public void setDesensitize(List<String> desensitize) {
|
||||||
this.desensitization = Utils.parseStringList(desensitization);
|
this.desensitize = Utils.parseStringList(desensitize);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import com.orion.ops.framework.common.annotation.IgnoreLog;
|
|||||||
import com.orion.ops.framework.common.constant.Const;
|
import com.orion.ops.framework.common.constant.Const;
|
||||||
import com.orion.ops.framework.common.meta.TraceIdHolder;
|
import com.orion.ops.framework.common.meta.TraceIdHolder;
|
||||||
import com.orion.ops.framework.web.core.config.LogPrinterConfig;
|
import com.orion.ops.framework.web.core.config.LogPrinterConfig;
|
||||||
|
import com.orion.ops.framework.common.utils.Desensitizes;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import org.aopalliance.intercept.MethodInvocation;
|
import org.aopalliance.intercept.MethodInvocation;
|
||||||
|
|
||||||
@@ -74,17 +75,14 @@ public abstract class BaseLogPrinterInterceptor implements LogPrinterInterceptor
|
|||||||
// 忽略字段过滤器
|
// 忽略字段过滤器
|
||||||
PropertyFilter ignoreFilter = (Object object, String name, Object value) -> !config.getField().getIgnore().contains(name);
|
PropertyFilter ignoreFilter = (Object object, String name, Object value) -> !config.getField().getIgnore().contains(name);
|
||||||
// 脱敏字段过滤器
|
// 脱敏字段过滤器
|
||||||
ValueFilter desensitizationFilter = (Object object, String name, Object value) -> {
|
ValueFilter desensitizeFilter = (Object object, String name, Object value) -> {
|
||||||
if (config.getField().getDesensitization().contains(name)) {
|
if (config.getField().getDesensitize().contains(name)) {
|
||||||
String s = Objects1.toString(value);
|
return Desensitizes.mix(Objects1.toString(value), 1, 1);
|
||||||
// Safes.mix()
|
|
||||||
// TODO
|
|
||||||
return "xxxxxx";
|
|
||||||
} else {
|
} else {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this.fieldFilter = new SerializeFilter[]{ignoreFilter, desensitizationFilter};
|
this.fieldFilter = new SerializeFilter[]{ignoreFilter, desensitizeFilter};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
"description": "忽略打印的字段."
|
"description": "忽略打印的字段."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "logging.printer.field.desensitization",
|
"name": "logging.printer.field.desensitize",
|
||||||
"type": "java.util.List",
|
"type": "java.util.List",
|
||||||
"description": "需要脱敏的字段."
|
"description": "需要脱敏的字段."
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.orion.ops.launch.controller;
|
package com.orion.ops.launch.controller;
|
||||||
|
|
||||||
import com.orion.ops.framework.common.annotation.RestWrapper;
|
import com.orion.ops.framework.common.annotation.RestWrapper;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RequestMapping("/server/bootstrap")
|
@RequestMapping("/server/bootstrap")
|
||||||
public class BootstrapController {
|
public class BootstrapController {
|
||||||
|
|
||||||
|
@Operation(summary = "检测心跳")
|
||||||
@GetMapping("/health")
|
@GetMapping("/health")
|
||||||
public String health() {
|
public String health() {
|
||||||
return "server ok";
|
return "server ok";
|
||||||
|
|||||||
@@ -32,3 +32,7 @@ springdoc:
|
|||||||
|
|
||||||
knife4j:
|
knife4j:
|
||||||
enable: false
|
enable: false
|
||||||
|
|
||||||
|
logging:
|
||||||
|
printer:
|
||||||
|
mode: ROW
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ logging:
|
|||||||
ignore:
|
ignore:
|
||||||
- password,newPassword
|
- password,newPassword
|
||||||
- metrics
|
- metrics
|
||||||
desensitization:
|
desensitize:
|
||||||
- phone,phoneNumber
|
- phone,phoneNumber
|
||||||
- email,sendEmail
|
- email,sendEmail
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user