当前位置:网站首页>When custom annotations implement log printing, specific fields are blocked from printing
When custom annotations implement log printing, specific fields are blocked from printing
2022-08-01 17:04:00 【Humanoid bug maker 9527】
需求:
It is required that the request parameters of an interface have a field not to be printed in the log
方法1:
The front-end parameters and the back-end are encrypted with the public key and then transmitted,It is decrypted when the back-end business is executed;
方法2:
Custom annotations are masked during log printing
注解
Laziness will not define three kinds here, prepare this one to use it to the end
/** * 跳过BodyRequest parameter log printing */
@Target({
ElementType.METHOD,ElementType.PARAMETER,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SkipBodyParamLogPrint {
}
日志aop的改造
MethodSignature signature = (MethodSignature) pjp.getSignature();
SkipBodyParamLogPrint skipBodyParamLog = signature.getMethod().getDeclaredAnnotation(SkipBodyParamLogPrint.class);
if (ObjectUtil.isNotEmpty(skipBodyParamLog)) {
// Checks are performed if there are request parameters that need to be skipped
args = paramSkipFilter(args);
}
参数过滤方法
/** * Check if you need to skip request parameter printing * * @return */
public Object[] paramSkipFilter(Object[] args) {
Object[] objects = Arrays.stream(args)
.map(
arg -> {
// 获取所有字段
Field[] fields = arg.getClass().getDeclaredFields();
// No fields are skipped
if (fields.length == 0)
return arg;
for (Field field : fields) {
// If the field has this annotation, it means that the field needs to be treated as empty
SkipBodyParamLogPrint[] annotationsByType = field.getDeclaredAnnotationsByType(SkipBodyParamLogPrint.class);
if (annotationsByType.length != 0){
try {
// 拼接方法名
String methodName = "set" + StringUtils.capitalize(field.getName());
// Knowing that there is only one parameter and the parameter is the type of the field itself, it is manually written,Elsewhere you need to use advice to get parameters directly from the array matching names
Method method = arg.getClass().getDeclaredMethod(methodName, field.getType());
// 关闭安全检查
field.setAccessible(true);
// 执行方法,The parameter instantiates an empty object with the field type,The same if other places need to use a similar method,Just get the method from the array and then take the parameters and splicing the array to pass in
method.invoke(arg,field.getType().newInstance());
field.setAccessible(false);
} catch (IllegalAccessException |InvocationTargetException e) {
log.error("When request parameter filteringset方法调用失败",e);
break;
} catch (NoSuchMethodException | InstantiationException e) {
log.error("When request parameter filteringsetMethod instantiation failed",e);
break;
}
}
}
// Returns the arguments after processing
return arg;
}
)
.toArray();
return objects;
}
使用
POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "Purchase request parameters-purchase vip request parameters")
public class BuyGoodsParam {
@NotNull
@ApiModelProperty(value = "商品类型,1音频课程,2直播课程,3vip订阅-Commodity type, 1 audio course, 2 live course, 3 vip subscription")
private Integer goodsType;
@NotNull
@ApiModelProperty(value = "商品id-goods id")
private Long goodsId;
@NotBlank
@ApiModelProperty(value = "交易类型,wechatPay:微信支付;aliPay:支付宝,stripe:Stripe",required = true)
private String paymentType;
@NotNull
@ApiModelProperty(value = "Whether to use points 0不使用,1使用-WHETHER TO USE POINTS 0 DO NOT USE 1 USE")
private Integer useScore;
@ApiModelProperty(value = "信用卡信息")
@SkipBodyParamLogPrint
private CreditCardInfo creditCardInfo;
}
执行方法
@ApiModelProperty("获取app支付参数")
@SkipBodyParamLogPrint
public void getPayOrderToApp(@RequestBody BuyGoodsParam buyGoodsParam) throws IOException {
orderService.getGoodsOrderFromApp(buyGoodsParam);
}
边栏推荐
猜你喜欢

Ali's official Redis development specification

Rancher 部署 DataKit 最佳实践

助推科技强国高质量发展《科创超级训练营》系列活动正式拉开帷幕

Good guy, the company server just crashed!

MUI as a mobile phone to return to the action bar

【paper】Cam2BEV论文浅析

金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)

04 flink 集群搭建

22年镜头“卷”史,智能手机之战卷进死胡同

沈腾拯救暑期档
随机推荐
关于2022年深圳市福田区支持高端服务业发展项目的申报通知
Isometric graph neural networks shine in drug discovery
Winform的UI帮助类——部分组件会使用到DevExpress组件
金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证
OpenCV-resize函数「建议收藏」
金仓数据库 KDTS 迁移工具使用指南(3. 系统部署)
The untiy Resources directory dynamically loads resources
泰国 好产品推荐!2022年最好的胶原蛋白评测有哪些? 喝出健康和美丽适合需要改善肌肤
主流小程序框架性能分析
变量交换;复合赋值;增递减运算符
ROS2系列知识(5):【参数】如何管理?
成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
SQL函数 TO_CHAR(一)
2022 Strong Net Cup CTF---Strong Net Pioneer ASR wp
金仓数据库 KDTS 迁移工具使用指南(2. 简介)
Go unit tests
MySQL's maximum recommended number of rows is 2000w, is it reliable?
二分练习题
zabbix部署和简单使用
06 redis cluster structures