From 38c3986ba443107eac2172dccdd738ac1dbd7685 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Mon, 28 Nov 2022 16:18:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=99=BB=E5=BD=95):=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=8F=90=E7=A4=BA=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/entity/AccountLockStatus.java | 2 ++ .../io/dataease/auth/server/AuthServer.java | 29 ++++++++++++------- .../auth/service/AuthUserService.java | 2 +- .../service/impl/AuthUserServiceImpl.java | 9 ++++-- .../resources/i18n/messages_en_US.properties | 1 + .../resources/i18n/messages_zh_CN.properties | 1 + .../resources/i18n/messages_zh_TW.properties | 1 + 7 files changed, 32 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/io/dataease/auth/entity/AccountLockStatus.java b/backend/src/main/java/io/dataease/auth/entity/AccountLockStatus.java index 8555d2c381..cc37ef3cb4 100644 --- a/backend/src/main/java/io/dataease/auth/entity/AccountLockStatus.java +++ b/backend/src/main/java/io/dataease/auth/entity/AccountLockStatus.java @@ -12,4 +12,6 @@ public class AccountLockStatus { private Long unlockTime; private Integer relieveTimes; + + private Integer remainderTimes; } diff --git a/backend/src/main/java/io/dataease/auth/server/AuthServer.java b/backend/src/main/java/io/dataease/auth/server/AuthServer.java index 4455484c75..44fe78b13e 100644 --- a/backend/src/main/java/io/dataease/auth/server/AuthServer.java +++ b/backend/src/main/java/io/dataease/auth/server/AuthServer.java @@ -81,8 +81,8 @@ public class AuthServer implements AuthApi { ValidateResult validateResult = ldapXpackService.login(request); if (!validateResult.isSuccess()) { - authUserService.recordLoginFail(username, 1); - DataEaseException.throwException(validateResult.getMsg()); + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 1); + DataEaseException.throwException(appendLoginErrorMsg(validateResult.getMsg(), lockStatus)); } XpackLdapUserEntity ldapUserEntity = validateResult.getData(); if (StringUtils.isBlank(ldapUserEntity.getEmail())) { @@ -120,19 +120,19 @@ public class AuthServer implements AuthApi { SysUserEntity user = authUserService.getUserByName(username); if (ObjectUtils.isEmpty(user)) { - authUserService.recordLoginFail(username, 0); - DataEaseException.throwException(Translator.get("i18n_user_do_not_exist")); + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_id_or_pwd_error"), lockStatus)); } // 验证登录类型是否与用户类型相同 if (!sysUserService.validateLoginType(user.getFrom(), loginType)) { - authUserService.recordLoginFail(username, 0); - DataEaseException.throwException(Translator.get("i18n_login_type_error")); + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_login_type_error"), lockStatus)); } if (user.getEnabled() == 0) { - authUserService.recordLoginFail(username, 0); - DataEaseException.throwException(Translator.get("i18n_user_is_disable")); + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_user_is_disable"), lockStatus)); } String realPwd = user.getPassword(); @@ -144,8 +144,8 @@ public class AuthServer implements AuthApi { pwd = CodingUtil.md5(pwd); if (!StringUtils.equals(pwd, realPwd)) { - authUserService.recordLoginFail(username, 0); - DataEaseException.throwException(Translator.get("i18n_id_or_pwd_error")); + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_id_or_pwd_error"), lockStatus)); } } @@ -161,6 +161,15 @@ public class AuthServer implements AuthApi { return result; } + private String appendLoginErrorMsg(String msg, AccountLockStatus lockStatus) { + if (ObjectUtils.isEmpty(lockStatus)) return msg; + if (ObjectUtils.isNotEmpty(lockStatus.getRemainderTimes())) { + String i18n = Translator.get("i18n_login_remainder_times"); + msg += String.format(i18n, lockStatus.getRemainderTimes()); + } + return msg; + } + @Override public CurrentUserDto userInfo() { CurrentUserDto userDto = (CurrentUserDto) SecurityUtils.getSubject().getPrincipal(); diff --git a/backend/src/main/java/io/dataease/auth/service/AuthUserService.java b/backend/src/main/java/io/dataease/auth/service/AuthUserService.java index e8704f5dc5..6db8a61695 100644 --- a/backend/src/main/java/io/dataease/auth/service/AuthUserService.java +++ b/backend/src/main/java/io/dataease/auth/service/AuthUserService.java @@ -55,7 +55,7 @@ public interface AuthUserService { void checkAdmin(String uname, String pwd); - void recordLoginFail(String username, Integer logintype); + AccountLockStatus recordLoginFail(String username, Integer logintype); void unlockAccount(String username, Integer logintype); diff --git a/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java b/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java index f5329eb743..c65ef79ab6 100644 --- a/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java +++ b/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java @@ -277,14 +277,16 @@ public class AuthUserServiceImpl implements AuthUserService { } @Override - public void recordLoginFail(String username, Integer logintype) { - if (!supportLoginLimit()) return; + public AccountLockStatus recordLoginFail(String username, Integer logintype) { + if (!supportLoginLimit()) return null; long now = System.currentTimeMillis(); SysLoginLimit sysLoginLimit = new SysLoginLimit(); sysLoginLimit.setUsername(username); sysLoginLimit.setLoginType(logintype); sysLoginLimit.setRecordTime(now); sysLoginLimitMapper.insert(sysLoginLimit); + return lockStatus(username, logintype); + } @Override @@ -312,13 +314,16 @@ public class AuthUserServiceImpl implements AuthUserService { SysLoginLimitExample example = new SysLoginLimitExample(); example.createCriteria().andUsernameEqualTo(username).andLoginTypeEqualTo(logintype).andRecordTimeGreaterThan(dividingPointTime); List sysLoginLimits = sysLoginLimitMapper.selectByExample(example); + accountLockStatus.setRemainderTimes(limitTimes); if (CollectionUtils.isNotEmpty(sysLoginLimits)) { boolean needLock = sysLoginLimits.size() >= limitTimes; + accountLockStatus.setRemainderTimes(limitTimes - sysLoginLimits.size()); accountLockStatus.setLocked(needLock); if (needLock) { long unlockTime = now + (longRelieveTimes * 60L * 1000L); accountLockStatus.setUnlockTime(unlockTime); accountLockStatus.setRelieveTimes(relieveTimes); + accountLockStatus.setRemainderTimes(0); } } diff --git a/backend/src/main/resources/i18n/messages_en_US.properties b/backend/src/main/resources/i18n/messages_en_US.properties index 951dca55a6..44b053e949 100644 --- a/backend/src/main/resources/i18n/messages_en_US.properties +++ b/backend/src/main/resources/i18n/messages_en_US.properties @@ -75,6 +75,7 @@ i18n_sql_not_empty=SQL can not be empty. i18n_datasource_not_allow_delete_msg=datasets are using this data source and cannot be deleted i18n_task_name_repeat=Name is used in same data set i18n_id_or_pwd_error=Invalid ID or password +i18n_login_remainder_times=(You can still enter %s times) i18n_user_do_not_exist=User do not exist i18n_user_is_disable=User is disabled i18n_login_type_error=Login type error diff --git a/backend/src/main/resources/i18n/messages_zh_CN.properties b/backend/src/main/resources/i18n/messages_zh_CN.properties index 6ec5cc2064..d51c0e69cf 100644 --- a/backend/src/main/resources/i18n/messages_zh_CN.properties +++ b/backend/src/main/resources/i18n/messages_zh_CN.properties @@ -75,6 +75,7 @@ i18n_sql_not_empty=SQL \u4E0D\u80FD\u4E3A\u7A7A i18n_datasource_not_allow_delete_msg=\u4E2A\u6570\u636E\u96C6\u6B63\u5728\u4F7F\u7528\u6B64\u6570\u636E\u6E90\uFF0C\u65E0\u6CD5\u5220\u9664 i18n_task_name_repeat=\u540C\u4E00\u6570\u636E\u96C6\u4E0B\u4EFB\u52A1\u540D\u79F0\u5DF2\u88AB\u4F7F\u7528 i18n_id_or_pwd_error=\u65E0\u6548\u7684ID\u6216\u5BC6\u7801 +i18n_login_remainder_times=(\u8FD8\u80FD\u8F93\u5165%s\u6B21) i18n_user_do_not_exist=\u7528\u6237\u4E0D\u5B58\u5728 i18n_user_is_disable=\u7528\u6237\u72B6\u6001\u65E0\u6548 i18n_login_type_error=\u767B\u5F55\u65B9\u5F0F\u9519\u8BEF diff --git a/backend/src/main/resources/i18n/messages_zh_TW.properties b/backend/src/main/resources/i18n/messages_zh_TW.properties index 0f0c6d30f2..37072949cb 100644 --- a/backend/src/main/resources/i18n/messages_zh_TW.properties +++ b/backend/src/main/resources/i18n/messages_zh_TW.properties @@ -75,6 +75,7 @@ i18n_sql_not_empty=SQL \u4E0D\u80FD\u70BA\u7A7A i18n_datasource_not_allow_delete_msg=\u500B\u6578\u64DA\u96C6\u6B63\u5728\u4F7F\u7528\u6B64\u6578\u64DA\u6E90\uFF0C\u7121\u6CD5\u522A\u9664 i18n_task_name_repeat=\u540C\u4E00\u6578\u64DA\u96C6\u4E0B\u4EFB\u52D9\u540D\u7A31\u5DF2\u88AB\u4F7F\u7528 i18n_id_or_pwd_error=\u7121\u6548\u7684ID\u6216\u5BC6\u78BC +i18n_login_remainder_times=(\u9084\u80FD\u8F38\u5165%s\u6B21) i18n_user_do_not_exist=\u7528\u6236\u4E0D\u5B58\u5728 i18n_user_is_disable=\u7528\u6236\u72C0\u614B\u7121\u6548 i18n_login_type_error=\u767B\u9304\u65B9\u5F0F\u932F\u8AA4