Spring Boot AOP 日志管理
程序员文章站
2022-03-02 15:39:55
...
/**
* Created by johnzh on
* 2017/3/20. 15:03
*/
@Aspect
@Component
public class WebLogAspect extends BaseController{
private final ManagementLogService managementLogService;
private final LoginService loginService;
private ManagementOperationLogDO managementOperationLogDO = new ManagementOperationLogDO();
@Autowired
public WebLogAspect(ManagementLogService managementLogService, LoginService loginService) {
this.managementLogService = managementLogService;
this.loginService = loginService;
}
/**
* 记录系统管理中的所有操作
*/
@Pointcut("execution(public * com.zx.debtCollection.controller.SystemManagementController.*(..))")
public void systemLog(){}
/**
* 记录登录操作
*/
@Pointcut("execution(public * com.zx.debtCollection.controller.LoginController.login(..))")
public void loginLog(){}
/**
* 请求执行后记录
*
* @param ret 返回信息
* @throws Throwable 抛出异常
*/
@AfterReturning(returning = "ret", pointcut = "systemLog() || loginLog()")
public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable{
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
AuthDTO authDTO= loginService.getAuthDTO(getTokenId(request));
String uri = request.getRequestURI();
//过滤不需要存储的日志
ManagementOperationDO managementOperationDO = managementLogService.getOperationByUri(uri);
if (managementOperationDO == null || managementOperationDO.getFlag() == 0) {
return;
}
managementOperationLogDO.setOperationId(managementOperationDO.getId());
//登录操作特殊处理
if (Objects.equals(managementOperationDO.getInterfaceUri(), "/system/log/getLogList"))
return;
if (Objects.equals(managementOperationDO.getInterfaceUri(), "/auth/login")) {
managementOperationLogDO.setOperationDetail("登录ip:" + getRemoteIP(request));
Object[] objects = joinPoint.getArgs();
System.out.println(objects[1].toString());
JSONObject json = JSONObject.parseObject(objects[1].toString());
String account = json.getString("account");
String userId = managementLogService.getUserIdByAccount(account);
if (userId == null) {
return;
}
managementOperationLogDO.setOperatorUserId(userId);
} else if(authDTO == null) {
return;
}
//其他操作处理
else {
managementOperationLogDO.setOperatorUserId(authDTO.getUserId());
List<String> stringList = new ArrayList<>();
Object[] objects = joinPoint.getArgs();
if (objects != null && objects.length>0){
for(Object object : objects){
if (object != null && !(object instanceof HttpServletRequest)){
stringList.add(object.toString());
}
}
}
managementOperationLogDO.setOperationDetail("请求参数:" + stringList.toString());
}
managementOperationLogDO.setCreateTime(new Date());
//返回结果处理
try{
JSONObject json = JSONObject.parseObject(ret.toString());
String response_code = json.getString("response_code");
managementOperationLogDO.setResponseCode(response_code);
}catch (Exception ignored){
ignored.printStackTrace();
managementOperationLogDO.setResponseCode("");
}finally {
managementLogService.saveSystemLog(managementOperationLogDO);
}
}
private String getRemoteIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
}