pojo
@Data
public class Reqparam {
Cif cif;
int id;
}
@Data
public class Cif {
String name;
String idNo;
}
定义注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
/**
* 请求接口名称
*
* @return
*/
String name() default "default名称";
String[] encryValues() default {"cif.name"};
String[] ingores() default {};
}
切面
@Aspect
@Component
@Slf4j
public class LogAsp {
/**
* 切点
*/
@Pointcut("@annotation(com.example.test.Log)")
public void pointCut() {
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args=joinPoint.getArgs();
Method method=((MethodSignature) joinPoint.getSignature()).getMethod();
Log log=method.getAnnotation(Log.class);
//获取忽略字段
String[] ingores=log.ingores();
//获取需加密字段
String[] encryValues=log.encryValues();
StringBuilder reqPram=new StringBuilder();
for(int i=0;i<args.length;i++){
JSONObject json=JSON.parseObject(JSON.toJSONString(args[i]));
for(String ingoreParam:ingores){
if(JSONPath.contains(json,ingoreParam)){
JSONPath.set(json,ingoreParam,"*****");
}
reqPram.append(JSON.toJSONString(json));
}
for(String encryValue:encryValues){
if(JSONPath.contains(json,encryValue)){
JSONPath.set(json,encryValue,json.getString(encryValue)+"encrypt");
}
}
args[i]=JSONObject.parseObject(JSON.toJSONString(json),args[i].getClass());
}
System.out.println("请求参数为:"+reqPram.toString());
Object proceed = joinPoint.proceed(args);
return proceed;
}
}