Spring Boot AOP记录用户操作日志
This commit is contained in:
parent
480c161c6d
commit
76d70776c0
|
|
@ -0,0 +1,76 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.springboot</groupId>
|
||||
<artifactId>Spring-Boot-AOP-Log</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<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>
|
||||
|
||||
<!-- oracle驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>6.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- druid数据源驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.6</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class,args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.springboot.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Log {
|
||||
String value() default "";
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package com.springboot.aspect;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.springboot.annotation.Log;
|
||||
import com.springboot.dao.SysLogDao;
|
||||
import com.springboot.domain.SysLog;
|
||||
import com.springboot.util.HttpContextUtils;
|
||||
import com.springboot.util.IPUtils;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class LogAspect {
|
||||
|
||||
@Autowired
|
||||
private SysLogDao sysLogDao;
|
||||
|
||||
@Pointcut("@annotation(com.springboot.annotation.Log)")
|
||||
public void pointcut() {
|
||||
}
|
||||
|
||||
@Around("pointcut()")
|
||||
public void around(ProceedingJoinPoint point) {
|
||||
long beginTime = System.currentTimeMillis();
|
||||
try {
|
||||
// 执行方法
|
||||
point.proceed();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 执行时长(毫秒)
|
||||
long time = System.currentTimeMillis() - beginTime;
|
||||
// 保存日志
|
||||
saveLog(point, time);
|
||||
}
|
||||
|
||||
private void saveLog(ProceedingJoinPoint joinPoint, long time) {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
SysLog sysLog = new SysLog();
|
||||
Log logAnnotation = method.getAnnotation(Log.class);
|
||||
if (logAnnotation != null) {
|
||||
// 注解上的描述
|
||||
sysLog.setOperation(logAnnotation.value());
|
||||
}
|
||||
// 请求的方法名
|
||||
String className = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = signature.getName();
|
||||
sysLog.setMethod(className + "." + methodName + "()");
|
||||
// 请求的方法参数值
|
||||
Object[] args = joinPoint.getArgs();
|
||||
// 请求的方法参数名称
|
||||
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 = HttpContextUtils.getHttpServletRequest();
|
||||
// 设置IP地址
|
||||
sysLog.setIp(IPUtils.getIpAddr(request));
|
||||
// 模拟一个用户名
|
||||
sysLog.setUsername("mrbird");
|
||||
sysLog.setTime((int) time);
|
||||
Date date = new Date();
|
||||
sysLog.setCreateTime(date);
|
||||
// 保存系统日志
|
||||
sysLogDao.saveSysLog(sysLog);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.springboot.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.springboot.annotation.Log;
|
||||
|
||||
@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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.springboot.dao;
|
||||
|
||||
import com.springboot.domain.SysLog;
|
||||
|
||||
public interface SysLogDao {
|
||||
void saveSysLog(SysLog syslog);
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.springboot.dao.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.springboot.dao.SysLogDao;
|
||||
import com.springboot.domain.SysLog;
|
||||
|
||||
@Repository
|
||||
public class SysLogDaoImp implements SysLogDao {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public void saveSysLog(SysLog syslog) {
|
||||
StringBuffer sql = new StringBuffer("insert into sys_log ");
|
||||
sql.append("(id,username,operation,time,method,params,ip,create_time) ");
|
||||
sql.append("values(seq_sys_log.nextval,:username,:operation,:time,:method,");
|
||||
sql.append(":params,:ip,:createTime)");
|
||||
|
||||
NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(this.jdbcTemplate.getDataSource());
|
||||
npjt.update(sql.toString(), new BeanPropertySqlParameterSource(syslog));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package com.springboot.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class SysLog implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = -6309732882044872298L;
|
||||
|
||||
private Integer id;
|
||||
private String username;
|
||||
private String operation;
|
||||
private Integer time;
|
||||
private String method;
|
||||
private String params;
|
||||
private String ip;
|
||||
private Date createTime;
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public String getOperation() {
|
||||
return operation;
|
||||
}
|
||||
public void setOperation(String operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
public Integer getTime() {
|
||||
return time;
|
||||
}
|
||||
public void setTime(Integer time) {
|
||||
this.time = time;
|
||||
}
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
public String getParams() {
|
||||
return params;
|
||||
}
|
||||
public void setParams(String params) {
|
||||
this.params = params;
|
||||
}
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.springboot.util;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
public class HttpContextUtils {
|
||||
public static HttpServletRequest getHttpServletRequest() {
|
||||
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.springboot.util;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class IPUtils {
|
||||
|
||||
/**
|
||||
* 获取IP地址
|
||||
*
|
||||
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
|
||||
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
|
||||
*/
|
||||
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("WL-Proxy-Client-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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
server:
|
||||
context-path: /web
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
# 数据库访问配置, 使用druid数据源
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: oracle.jdbc.driver.OracleDriver
|
||||
url: jdbc:oracle:thin:@localhost:1521:ORCL
|
||||
username: test
|
||||
password: 123456
|
||||
# 连接池配置
|
||||
initial-size: 5
|
||||
min-idle: 5
|
||||
max-active: 20
|
||||
# 连接等待超时时间
|
||||
max-wait: 30000
|
||||
# 配置检测可以关闭的空闲连接间隔时间
|
||||
time-between-eviction-runs-millis: 60000
|
||||
# 配置连接在池中的最小生存时间
|
||||
min-evictable-idle-time-millis: 300000
|
||||
validation-query: select '1' from dual
|
||||
test-while-idle: true
|
||||
test-on-borrow: false
|
||||
test-on-return: false
|
||||
# 打开PSCache,并且指定每个连接上PSCache的大小
|
||||
pool-prepared-statements: true
|
||||
max-open-prepared-statements: 20
|
||||
max-pool-prepared-statement-per-connection-size: 20
|
||||
# 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙
|
||||
filters: stat,wall
|
||||
# Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
|
||||
aop-patterns: com.springboot.servie.*
|
||||
|
||||
|
||||
# WebStatFilter配置
|
||||
web-stat-filter:
|
||||
enabled: true
|
||||
# 添加过滤规则
|
||||
url-pattern: /*
|
||||
# 忽略过滤的格式
|
||||
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
|
||||
|
||||
# StatViewServlet配置
|
||||
stat-view-servlet:
|
||||
enabled: true
|
||||
# 访问路径为/druid时,跳转到StatViewServlet
|
||||
url-pattern: /druid/*
|
||||
# 是否能够重置数据
|
||||
reset-enable: false
|
||||
# 需要账号密码才能访问控制台
|
||||
login-username: druid
|
||||
login-password: druid123
|
||||
# IP白名单
|
||||
# allow: 127.0.0.1
|
||||
# IP黑名单(共同存在时,deny优先于allow)
|
||||
# deny: 192.168.1.218
|
||||
|
||||
# 配置StatFilter
|
||||
filter:
|
||||
stat:
|
||||
log-slow-sql: true
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
CREATE TABLE SYS_LOG (
|
||||
ID NUMBER(20) NOT NULL ,
|
||||
USERNAME VARCHAR2(50 BYTE) NULL ,
|
||||
OPERATION VARCHAR2(50 BYTE) NULL ,
|
||||
TIME NUMBER(11) NULL ,
|
||||
METHOD VARCHAR2(200 BYTE) NULL ,
|
||||
PARAMS VARCHAR2(500 BYTE) NULL ,
|
||||
IP VARCHAR2(64 BYTE) NULL ,
|
||||
CREATE_TIME DATE NULL
|
||||
);
|
||||
COMMENT ON COLUMN SYS_LOG.USERNAME IS '用户名';
|
||||
COMMENT ON COLUMN SYS_LOG.OPERATION IS '用户操作';
|
||||
COMMENT ON COLUMN SYS_LOG.TIME IS '响应时间';
|
||||
COMMENT ON COLUMN SYS_LOG.METHOD IS '请求方法';
|
||||
COMMENT ON COLUMN SYS_LOG.PARAMS IS '请求参数';
|
||||
COMMENT ON COLUMN SYS_LOG.IP IS 'IP地址';
|
||||
COMMENT ON COLUMN SYS_LOG.CREATE_TIME IS '创建时间';
|
||||
CREATE SEQUENCE seq_sys_log START WITH 1 INCREMENT BY 1;
|
||||
Loading…
Reference in New Issue