日志的实体类
@Data
public class OperationLogPojo {
private String id;
private String optionName;//操作名字
private String userid;//操作者
private String optionType;//操作类型
private String operationTime;//操作时间
private String username;//操作者名字
}
获取方法名和操作内容
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Order(Ordered.HIGHEST_PRECEDENCE)
@Documented
public @interface OperationLog {
//方法名
String moduleName() default "";
//操作内容
String option() default "";
}
切入类
@Aspect
@Component
public class LogInterceptor {
@Autowired
private HttpServletRequest request2;
@Autowired
private LogService logService;
@Around("@annotation(log)")
public Object interceptorApplogic(ProceedingJoinPoint pj, OperationLog log) throws Throwable {
HttpSession session = request2.getSession(true);
String userid = session.getAttribute("userguid").toString();
String username = session.getAttribute("name").toString();
//这个Pojo是我日志记录的实体类
OperationLogPojo operationLogPojo = new OperationLogPojo();
//因为项目的权限控制用的是shiro,所以我这里的可以获取到操作人id,名字这段无关具体操作流程
//获取到被拦截接口上注解的option
String option = log.option();
//***继续执行被拦截的接口,记住最后一定要返回这个结果***
Object proceed = pj.proceed();
//获取到拦截的接口的方法名
String methodName = pj.getSignature().getName();
MethodSignature signature = (MethodSignature) pj.getSignature();
//被拦截的方法的参数名称数组
String[] parameterNames = signature.getParameterNames();
//被拦截的方法的参数值对象数组,这里我们将传入的参数转为json字符串格式便于存入数据库
Object[] objects = pj.getArgs();
//给日志操作记录类赋值然后存入数据库
operationLogPojo.setId(UUIDUtils.generate36UUID());
operationLogPojo.setUserid(userid);
operationLogPojo.setOptionName(methodName);
//方法名
operationLogPojo.setOptionType(option);
//操作名
operationLogPojo.setUsername(username);
//operationLogPojo.setOperation(option);
operationLogPojo.setOperationTime(TimeUtil.get19SysTime());
logService.insertlog(operationLogPojo);
return proceed;
}
}
在具体方法的实现
/**
* 数据库图片下载
*/
@OperationLog(moduleName = "数据库图片下载",option = "数据库图片下载")
@GetMapping("/getdetailimg")
public JSONObject getdetailimg(HttpServletResponse response,HttpServletRequest request){
JSONObject jsonobj = new JSONObject();
Map<String, Object> result = new HashMap<>();
Img imgList = imgService.getdetailimg(request.getParameter("id"));
result.put("img",imgList);
jsonobj = JSONResultUtil.getJSONResult(result);
return jsonobj;
}