当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

酷逼了 Pathetic Dog 第 304 场周赛

The anxiety of the post-90s was cured by the vegetable market

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

【黑马早报】胡军代言悟空理财涉嫌欺诈,疑似涉及390亿元;涪陵榨菜回应榨菜吃出脚指甲; 鸿星尔克再捐1个亿;腾讯控股股价跌回5年前...

Unity ui点击事件只响应最上层ui的方式

02 es 集群搭建

首席工程师究竟是怎样的存在?

Rancher 部署 DataKit 最佳实践

京东软件测试面试题,仅30题就已经拯救了50%的人

Vulnhub target drone: HARRYPOTTER_ NAGINI
随机推荐
DataTable Helper Class for C#
Good guy, the company server just crashed!
MySQL's maximum recommended number of rows is 2000w, is it reliable?
深圳市商务局2022年度中央资金(跨境电子商务企业市场开拓扶持事项)申报指南
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
LeetCode Week 303
MySQL locking case analysis
intentservice使用(Intention)
【硬核拆解】50块2个的2022年夏季款智能节电器到底能不能省电?
90后的焦虑,被菜市场治好了
AntDB数据库亮相24届高速展,助力智慧高速创新应用
酷逼了 Pathetic Dog 第 304 场周赛
The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
ROS2系列知识(5):【参数】如何管理?
C#中关于DevExpress的常用操作和帮助类项目工程内容说明
我的新书销量1万册了!
03 gp 集群搭建
OpenCV-resize函数「建议收藏」
银行案例|Zabbix跨版本升级指南,4.2-6.0不香吗?
首席工程师究竟是怎样的存在?