update 更新 SysLogininfor 字段 (去掉 user_type) ;

update 更新登录相关方法参数, 去掉 userType ;
update 更新 mysql 脚本 ;
add 新增其他数据库更新脚本 ;
This commit is contained in:
Michelle.Chung 2023-10-22 14:05:55 +08:00
parent 849803e888
commit 7b60739e5d
17 changed files with 76 additions and 46 deletions

View File

@ -99,7 +99,7 @@ public class SysLoginService {
// 超级管理员 登出清除动态租户
TenantHelper.clearDynamic();
}
recordLogininfor(loginUser.getTenantId(), loginUser.getUsername(), loginUser.getUserType(), Constants.LOGOUT, MessageUtils.message("user.logout.success"));
recordLogininfor(loginUser.getTenantId(), loginUser.getUsername(), Constants.LOGOUT, MessageUtils.message("user.logout.success"));
} catch (NotLoginException ignored) {
} finally {
try {
@ -114,15 +114,13 @@ public class SysLoginService {
*
* @param tenantId 租户ID
* @param username 用户名
* @param userType 用户类型
* @param status 状态
* @param message 消息内容
*/
public void recordLogininfor(String tenantId, String username, String userType, String status, String message) {
public void recordLogininfor(String tenantId, String username, String status, String message) {
LogininforEvent logininforEvent = new LogininforEvent();
logininforEvent.setTenantId(tenantId);
logininforEvent.setUsername(username);
logininforEvent.setUserType(userType);
logininforEvent.setStatus(status);
logininforEvent.setMessage(message);
logininforEvent.setRequest(ServletUtils.getRequest());
@ -166,7 +164,7 @@ public class SysLoginService {
/**
* 登录校验
*/
public void checkLogin(LoginType loginType, String tenantId, String username, String userType, Supplier<Boolean> supplier) {
public void checkLogin(LoginType loginType, String tenantId, String username, Supplier<Boolean> supplier) {
String errorKey = GlobalConstants.PWD_ERR_CNT_KEY + username;
String loginFail = Constants.LOGIN_FAIL;
@ -174,7 +172,7 @@ public class SysLoginService {
int errorNumber = ObjectUtil.defaultIfNull(RedisUtils.getCacheObject(errorKey), 0);
// 锁定时间内登录 则踢出
if (errorNumber >= maxRetryCount) {
recordLogininfor(tenantId, username, userType, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime));
recordLogininfor(tenantId, username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime));
throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime);
}
@ -184,11 +182,11 @@ public class SysLoginService {
RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(lockTime));
// 达到规定错误次数 则锁定登录
if (errorNumber >= maxRetryCount) {
recordLogininfor(tenantId, username, userType, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime));
recordLogininfor(tenantId, username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime));
throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime);
} else {
// 未达到规定错误次数
recordLogininfor(tenantId, username, userType, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber));
recordLogininfor(tenantId, username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber));
throw new UserException(loginType.getRetryLimitCount(), errorNumber);
}
}

View File

@ -56,9 +56,8 @@ public class EmailAuthStrategy implements IAuthStrategy {
// 通过邮箱查找用户
SysUserVo user = loadUserByEmail(tenantId, email);
String userType = user.getUserType();
loginService.checkLogin(LoginType.EMAIL, tenantId, user.getUserName(), userType, () -> !validateEmailCode(tenantId, email, emailCode, userType));
loginService.checkLogin(LoginType.EMAIL, tenantId, user.getUserName(), () -> !validateEmailCode(tenantId, email, emailCode));
// 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
LoginUser loginUser = loginService.buildLoginUser(user);
loginUser.setClientKey(client.getClientKey());
@ -73,7 +72,7 @@ public class EmailAuthStrategy implements IAuthStrategy {
// 生成token
LoginHelper.login(loginUser, model);
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), userType, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLoginInfo(user.getUserId());
LoginVo loginVo = new LoginVo();
@ -86,10 +85,10 @@ public class EmailAuthStrategy implements IAuthStrategy {
/**
* 校验邮箱验证码
*/
private boolean validateEmailCode(String tenantId, String email, String emailCode, String userType) {
private boolean validateEmailCode(String tenantId, String email, String emailCode) {
String code = RedisUtils.getCacheObject(GlobalConstants.CAPTCHA_CODE_KEY + email);
if (StringUtils.isBlank(code)) {
loginService.recordLogininfor(tenantId, email, userType, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
loginService.recordLogininfor(tenantId, email, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
throw new CaptchaExpireException();
}
return code.equals(emailCode);

View File

@ -60,16 +60,14 @@ public class PasswordAuthStrategy implements IAuthStrategy {
String code = loginBody.getCode();
String uuid = loginBody.getUuid();
SysUserVo user = loadUserByUsername(tenantId, username);
String userType = user.getUserType();
boolean captchaEnabled = captchaProperties.getEnable();
// 验证码开关
if (captchaEnabled) {
validateCaptcha(tenantId, username, userType, code, uuid);
validateCaptcha(tenantId, username, code, uuid);
}
loginService.checkLogin(LoginType.PASSWORD, tenantId, username, userType, () -> !BCrypt.checkpw(password, user.getPassword()));
SysUserVo user = loadUserByUsername(tenantId, username);
loginService.checkLogin(LoginType.PASSWORD, tenantId, username, () -> !BCrypt.checkpw(password, user.getPassword()));
// 此处可根据登录用户的数据不同 自行创建 loginUser
LoginUser loginUser = loginService.buildLoginUser(user);
loginUser.setClientKey(client.getClientKey());
@ -84,7 +82,7 @@ public class PasswordAuthStrategy implements IAuthStrategy {
// 生成token
LoginHelper.login(loginUser, model);
loginService.recordLogininfor(loginUser.getTenantId(), username, userType, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLogininfor(loginUser.getTenantId(), username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLoginInfo(user.getUserId());
LoginVo loginVo = new LoginVo();
@ -101,16 +99,16 @@ public class PasswordAuthStrategy implements IAuthStrategy {
* @param code 验证码
* @param uuid 唯一标识
*/
private void validateCaptcha(String tenantId, String username, String userType, String code, String uuid) {
private void validateCaptcha(String tenantId, String username, String code, String uuid) {
String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
String captcha = RedisUtils.getCacheObject(verifyKey);
RedisUtils.deleteObject(verifyKey);
if (captcha == null) {
loginService.recordLogininfor(tenantId, username, userType, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
loginService.recordLogininfor(tenantId, username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
throw new CaptchaExpireException();
}
if (!code.equalsIgnoreCase(captcha)) {
loginService.recordLogininfor(tenantId, username, userType, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
loginService.recordLogininfor(tenantId, username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
throw new CaptchaException();
}
}

View File

@ -56,9 +56,8 @@ public class SmsAuthStrategy implements IAuthStrategy {
// 通过手机号查找用户
SysUserVo user = loadUserByPhonenumber(tenantId, phonenumber);
String userType = user.getUserType();
loginService.checkLogin(LoginType.SMS, tenantId, user.getUserName(), userType, () -> !validateSmsCode(tenantId, phonenumber, smsCode, userType));
loginService.checkLogin(LoginType.SMS, tenantId, user.getUserName(), () -> !validateSmsCode(tenantId, phonenumber, smsCode));
// 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
LoginUser loginUser = loginService.buildLoginUser(user);
loginUser.setClientKey(client.getClientKey());
@ -73,7 +72,7 @@ public class SmsAuthStrategy implements IAuthStrategy {
// 生成token
LoginHelper.login(loginUser, model);
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), userType, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLoginInfo(user.getUserId());
LoginVo loginVo = new LoginVo();
@ -86,10 +85,10 @@ public class SmsAuthStrategy implements IAuthStrategy {
/**
* 校验短信验证码
*/
private boolean validateSmsCode(String tenantId, String phonenumber, String smsCode, String userType) {
private boolean validateSmsCode(String tenantId, String phonenumber, String smsCode) {
String code = RedisUtils.getCacheObject(GlobalConstants.CAPTCHA_CODE_KEY + phonenumber);
if (StringUtils.isBlank(code)) {
loginService.recordLogininfor(tenantId, phonenumber, userType, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
loginService.recordLogininfor(tenantId, phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
throw new CaptchaExpireException();
}
return code.equals(smsCode);

View File

@ -109,7 +109,7 @@ public class SocialAuthStrategy implements IAuthStrategy {
// 生成token
LoginHelper.login(loginUser, model);
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), user.getUserType(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLoginInfo(user.getUserId());
LoginVo loginVo = new LoginVo();

View File

@ -68,7 +68,7 @@ public class XcxAuthStrategy implements IAuthStrategy {
// 生成token
LoginHelper.login(loginUser, model);
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), user.getUserType(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLogininfor(loginUser.getTenantId(), user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
loginService.recordLoginInfo(user.getUserId());
LoginVo loginVo = new LoginVo();

View File

@ -37,11 +37,6 @@ public class SysLogininfor implements Serializable {
*/
private String userName;
/**
* 用户类型
*/
private String userType;
/**
* 客户端
*/

View File

@ -33,11 +33,6 @@ public class SysLogininforBo {
*/
private String userName;
/**
* 用户类型
*/
private String userType;
/**
* 客户端
*/

View File

@ -45,12 +45,6 @@ public class SysLogininforVo implements Serializable {
@ExcelProperty(value = "用户账号")
private String userName;
/**
* 用户类型
*/
@ExcelProperty(value = "用户类型")
private String userType;
/**
* 客户端
*/

View File

@ -925,6 +925,8 @@ create table sys_logininfor (
info_id number(20) not null,
tenant_id varchar2(20) default '000000',
user_name varchar2(50) default '',
client_key varchar2(32) default '',
device_type varchar2(32) default '',
ipaddr varchar2(128) default '',
login_location varchar2(255) default '',
browser varchar2(50) default '',
@ -942,6 +944,8 @@ comment on table sys_logininfor is '系统访问记录';
comment on column sys_logininfor.info_id is '访问ID';
comment on column sys_logininfor.tenant_id is '租户编号';
comment on column sys_logininfor.user_name is '登录账号';
comment on column sys_logininfor.client_key is '客户端';
comment on column sys_logininfor.device_type is '设备类型';
comment on column sys_logininfor.ipaddr is '登录IP地址';
comment on column sys_logininfor.login_location is '登录地点';
comment on column sys_logininfor.browser is '浏览器类型';

View File

@ -946,6 +946,8 @@ create table if not exists sys_logininfor
info_id int8,
tenant_id varchar(20) default '000000'::varchar,
user_name varchar(50) default ''::varchar,
client_key varchar(32) default ''::varchar,
device_type varchar(32) default ''::varchar,
ipaddr varchar(128) default ''::varchar,
login_location varchar(255) default ''::varchar,
browser varchar(50) default ''::varchar,
@ -963,6 +965,8 @@ comment on table sys_logininfor is '系统访问记录';
comment on column sys_logininfor.info_id is '访问ID';
comment on column sys_logininfor.tenant_id is '租户编号';
comment on column sys_logininfor.user_name is '用户账号';
comment on column sys_logininfor.client_key is '客户端';
comment on column sys_logininfor.device_type is '设备类型';
comment on column sys_logininfor.ipaddr is '登录IP地址';
comment on column sys_logininfor.login_location is '登录地点';
comment on column sys_logininfor.browser is '浏览器类型';

View File

@ -699,6 +699,8 @@ create table sys_logininfor (
info_id bigint(20) not null comment '访问ID',
tenant_id varchar(20) default '000000' comment '租户编号',
user_name varchar(50) default '' comment '用户账号',
client_key varchar(32) default '' comment '客户端',
device_type varchar(32) default '' comment '设备类型',
ipaddr varchar(128) default '' comment '登录IP地址',
login_location varchar(255) default '' comment '登录地点',
browser varchar(50) default '' comment '浏览器类型',

View File

@ -1383,6 +1383,8 @@ CREATE TABLE sys_logininfor
info_id bigint NOT NULL,
tenant_id nvarchar(20) DEFAULT ('000000') NULL,
user_name nvarchar(50) DEFAULT '' NULL,
client_key nvarchar(32) DEFAULT '' NULL,
device_type nvarchar(32) DEFAULT '' NULL,
ipaddr nvarchar(128) DEFAULT '' NULL,
login_location nvarchar(255) DEFAULT '' NULL,
browser nvarchar(50) DEFAULT '' NULL,
@ -1420,6 +1422,18 @@ EXEC sys.sp_addextendedproperty
'TABLE', N'sys_logininfor',
'COLUMN', N'user_name'
GO
EXEC sys.sp_addextendedproperty
'MS_Description', N'客户端' ,
'SCHEMA', N'dbo',
'TABLE', N'sys_logininfor',
'COLUMN', N'client_key'
GO
EXEC sys.sp_addextendedproperty
'MS_Description', N'设备类型' ,
'SCHEMA', N'dbo',
'TABLE', N'sys_logininfor',
'COLUMN', N'device_type'
GO
EXEC sys.sp_addextendedproperty
'MS_Description', N'登录IP地址' ,
'SCHEMA', N'dbo',

View File

@ -0,0 +1,5 @@
ALTER TABLE sys_logininfor ADD (client_key VARCHAR(32) DEFAULT '');
COMMENT ON COLUMN sys_logininfor.client_key IS '客户端';
ALTER TABLE sys_logininfor ADD (device_type VARCHAR(32) DEFAULT '');
COMMENT ON COLUMN sys_logininfor.device_type IS '设备类型';

View File

@ -0,0 +1,5 @@
ALTER TABLE sys_logininfor ADD client_key varchar(32) default ''::varchar;
COMMENT ON COLUMN sys_logininfor.client_key IS '客户端';
ALTER TABLE sys_logininfor ADD device_type varchar(32) default ''::varchar;
COMMENT ON COLUMN sys_logininfor.device_type IS '设备类型';

View File

@ -0,0 +1,19 @@
ALTER TABLE sys_logininfor ADD client_key nvarchar(32) DEFAULT '' NULL
GO
EXEC sp_addextendedproperty
'MS_Description', N'客户端',
'SCHEMA', N'dbo',
'TABLE', N'sys_logininfor',
'COLUMN', N'client_key'
GO
ALTER TABLE sys_logininfor ADD device_type nvarchar(32) DEFAULT '' NULL
GO
EXEC sp_addextendedproperty
'MS_Description', N'设备类型',
'SCHEMA', N'dbo',
'TABLE', N'sys_logininfor',
'COLUMN', N'device_type'
GO

View File

@ -1,4 +1,3 @@
ALTER TABLE sys_logininfor
ADD COLUMN user_type VARCHAR(10) NULL DEFAULT 'sys_user' COMMENT '用户类型sys_user系统用户' AFTER `user_name`,
ADD COLUMN client_key VARCHAR(32) NULL DEFAULT NULL COMMENT '客户端' AFTER `user_type`,
ADD COLUMN device_type VARCHAR(32) NULL DEFAULT NULL COMMENT '设备类型' AFTER `client_key`;