审计日志增加插入数据库实现类
This commit is contained in:
@@ -37,4 +37,10 @@ zlt:
|
||||
- com.central.user.mapper.SysRoleMapper.findAll
|
||||
#审计日志
|
||||
# audit-log:
|
||||
# enabled: true
|
||||
# enabled: true
|
||||
# log-type: db
|
||||
# datasource:
|
||||
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# jdbc-url: jdbc:mysql://${zlt.datasource.ip}:3306/logger-center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
|
||||
# username: ${zlt.datasource.username}
|
||||
# password: ${zlt.datasource.password}
|
||||
@@ -52,5 +52,10 @@
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+5
@@ -27,4 +27,9 @@ public class AuditLogProperties {
|
||||
* 日志记录类型(logger/redis/db/es)
|
||||
*/
|
||||
private String logType;
|
||||
/**
|
||||
* 数据源配置
|
||||
* logType=db时生效(非必须),如果不配置则使用当前数据源
|
||||
*/
|
||||
private LogDbProperties datasource = new LogDbProperties();
|
||||
}
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.central.log.properties;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 日志数据源配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2020/2/8
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class LogDbProperties extends HikariConfig {
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.central.log.service.impl;
|
||||
|
||||
import com.central.log.model.Audit;
|
||||
import com.central.log.properties.AuditLogProperties;
|
||||
import com.central.log.service.IAuditService;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 审计日志实现类-数据库
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2020/2/8
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
@ConditionalOnProperty(name = "zlt.audit-log.log-type", havingValue = "db")
|
||||
@ConditionalOnClass(JdbcTemplate.class)
|
||||
public class DbAuditServiceImpl implements IAuditService {
|
||||
private static final String INSERT_SQL = " insert into sys_logger " +
|
||||
" (application_name, class_name, method_name, user_id, user_name, client_id, operation, timestamp) " +
|
||||
" values (?,?,?,?,?,?,?,?)";
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public DbAuditServiceImpl(AuditLogProperties auditLogProperties, DataSource dataSource) {
|
||||
//优先使用配置的日志数据源,否则使用默认的数据源
|
||||
if (StringUtils.isNotEmpty(auditLogProperties.getDatasource().getJdbcUrl())) {
|
||||
dataSource = new HikariDataSource(auditLogProperties.getDatasource());
|
||||
}
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `sys_logger` (\n" +
|
||||
" `id` int(11) NOT NULL AUTO_INCREMENT,\n" +
|
||||
" `application_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '应用名',\n" +
|
||||
" `class_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '类名',\n" +
|
||||
" `method_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '方法名',\n" +
|
||||
" `user_id` int(11) NULL COMMENT '用户id',\n" +
|
||||
" `user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '用户名',\n" +
|
||||
" `client_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '租户id',\n" +
|
||||
" `operation` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '操作信息',\n" +
|
||||
" `timestamp` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '创建时间',\n" +
|
||||
" PRIMARY KEY (`id`) USING BTREE\n" +
|
||||
") ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;";
|
||||
this.jdbcTemplate.execute(sql);
|
||||
}
|
||||
|
||||
@Async
|
||||
@Override
|
||||
public void save(Audit audit) {
|
||||
this.jdbcTemplate.update(INSERT_SQL
|
||||
, audit.getApplicationName(), audit.getClassName(), audit.getMethodName()
|
||||
, audit.getUserId(), audit.getUserName(), audit.getClientId()
|
||||
, audit.getOperation(), audit.getTimestamp());
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,5 @@ com.central.log.config.TtlMDCAdapterInitializer
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.log.config.LogAutoConfigure,\
|
||||
com.central.log.service.impl.LoggerAuditServiceImpl,\
|
||||
com.central.log.service.impl.DbAuditServiceImpl,\
|
||||
com.central.log.aspect.AuditLogAspect
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
CREATE DATABASE IF NOT EXISTS `logger-center` DEFAULT CHARACTER SET = utf8;
|
||||
Use `logger-center`;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_logger
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_logger`;
|
||||
CREATE TABLE `sys_logger` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`application_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '应用名',
|
||||
`class_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '类名',
|
||||
`method_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '方法名',
|
||||
`user_id` int(11) NULL COMMENT '用户id',
|
||||
`user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '用户名',
|
||||
`client_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '租户id',
|
||||
`operation` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '操作信息',
|
||||
`timestamp` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
Reference in New Issue
Block a user