当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
70后夫妻给苹果华为做“雨衣”,三年进账7.91亿
MySQL INTERVAL Keyword Guidelines
泰国 好产品推荐!2022年最好的胶原蛋白评测有哪些? 喝出健康和美丽适合需要改善肌肤
[Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails
我的新书销量1万册了!
金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证
短剧正在抢长剧的生意
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
14年测试人最近的面试经历,值得借鉴√
Complete knapsack problem to find the number of combinations and permutations
随机推荐
如何有效地开发 Jmix 扩展组件
年化收益高的理财产品
MySQL's maximum recommended number of rows is 2000w, is it reliable?
泰国 好产品推荐!2022年最好的胶原蛋白评测有哪些? 喝出健康和美丽适合需要改善肌肤
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
半自动化爬虫-爬取一个网站的内容及回复
intentservice使用(Intention)
软测面试如何介绍项目?要做哪些技术准备?
二分练习题
机器学习快速入门
DataTable Helper Class for C#
Flask框架实战
Winform message prompt box helper class
金仓数据库 OCCI迁移指南(3. KingbaseES的OCCI特性支持)
夸克网盘资源站
请问数据库中报错信息如下,mongoshake 有什么配置的方式解决这种大消息问题吗?
主流小程序框架性能分析
Using Canvas to achieve web page mouse signature effect
ROS2系列知识(7):用rqt_console查看日志logs
MySQL最大建议行数2000w, 靠谱吗?