Merge pull request #3906 from dataease/pr@dev@fix_login_error_msg

fix(登录): 登录失败提示信息
This commit is contained in:
Junjun 2022-11-28 17:06:31 +08:00 committed by GitHub
commit c7ce770914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 13 deletions

View File

@ -12,4 +12,6 @@ public class AccountLockStatus {
private Long unlockTime;
private Integer relieveTimes;
private Integer remainderTimes;
}

View File

@ -81,8 +81,8 @@ public class AuthServer implements AuthApi {
ValidateResult<XpackLdapUserEntity> 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();

View File

@ -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);

View File

@ -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<SysLoginLimit> 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);
}
}

View File

@ -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

View File

@ -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

View File

@ -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