1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
public String login(String username, String password, String code, String uuid) { HttpServletRequest request = ServletUtils.getRequest(); boolean captchaEnabled = configService.selectCaptchaEnabled(); SysUser user = loadUserByUsername(username); checkLogin(LoginType.PASSWORD, username, () -> !BCrypt.checkpw(password, user.getPassword())); LoginUser loginUser = buildLoginUser(user); LoginHelper.loginByDevice(loginUser, DeviceType.PC);
recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); recordLoginInfo(user.getUserId(), username); return StpUtil.getTokenValue(); }
private SysUser loadUserByUsername(String username) { SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() .select(SysUser::getUserName, SysUser::getStatus) .eq(SysUser::getUserName, username)); if (ObjectUtil.isNull(user)) { log.info("登录用户:{} 不存在.", username); throw new UserException("user.not.exists", username); } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { log.info("登录用户:{} 已被停用.", username); throw new UserException("user.blocked", username); } return userMapper.selectUserByUserName(username); }
private void checkLogin(LoginType loginType, String username, Supplier<Boolean> supplier) { String errorKey = CacheConstants.PWD_ERR_CNT_KEY + username; String loginFail = Constants.LOGIN_FAIL;
Integer errorNumber = RedisUtils.getCacheObject(errorKey); if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(maxRetryCount)) { recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime)); throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime); }
if (supplier.get()) { errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1; if (errorNumber.equals(maxRetryCount)) { RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(lockTime)); recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime)); throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime); } else { RedisUtils.setCacheObject(errorKey, errorNumber); recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber)); throw new UserException(loginType.getRetryLimitCount(), errorNumber); } }
RedisUtils.deleteObject(errorKey); }
private void recordLogininfor(String username, String status, String message) { LoginInEvent loginInEvent = new LoginInEvent(); loginInEvent.setUsername(username); loginInEvent.setStatus(status); loginInEvent.setMessage(message); loginInEvent.setRequest(ServletUtils.getRequest()); SpringUtils.context().publishEvent(loginInEvent); }
public void recordLoginInfo(Long userId, String username) { SysUser sysUser = new SysUser(); sysUser.setUserId(userId); sysUser.setLoginIp(ServletUtils.getClientIP()); sysUser.setLoginDate(DateUtils.getNowDate()); sysUser.setUpdateBy(username); userMapper.updateById(sysUser); }
|