移动网站设计上机考试,wordpress7比2主题破解版,经济师考试时间2023报名时间,网站建设了解背景
业务组有一些给开发用的后门接口#xff0c;为了做到调用溯源#xff0c;业务组最近需要记录所有接口的访问记录#xff0c;暂时只需要记录接口的响应结果#xff0c;如果调用失败#xff0c;则记录异常信息。由于后门接口较多以及只是业务组内部轻度使用#xff0…背景
业务组有一些给开发用的后门接口为了做到调用溯源业务组最近需要记录所有接口的访问记录暂时只需要记录接口的响应结果如果调用失败则记录异常信息。由于后门接口较多以及只是业务组内部轻度使用因此使用了切面的方式实现。
方案
EnableAspectJAutoProxy
Aspect
Component
Slf4j
public class ResponseLogAspect {Resourceprivate CommonConstants commonConstants;Pointcut(annotation(*.log.ResponseLog))public void logPointcut() {}/*** 执行成功打印*/AfterReturning(pointcut logPointcut(), returning result)public void log(JoinPoint joinPoint, Object result) {try {// 降级开关if (!commonConstants.getBoolean(interface.response.log.switch, true)) {return;}ResponseLog annotation findAnnotation(joinPoint, ResponseLog.class);String metric metric(annotation.value(), joinPoint);log.info(interface success: {}, result: {}, metric, result);} catch (Exception e) {log.error(log error, e);QMonitor.recordOne(interface_response_log_fail);}}/*** 执行失败打印*/AfterThrowing(pointcut logPointcut(), throwing error)public void logError(JoinPoint joinPoint, Throwable error) {try {// 降级开关if (!commonConstants.getBoolean(interface.response.log.switch, true)) {return;}ResponseLog annotation findAnnotation(joinPoint, ResponseLog.class);String metric metric(annotation.value(), joinPoint);log.error(interface fail: {}, error: {}, metric, error.getMessage());} catch (Exception e) {log.error(log error, e);QMonitor.recordOne(interface_response_log_fail);}}/*** 监控指标* param specificName 具体指标名* param point 切点* return 指标名称*/private String metric(String specificName, JoinPoint point) {if (StringUtils.isBlank(specificName)) {String clz point.getTarget().getClass().getSimpleName();String mtd point.getSignature().getName();return clz _ mtd;} else {return specificName;}}/*** 注解查询* param point 切点* param annotationType 注解类型* return 注解信息*/private A extends Annotation A findAnnotation(JoinPoint point, ClassA annotationType) {MethodSignature signature (MethodSignature) point.getSignature();return AnnotationUtils.findAnnotation(signature.getMethod(), annotationType);}
}
接下来只需要在后门接口上增加对应的注解即可 RequestMapping(value save, method RequestMethod.POST)ResponseBodyResponseLog(/voucher/save)public APIResponseBoolean save(HttpServletRequest request, RequestBody VoucherCommit voucherCommit) {// 代金券保存接口}
加餐
Target({ElementType.METHOD})指定该注解可以应用于方法。如果不加这个注解则表示默认该注解可以应用到类与方法上但是加上后就表示这个注解只能作用于方法否则会报错。springboot项目由于存在spring-boot-autoconfigure依赖会默认开启aop代理所以注解EnableAspectJAutoProxy可以不用加但是由于可以在配置文件中修改默认开启的逻辑所以建议加上避免失效。Pointcut注解中的参数within和annotation。annotation注解用于匹配那些具有指定注解的方法within注解用于匹配那些具有指定注解的类中的所有方法即使这些方法本身没有显式地标注注解。 // 切点匹配带有OnlyIntranetAccess注解的类Pointcut(within(org.openmmlab.platform.common.annotation.OnlyIntranetAccess))public void onlyIntranetAccessOnClass() {}// 切点匹配带有OnlyIntranetAccess注解的方法Pointcut(annotation(org.openmmlab.platform.common.annotation.OnlyIntranetAccess))public void onlyIntranetAccessOnMethed() {}