当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
随机推荐
08 spark 集群搭建
MySQL INTERVAL Keyword Guidelines
My new book has sold 10,000 copies!
程序员架构修炼之道:如何设计“易理解”的系统架构?
面对营销难,有米云指出一条破局之路
03 gp cluster construction
金仓数据库 KDTS 迁移工具使用指南(3. 系统部署)
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
90后的焦虑,被菜市场治好了
京东软件测试面试题,仅30题就已经拯救了50%的人
Path helper class for C#
PAT 甲级 A1030 Travel Plan
C # Excel helper classes
关于2022年深圳市福田区支持高端服务业发展项目的申报通知
不需要写代码,快速批量修改文件夹中图片的格式
M1芯片电脑安装cerebro
MUI as a mobile phone to return to the action bar
2022强网杯CTF---强网先锋 ASR wp
C#中关于DevExpress的常用操作和帮助类项目工程内容说明
C#的DataTable帮助类









