日誌的實體類
@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;
}