新建logs表

添加aop依赖
<!-- aop依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
新建获取ip地址工具类
import javax.servlet.http.HttpServletRequest;
/**
* 功能:IpUtils
* 获取ip地址工具类
* 作者:爱因斯坦乐
*/
public class IpUtils {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown ".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
}
新建注解@HoneyLogs
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HoneyLogs {
//操作的模块
String operation();
//操作类型
LogType type();
}
新建枚举类
/**
* 系统日志的操作类型的枚举
*/
public enum LogType {
ADD("新增"), UPDATE("更新"), DELETE("删除"), LOGIN("登录");
private String value;
public String getValue() {
return value;
}
LogType(String value) {
this.value = value;
}
}
创建日志实体类
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 功能:Logs
* 日志实体类
* 作者:爱因斯坦乐
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Logs {
@TableId(type = IdType.AUTO)
private Integer id;
private String operation;
private String type;
private String ip;
private String user;