From 97af1331e2f6d13f52259b7743b35e03be6b8142 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 30 Oct 2023 00:24:39 +0800 Subject: [PATCH] Fix last login bug --- .../top/fatweb/api/converter/UserConverter.kt | 2 ++ .../top/fatweb/api/entity/permission/User.kt | 14 +++++++++++++- .../permission/impl/AuthenticationServiceImpl.kt | 15 +++++++++------ .../fatweb/api/vo/authentication/UserInfoVo.kt | 6 ++++++ src/main/resources/db/migration/R__Basic_data.sql | 15 ++++++++------- .../V1_0_0_231019__Add_table_'t_user'.sql | 2 ++ .../V1_0_0_231026__Add_table_'t_element'.sql | 2 +- .../resources/mapper/permission/UserMapper.xml | 4 ++++ 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt b/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt index 2f15140..07b8fe1 100644 --- a/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt @@ -24,6 +24,8 @@ object UserConverter { expiration = user.expiration, credentialsExpiration = user.credentialsExpiration, enable = user.enable?.let { it == 1 }, + currentLoginTime = user.currentLoginTime, + currentLoginIp = user.currentLoginIp, lastLoginTime = user.lastLoginTime, lastLoginIp = user.lastLoginIp, createTime = user.createTime, diff --git a/src/main/kotlin/top/fatweb/api/entity/permission/User.kt b/src/main/kotlin/top/fatweb/api/entity/permission/User.kt index 2956bf0..62fa0be 100644 --- a/src/main/kotlin/top/fatweb/api/entity/permission/User.kt +++ b/src/main/kotlin/top/fatweb/api/entity/permission/User.kt @@ -60,6 +60,18 @@ class User() : Serializable { @TableField("enable") var enable: Int? = null + /** + * 当前登录时间 + */ + @TableField("current_login_time") + var currentLoginTime: LocalDateTime? = null + + /** + * 当前登录 IP + */ + @TableField("current_login_ip") + var currentLoginIp: String? = null + /** * 上次登录时间 */ @@ -108,6 +120,6 @@ class User() : Serializable { var operations: List? = null override fun toString(): String { - return "User(id=$id, username=$username, password=$password, locking=$locking, expiration=$expiration, credentialsExpiration=$credentialsExpiration, enable=$enable, lastLoginTime=$lastLoginTime, lastLoginIp=$lastLoginIp, createTime=$createTime, updateTime=$updateTime, deleted=$deleted, version=$version, roles=$roles, groups=$groups, menus=$menus, elements=$elements, operations=$operations)" + return "User(id=$id, username=$username, password=$password, locking=$locking, expiration=$expiration, credentialsExpiration=$credentialsExpiration, enable=$enable, currentLoginTime=$currentLoginTime, currentLoginIp=$currentLoginIp, lastLoginTime=$lastLoginTime, lastLoginIp=$lastLoginIp, createTime=$createTime, updateTime=$updateTime, deleted=$deleted, version=$version, roles=$roles, groups=$groups, menus=$menus, elements=$elements, operations=$operations)" } } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt index e7947ec..7147327 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt @@ -36,14 +36,17 @@ class AuthenticationServiceImpl( throw RuntimeException("Login failed") } - logger.info("用户登录 [用户名: '{}', IP: '{}']", user.username, request.remoteAddr) - userService.update(User().apply { - lastLoginIp = request.remoteAddr - lastLoginTime = LocalDateTime.now(ZoneOffset.UTC) - }, KtUpdateWrapper(User()).eq(User::username, user.username)) - val loginUser = authentication.principal as LoginUser loginUser.user.password = "" + + logger.info("用户登录 [用户名: '{}', IP: '{}']", user.username, request.remoteAddr) + userService.update(User().apply { + currentLoginIp = request.remoteAddr + currentLoginTime = LocalDateTime.now(ZoneOffset.UTC) + lastLoginIp = loginUser.user.currentLoginIp + lastLoginTime = loginUser.user.currentLoginTime + }, KtUpdateWrapper(User()).eq(User::username, user.username)) + val userId = loginUser.user.id.toString() val jwt = JwtUtil.createJwt(userId) diff --git a/src/main/kotlin/top/fatweb/api/vo/authentication/UserInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/authentication/UserInfoVo.kt index 58bed65..29f047b 100644 --- a/src/main/kotlin/top/fatweb/api/vo/authentication/UserInfoVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/authentication/UserInfoVo.kt @@ -22,6 +22,12 @@ data class UserInfoVo( @Schema(description = "是否启用", example = "true") val enable: Boolean?, + @Schema(description = "当前登录时间", example = "1900-01-01T00:00:00.000Z") + val currentLoginTime: LocalDateTime?, + + @Schema(description = "当前登录 IP", example = "1.1.1.1") + val currentLoginIp: String?, + @Schema(description = "最后登录时间", example = "1900-01-01T00:00:00.000Z") val lastLoginTime: LocalDateTime?, diff --git a/src/main/resources/db/migration/R__Basic_data.sql b/src/main/resources/db/migration/R__Basic_data.sql index 3b94441..6b1e2a5 100644 --- a/src/main/resources/db/migration/R__Basic_data.sql +++ b/src/main/resources/db/migration/R__Basic_data.sql @@ -41,14 +41,15 @@ on duplicate key update name = values(name), power_id = values(power_id), parent_id = values(parent_id); -insert into t_element(id, name, power_id, menu_id) -values (1010100, '公用', id, 1010000), - (101010100, '角色基础', id, 101010000), - (102010100, '用户组基础', id, 102010000), - (103010100, '用户基础', id, 103010000) -on duplicate key update name = values(name), +insert into t_element(id, name, power_id, menu_id, parent_id) +values (1010100, '公用', id, 1010000, null), + (101010100, '角色基础', id, 101010000, null), + (102010100, '用户组基础', id, 102010000, null), + (103010100, '用户基础', id, 103010000, null) +on duplicate key update name = values(name), power_id=values(power_id), - menu_id = values(menu_id); + menu_id = values(menu_id), + parent_id = values(parent_id); insert into t_operation(id, name, code, power_id, element_id, parent_id) values (1010101, '查询当前用户信息', 'common:user:self', id, 1010100, null), diff --git a/src/main/resources/db/migration/V1_0_0_231019__Add_table_'t_user'.sql b/src/main/resources/db/migration/V1_0_0_231019__Add_table_'t_user'.sql index 659635e..7c4b2ba 100644 --- a/src/main/resources/db/migration/V1_0_0_231019__Add_table_'t_user'.sql +++ b/src/main/resources/db/migration/V1_0_0_231019__Add_table_'t_user'.sql @@ -9,6 +9,8 @@ create table if not exists t_user expiration datetime comment '过期时间', credentials_expiration datetime comment '认证过期时间', enable int not null comment '启用', + current_login_time datetime comment '当前登录时间', + current_login_ip varchar(128) comment '当前登录 IP', last_login_time datetime comment '上次登录时间', last_login_ip varchar(128) comment '上次登录 IP', create_time datetime not null default (utc_timestamp()) comment '创建时间', diff --git a/src/main/resources/db/migration/V1_0_0_231026__Add_table_'t_element'.sql b/src/main/resources/db/migration/V1_0_0_231026__Add_table_'t_element'.sql index 812b0e6..9bbbe94 100644 --- a/src/main/resources/db/migration/V1_0_0_231026__Add_table_'t_element'.sql +++ b/src/main/resources/db/migration/V1_0_0_231026__Add_table_'t_element'.sql @@ -5,6 +5,6 @@ create table if not exists t_element id bigint not null primary key, name varchar(100) not null comment '元素名', power_id bigint not null comment '权限ID', - parent_id bigint not null comment '父ID', + parent_id bigint null comment '父ID', menu_id bigint not null comment '菜单ID' ) comment '页面元素'; \ No newline at end of file diff --git a/src/main/resources/mapper/permission/UserMapper.xml b/src/main/resources/mapper/permission/UserMapper.xml index 050578e..4fb3f41 100644 --- a/src/main/resources/mapper/permission/UserMapper.xml +++ b/src/main/resources/mapper/permission/UserMapper.xml @@ -9,6 +9,8 @@ t_user.expiration as user_expiration, t_user.credentials_expiration as user_credentials_expiration, t_user.enable as user_enable, + t_user.current_login_time as user_current_login_time, + t_user.current_login_ip as user_current_login_ip, t_user.last_login_time as user_last_login_time, t_user.last_login_ip as user_last_login_ip, t_user.create_time as user_create_time, @@ -54,6 +56,8 @@ + +