SpringBootAOP记录⽤户操作⽇志
在Spring框架中,使⽤AOP配合⾃定义注解可以⽅便的实现⽤户操作的监控。⾸先搭建⼀个基本的Spring Boot Web环境开启Spring Boot,然后
引⼊必要依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- druid数据源驱动 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
⾃定义注解
定义⼀个⽅法级别的@Log注解,⽤于标注需要监控的⽅法:
@Target(ElementType.METHOD)
jdbctemplate查询一条数据@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default "";
}
创建库表和实体
在数据库中创建⼀张sys_log表,⽤于保存⽤户的操作⽇志
实体:
public class SysLog implements Serializable {
private Integer id;
private String username;
private String operation;
private Integer time;
private String method;
private String params;
private String ip;
private Date createTime;
............
dao:
public interface SysLogDao {
void saveSysLog(SysLog syslog);
}
@Repository
public class SysLogDaoImpl implements SysLogDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void saveSysLog(SysLog syslog) {
System.out.println(syslog);
StringBuffer sql = new StringBuffer("insert into sys_log ");
sql.append("(id,username,operation,time,method,params,ip,create_time) ");
sql.append("values(NULL,?,?,?,?,?,?,?)");
String String();
jdbcTemplate.update(Username(),Operation(),Time(),Method(),Params(),Ip(),CreateTime());        jdbcTemplate.update(sql2);
}
}
Aspect:
@Aspect
@Component
public class LogAspect {
@Autowired
private SysLogDao sysLogDao;
@Pointcut("@annotation(com.lc.aop.annotation.Log)")
public void pointcut() { }
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) {
Object result = null;
long beginTime = System.currentTimeMillis();
try {
// 执⾏⽅法
result = point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
// 执⾏时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
// 保存⽇志
saveLog(point, time);
return result;
}
private void saveLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) Signature();
Method method = Method();
SysLog sysLog = new SysLog();
Log logAnnotation = Annotation(Log.class);
if (logAnnotation != null) {
// 注解上的描述
sysLog.setOperation(logAnnotation.value());
}
// 请求的⽅法名
String className = Target().getClass().getName();
String methodName = Name();
sysLog.setMethod(className + "." + methodName + "()");
// 请求的⽅法参数值
Object[] args = Args();
// 请求的⽅法参数名称
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
String[] paramNames = u.getParameterNames(method);
if (args != null && paramNames != null) {
String params = "";
for (int i = 0; i < args.length; i++) {
params += "  " + paramNames[i] + ": " + args[i];
}
sysLog.setParams(params);
}
// 获取request
HttpServletRequest request = HttpServletRequest();
// 设置IP地址
sysLog.IpAddr(request));
// 模拟⼀个⽤户名
sysLog.setUsername("mrbird");
sysLog.setTime((int) time);
sysLog.setCreateTime(new Date());
// 保存系统⽇志
sysLogDao.saveSysLog(sysLog);
}
}
controller:
@RestController
public class TestController {
@Log("执⾏⽅法⼀")
@GetMapping("/one")
public void methodOne(String name) { }
@Log("执⾏⽅法⼆")
@GetMapping("/two")
public void methodTwo() throws InterruptedException {
Thread.sleep(2000);
}
@Log("执⾏⽅法三")
@GetMapping("/three")
public void methodThree(String name, String age) { }
}
⼯具类:
public class IPUtils {
/**
* 获取IP地址
*
* 使⽤Nginx等反向代理软件,则不能通过RemoteAddr()获取IP地址
* 如果使⽤了多级反向代理的话,X-Forwarded-For的值并不⽌⼀个,⽽是⼀串IP地址,X-Forwarded-For中第⼀个⾮unknown的有效IP字符串,则为真实IP地址    */
public static String getIpAddr(HttpServletRequest request) {
String ip = Header("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = RemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
}
public class HttpContextUtils {
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestAttributes()).getRequest();
}
}
application.properties
spring.datasource.sql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
项⽬结构:
访问后:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。