在Spring Boot中使用AOP(面向切面编程)记录操作日志具有以下好处:
- 减少重复代码:利用AOP,可以将日志记录、性能统计、安全控制、事务处理、异常处理等代码从业务逻辑代码中划分出来作为公共部分,从而减少重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。
- 提高开发效率:通过将日志记录等通用功能与核心业务功能分离,开发人员可以专注于业务逻辑的开发,而无需在每个业务逻辑中手动添加日志记录等代码。这有助于提高开发效率。
- 方便跟踪和统计:通过AOP记录操作日志,可以方便地跟踪和统计用户对系统的操作情况。例如,通过分析日志记录,可以了解用户常用操作,以便定点推送消息。此外,还可以统计异常出现的次数或发生的时间,方便对系统进行优化和改进。
- 符合开闭原则:AOP是面向切面编程的,符合开闭原则。这意味着在不修改原有代码的基础上,可以对代码进行扩展。例如,在不修改原有业务逻辑代码的情况下,可以添加新的日志记录或安全控制功能。
- 易于集成和上手:Spring Boot已经集成了AOP功能,使得开发者可以更容易地使用和集成AOP。同时,由于Spring Boot的广泛使用,AOP与Spring Boot的集成也使得开发者可以更容易地找到相关的资源和文档。
总之,在Spring Boot中使用AOP记录操作日志可以提高系统的可维护性、可读性和可扩展性,并降低开发成本?。
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<!-- 获取ip-->
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.20</version>
</dependency>
<!-- knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
<!-- aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
package com.pzg.chat.annotation;
import java.lang.annotation.*;
/**
* 获取某些方法执行的method操作(参照包com.example.demo.constant.OptTypeConstant下的常量选择)
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OptLog {
String optType() default ""; //执行声明操作
}
//操作日志
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_operation_log")
public class OperationLog {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String optModule;
private String optUri;
private String optType;
private String optMethod;
private String optDesc;
private String requestMethod;
private String requestParam;
private String responseData;
private Integer userId;
private String nickname;
private String ipAddress;
private String ipSource;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime updateTime;
}
? ? ? ? 负责指定切点、切面、逻辑处理,对操作日志进行收集,通过反射获取方法上的注解,并对注解进行解析,获取注解字段信息,并封装,然后通过SpringBoot的监听器功能实现异步记录
@Aspect
@Component
@SuppressWarnings("all")
public class OperationLogAspect {
@Autowired
private ApplicationContext applicationContext;
@Pointcut("@annotation(com.pzg.chat.annotation.OptLog)")
public void operationLogPointCut(){
}
@AfterReturning(value = "operationLogPointCut()", returning = "keys")
@SuppressWarnings("unchecked")
public void saveOperationLog(JoinPoint joinPoint, Object keys) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) Objects.requireNonNull(requestAttributes).resolveReference(RequestAttributes.REFERENCE_REQUEST);
OperationLog operationLog = new OperationLog();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Api api = (Api) signature.getDeclaringType().getAnnotation(Api.class);
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
OptLog optLog = method.getAnnotation(OptLog.class);
operationLog.setOptModule(api.tags()[0]);
operationLog.setOptType(optLog.optType());
operationLog.setOptDesc(apiOperation.value());
String className = joinPoint.getTarget().getClass().getName();
String methodName = method.getName();
methodName = className + "." + methodName;
operationLog.setRequestMethod(Objects.requireNonNull(request).getMethod());
operationLog.setOptMethod(methodName);
if (joinPoint.getArgs().length > 0) {
if (joinPoint.getArgs()[0] instanceof MultipartFile) {
operationLog.setRequestParam("file");
} else {
operationLog.setRequestParam(JSON.toJSONString(joinPoint.getArgs()));
}
}
operationLog.setResponseData(JSON.toJSONString(keys));
operationLog.setUserId(Optional.ofNullable(UserUtil.getUserDetailsDTO().getId()).orElse(0));
operationLog.setNickname(Optional.ofNullable(UserUtil.getUserDetailsDTO().getNickName()).orElse("anonymous"));
String ipAddress = IpUtil.getIpAddress(request);
operationLog.setIpAddress(ipAddress);
operationLog.setIpSource(IpUtil.getIpSource(ipAddress));
operationLog.setOptUri(request.getRequestURI());
applicationContext.publishEvent(new OperationLogEvent(operationLog));
}
}
? ? ? ? 负责接收日志数据
public class ExceptionLogEvent extends ApplicationEvent {
public ExceptionLogEvent(ExceptionLog exceptionLog) {
super(exceptionLog);
}
}
? ? ? ? 通过在监听类中监听事件的变化,最后将数据通过mybatis plus插入到数据库当中
@Component
public class EventListeners {
@Resource
private OperationLogMapper operationLogMapper;
@Autowired
private ExceptionLogMapper exceptionLogMapper;
@Async
@EventListener(OperationLogEvent.class)
public void operationLogEvent(OperationLogEvent operationLogEvent){
operationLogMapper.insert((OperationLog)operationLogEvent.getSource());
}
}
package com.aurora.util;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
@Component
public class IpUtil {
private static DbSearcher searcher;
private static Method method;
public static String getIpAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("X-Real-IP");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("x-forwarded-for");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
//根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
log.error("getIpAddress exception:", e);
}
assert inet != null;
ipAddress = inet.getHostAddress();
}
}
return StringUtils.substringBefore(ipAddress, ",");
}
@PostConstruct
private void initIp2regionResource() throws Exception {
InputStream inputStream = new ClassPathResource("/ip/ip2region.db").getInputStream();
byte[] dbBinStr = FileCopyUtils.copyToByteArray(inputStream);
DbConfig dbConfig = new DbConfig();
searcher = new DbSearcher(dbConfig, dbBinStr);
method = searcher.getClass().getMethod("memorySearch", String.class);
}
public static String getIpSource(String ipAddress) {
if (ipAddress == null || !Util.isIpAddress(ipAddress)) {
log.error("Error: Invalid ip address");
return "";
}
try {
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ipAddress);
String ipInfo = dataBlock.getRegion();
if (!StringUtils.isEmpty(ipInfo)) {
ipInfo = ipInfo.replace("|0", "");
ipInfo = ipInfo.replace("0|", "");
return ipInfo;
}
} catch (Exception e) {
log.error("getCityInfo exception:", e);
}
return "";
}
public static String getIpProvince(String ipSource) {
String[] strings = ipSource.split("\\|");
if (strings[1].endsWith("省")) {
return StringUtils.substringBefore(strings[1], "省");
}
return strings[1];
}
public static UserAgent getUserAgent(HttpServletRequest request) {
return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
}
}
@Api(tags = "操作日志模块")
@RestController
public class OperationLogController {
@Autowired
private OperationLogService operationLogService;
@ApiOperation(value = "查看操作日志")
@GetMapping("/admin/operation/logs")
public ResultVO<PageVO<OperationLogDTO>> listOperationLogs(ConditionVO conditionVO) {
return ResultVO.ok(operationLogService.listOperationLogs(conditionVO));
}
@OptLog(optType = DELETE)
@ApiOperation(value = "删除操作日志")
@DeleteMapping("/admin/operation/logs")
public ResultVO<?> deleteOperationLogs(@RequestBody List<Integer> operationLogIds) {
operationLogService.removeByIds(operationLogIds);
return ResultVO.ok();
}
}