diff --git a/db/schema.sql b/db/schema.sql index 70cc8af..b1b9aa7 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -1,48 +1,19 @@ -drop table if exists t_user; - -create table if not exists t_user -( - id bigint not null primary key, - username varchar(20) not null comment '用户名', - password char(70) not null comment '密码', - locking int not null comment '锁定', - expiration datetime comment '过期时间', - credentials_expiration datetime comment '认证过期时间', - enable int not null comment '启用', - last_login_time datetime comment '上次登录时间', - last_login_ip varchar(128) comment '上次登录 IP', - create_time datetime not null default (utc_timestamp()) comment '创建时间', - update_time datetime not null default (utc_timestamp()) comment '修改时间', - deleted bigint not null default 0, - version int not null default 0, - constraint t_user_unique unique (username, deleted) -) comment '用户表'; - -insert into t_user (id, username, password, locking, enable) value (0, 'admin', - '$2a$10$3wDGdzTZlC..7eY6u2XM5u78xUQo0z5Sj5yOpneD4QJ0q/TA5TY0S', - 0, 1); - drop table if exists t_sys_log; -create table t_sys_log +create table t_sys_log -- 系统日志表 ( id bigint not null, - log_type varchar(50) not null comment '日志类型', - operate_user_id bigint not null comment '操作用户', - operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间', - request_uri varchar(500) default null comment '请求 URI', - request_method varchar(10) default null comment '请求方式', - request_params text comment '请求参数', - request_ip varchar(20) not null comment '请求 IP', - request_server_address varchar(50) not null comment '请求服务器地址', - is_exception char(1) default null comment '是否异常', - exception_info text comment '异常信息', - start_time datetime(3) not null comment '开始时间', - end_time datetime(3) not null comment '结束时间', - execute_time bigint default null comment '执行时间', - user_agent varchar(500) default null comment '用户代理', - primary key (id) using btree, - key idx_sys_log_log_type (log_type) using btree, - key idx_sys_log_operate_user_id (operate_user_id) using btree, - key idx_sys_log_is_exception (is_exception) using btree, - key idx_sys_log_operate_time (operate_time) using btree -) comment '系统日志表'; \ No newline at end of file + log_type varchar(50) not null, -- 日志类型 + operate_user_id bigint not null, -- 操作用户 + operate_time datetime(3) not null default (strftime('%Y-%m-%d %H:%M:%f','now')), -- 操作时间 + request_uri varchar(500) default null, -- 请求 URI + request_method varchar(10) default null, -- 请求方式 + request_params text, -- 请求参数 + request_ip varchar(20) not null, -- 请求 IP + request_server_address varchar(50) not null, -- 请求服务器地址 + exception int not null default 0, -- 是否异常 + exception_info text, -- 异常信息 + start_time datetime(3) not null, -- 开始时间 + end_time datetime(3) not null, -- 结束时间 + execute_time bigint default null, -- 执行时间 + user_agent varchar(500) default null -- 用户代理 +); \ No newline at end of file diff --git a/pom.xml b/pom.xml index 36048df..096f298 100644 --- a/pom.xml +++ b/pom.xml @@ -153,7 +153,7 @@ org.xerial sqlite-jdbc - 3.42.0.0 + 3.44.1.0 com.baomidou diff --git a/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt b/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt index 75a4be8..8f06f1f 100644 --- a/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt +++ b/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt @@ -34,6 +34,26 @@ fun main(args: Array) { } } + if (!File("data/db").isDirectory) { + if (!File("data/db").mkdir()) { + logger.error("Can not create directory 'data/db', please try again later.") + return + } + } + + if (!File("data/db/sqlite.db").isFile || File("data/db/sqlite.db").inputStream() + .use { it.readNBytes(15).toString(Charsets.UTF_8) != "SQLite format 3" } + ) { + logger.warn("The 'data/db/sqlite.db' database is lost or damaged, recreating...") + if (File("data/db/sqlite.db").exists() && !File("data/db/sqlite.db").delete()) { + logger.error("Can not recreate database 'data/db/sqlite.db', please try again later.") + } + + FatWebApiApplication::class.java.getResourceAsStream("/db/sqlite.db")?.let { + File("data/db/sqlite.db").writeBytes(it.readAllBytes()) + } + } + if (File("application-config.yml").exists() || File("data/application-config.yml").exists()) { runApplication(*args) } else { diff --git a/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt index c038a6c..8ba7c37 100644 --- a/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt @@ -7,7 +7,6 @@ import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController -import top.fatweb.api.converter.system.SysLogConverter import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.param.system.SysLogGetParam @@ -44,9 +43,7 @@ class SysLogController( @PreAuthorize("hasAnyAuthority('system:log:query:all')") fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult> { return ResponseResult.success( - ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo( - sysLogService.getPage(sysLogGetParam) - ) + ResponseCode.DATABASE_SELECT_SUCCESS, data = sysLogService.getPage(sysLogGetParam) ) } } diff --git a/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt index 19db9bd..3bb670d 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt @@ -23,7 +23,6 @@ interface SysLogMapper : BaseMapper { * @param logType List of log types * @param requestMethod List of request methods * @param searchRequestUrl Request URL to search for - * @param searchRegex Use regex * @param searchStartTime Start time to search for * @param searchEndTime end time to search for * @return System log in page @@ -38,7 +37,6 @@ interface SysLogMapper : BaseMapper { logType: List?, requestMethod: List?, searchRequestUrl: String?, - searchRegex: Boolean, searchStartTime: LocalDateTime?, searchEndTime: LocalDateTime? ): IPage diff --git a/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt index c5083ad..0a44587 100644 --- a/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt @@ -51,8 +51,10 @@ data class SysLogGetParam( * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 */ +/* @Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"], defaultValue = "false") val searchRegex: Boolean = false, +*/ /** * Start time to search for diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt index e7cc6e0..3dfa092 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt @@ -1,5 +1,6 @@ package top.fatweb.api.service.permission.impl +import com.baomidou.dynamic.datasource.annotation.DS import com.baomidou.mybatisplus.core.metadata.OrderItem import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper @@ -47,6 +48,7 @@ import java.time.ZoneOffset * @see User * @see IUserService */ +@DS("master") @Service class UserServiceImpl( private val passwordEncoder: PasswordEncoder, diff --git a/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt b/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt index 4b3a431..034450d 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt @@ -1,9 +1,10 @@ package top.fatweb.api.service.system -import com.baomidou.mybatisplus.core.metadata.IPage import com.baomidou.mybatisplus.extension.service.IService import top.fatweb.api.entity.system.SysLog import top.fatweb.api.param.system.SysLogGetParam +import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.system.SysLogVo /** * System log service interface @@ -22,8 +23,8 @@ interface ISysLogService : IService { * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 * @see SysLogGetParam - * @see IPage - * @see SysLog + * @see PageVo + * @see SysLogVo */ - fun getPage(sysLogGetParam: SysLogGetParam?): IPage + fun getPage(sysLogGetParam: SysLogGetParam?): PageVo } diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt index 4ea58fa..066f0c1 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt @@ -1,15 +1,19 @@ package top.fatweb.api.service.system.impl -import com.baomidou.mybatisplus.core.metadata.IPage +import com.baomidou.dynamic.datasource.annotation.DS import com.baomidou.mybatisplus.core.metadata.OrderItem import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import org.springframework.stereotype.Service +import top.fatweb.api.converter.system.SysLogConverter import top.fatweb.api.entity.system.SysLog import top.fatweb.api.mapper.system.SysLogMapper import top.fatweb.api.param.system.SysLogGetParam +import top.fatweb.api.service.permission.IUserService import top.fatweb.api.service.system.ISysLogService import top.fatweb.api.util.PageUtil +import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.system.SysLogVo /** * System log service implement @@ -21,21 +25,29 @@ import top.fatweb.api.util.PageUtil * @see SysLog * @see ISysLogService */ +@DS("sqlite") @Service -class SysLogServiceImpl : ServiceImpl(), ISysLogService { - override fun getPage(sysLogGetParam: SysLogGetParam?): IPage { +class SysLogServiceImpl( + private val userService: IUserService +) : ServiceImpl(), ISysLogService { + override fun getPage(sysLogGetParam: SysLogGetParam?): PageVo { val sysLogPage = Page(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20) PageUtil.setPageSort(sysLogGetParam, sysLogPage, OrderItem.desc("start_time")) - return baseMapper.selectPage( + val sysLogIPage = baseMapper.selectPage( sysLogPage, sysLogGetParam?.logType?.split(","), sysLogGetParam?.requestMethod?.split(","), sysLogGetParam?.searchRequestUrl, - sysLogGetParam?.searchRegex ?: false, sysLogGetParam?.searchStartTime, sysLogGetParam?.searchEndTime ) + sysLogIPage.records.forEach { + it.operateUsername = + it.operateUserId?.let { it1 -> userService.getOne(it1)?.username } + } + + return SysLogConverter.sysLogPageToSysLogPageVo(sysLogIPage) } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a273423..f0dbc7f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -14,10 +14,10 @@ spring: master: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver - local: + sqlite: type: com.zaxxer.hikari.HikariDataSource driver-class-name: org.sqlite.JDBC - url: jdbc:sqlite::resource:db/sqlite.db + url: jdbc:sqlite:data/db/sqlite.db?date_string_format=yyyy-MM-dd'T'HH:mm:ss.SSS jackson: diff --git a/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql b/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql deleted file mode 100644 index 626377c..0000000 --- a/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql +++ /dev/null @@ -1,24 +0,0 @@ -drop table if exists t_sys_log; -create table t_sys_log -( - id bigint not null, - log_type varchar(50) not null comment '日志类型', - operate_user_id bigint not null comment '操作用户', - operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间', - request_uri varchar(500) default null comment '请求 URI', - request_method varchar(10) default null comment '请求方式', - request_params text comment '请求参数', - request_ip varchar(20) not null comment '请求 IP', - request_server_address varchar(50) not null comment '请求服务器地址', - exception int not null default 0 comment '是否异常', - exception_info text comment '异常信息', - start_time datetime(3) not null comment '开始时间', - end_time datetime(3) not null comment '结束时间', - execute_time bigint default null comment '执行时间', - user_agent varchar(500) default null comment '用户代理', - primary key (id) using btree, - key idx_sys_log_log_type (log_type) using btree, - key idx_sys_log_operate_user_id (operate_user_id) using btree, - key idx_sys_log_exception (exception) using btree, - key idx_sys_log_operate_time (operate_time) using btree -) comment '系统日志表'; \ No newline at end of file diff --git a/src/main/resources/db/sqlite.db b/src/main/resources/db/sqlite.db index e69de29..c4467bb 100644 Binary files a/src/main/resources/db/sqlite.db and b/src/main/resources/db/sqlite.db differ diff --git a/src/main/resources/mapper/system/SysLogMapper.xml b/src/main/resources/mapper/system/SysLogMapper.xml index 5bde6d8..ee47c86 100644 --- a/src/main/resources/mapper/system/SysLogMapper.xml +++ b/src/main/resources/mapper/system/SysLogMapper.xml @@ -16,10 +16,8 @@ t_sys_log.start_time as sys_log_start_time, t_sys_log.end_time as sys_log_end_time, t_sys_log.execute_time as sys_log_execute_time, - t_sys_log.user_agent as sys_log_user_agent, - t_user.username as sys_log_operate_username + t_sys_log.user_agent as sys_log_user_agent from t_sys_log - left join t_user on t_user.id = t_sys_log.operate_user_id @@ -30,17 +28,9 @@ #{item} - - - and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri), - if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) regexp #{searchRequestUrl} - - - and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri), - if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) like concat('%', - #{searchRequestUrl}, '%') - - + and t_sys_log.request_server_address || t_sys_log.request_uri || + case when length(t_sys_log.request_params) != 0 then '?' || t_sys_log.request_params else '' end like + '%'||'mail'||'%' and t_sys_log.start_time between #{searchStartTime} and #{searchEndTime} @@ -64,7 +54,5 @@ - -