From 756a4370d4e298d3c429d54dc29672c4cb52eea4 Mon Sep 17 00:00:00 2001 From: Hoshi <1196756653@qq.com> Date: Tue, 12 Nov 2024 21:22:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../booter/controller/LoginController.java | 109 +++++++++--------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java index 05392de..607daa0 100644 --- a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java +++ b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java @@ -63,65 +63,70 @@ public class LoginController { @PostMapping("/users/login") public GeneralResult> userLogin(@RequestBody LoginModel loginModel, HttpServletRequest request) { - log.info("开始执行登录请求"); - long start; - String account = loginModel.getAccount(); - String password = loginModel.getPassword(); - log.info("开始调用UserService"); - GeneralResult> loginResult = userService - .login(account, password, request.getSession().getId(), IpTool.getIP(request), loginModel.getIsManager()); - log.info("请求方ip地址trx:" + trxService.getRequestIpAddress(request)); - log.info("请求方ip地址:" + IpTool.getIP(request)); - log.info("结束调用UserService"); - if (loginResult.isFailed()) { - if (null == loginResult.getData()) { - String path = "Login_" + account; - try (com.megatron.framework.lock.AutoCloseLock lock = lockFactory.getACLock(path)) { - Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时"); - start = System.currentTimeMillis(); - String value = redisTemplate.opsForValue().get(RequestContext.Catalog.User.name() + account); - log.info("查询Redis耗时{}ms", System.currentTimeMillis() - start); - SystemConfig errorNumber = systemConfigRepository.queryByCode("pwdErrorNumber"); - int error = Integer.parseInt(errorNumber.getValue()); - if (!StringUtils.hasText(value)) { - //密码操作失败 + try { + log.info("开始执行登录请求"); + long start; + String account = loginModel.getAccount(); + String password = loginModel.getPassword(); + log.info("开始调用UserService"); + GeneralResult> loginResult = userService + .login(account, password, request.getSession().getId(), IpTool.getIP(request), loginModel.getIsManager()); + log.info("请求方ip地址trx:" + trxService.getRequestIpAddress(request)); + log.info("请求方ip地址:" + IpTool.getIP(request)); + log.info("结束调用UserService"); + if (loginResult.isFailed()) { + if (null == loginResult.getData()) { + String path = "Login_" + account; + try (com.megatron.framework.lock.AutoCloseLock lock = lockFactory.getACLock(path)) { + Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时"); start = System.currentTimeMillis(); - redisTemplate.opsForValue() - .set(RequestContext.Catalog.User.name() + account, String.valueOf(1), 60, TimeUnit.SECONDS); - log.info("写入Redis耗时{}ms", System.currentTimeMillis() - start); - loginResult = new GeneralResult<>(false, loginResult.getMessage() + ",已操作【1】次"); - } else if (Integer.parseInt(value) >= error) { - Result lockResult = userService.lockByAccount(account); - boolean success = lockResult.isSuccess(); - start = System.currentTimeMillis(); - redisTemplate.delete(RequestContext.Catalog.User.name() + account); - log.info("删除Redis耗时{}ms", System.currentTimeMillis() - start); - if (success) { - loginResult = new GeneralResult<>(false, "账号或者密码错误已达【" + error + "】次,若用户存在将被冻结,请联系管理员解冻"); + String value = redisTemplate.opsForValue().get(RequestContext.Catalog.User.name() + account); + log.info("查询Redis耗时{}ms", System.currentTimeMillis() - start); + SystemConfig errorNumber = systemConfigRepository.queryByCode("pwdErrorNumber"); + int error = Integer.parseInt(errorNumber.getValue()); + if (!StringUtils.hasText(value)) { + //密码操作失败 + start = System.currentTimeMillis(); + redisTemplate.opsForValue() + .set(RequestContext.Catalog.User.name() + account, String.valueOf(1), 60, TimeUnit.SECONDS); + log.info("写入Redis耗时{}ms", System.currentTimeMillis() - start); + loginResult = new GeneralResult<>(false, loginResult.getMessage() + ",已操作【1】次"); + } else if (Integer.parseInt(value) >= error) { + Result lockResult = userService.lockByAccount(account); + boolean success = lockResult.isSuccess(); + start = System.currentTimeMillis(); + redisTemplate.delete(RequestContext.Catalog.User.name() + account); + log.info("删除Redis耗时{}ms", System.currentTimeMillis() - start); + if (success) { + loginResult = new GeneralResult<>(false, "账号或者密码错误已达【" + error + "】次,若用户存在将被冻结,请联系管理员解冻"); + } else { + loginResult = new GeneralResult<>(false, "认证服务账号缓存存在问题"); + } } else { - loginResult = new GeneralResult<>(false, "认证服务账号缓存存在问题"); + start = System.currentTimeMillis(); + redisTemplate.boundValueOps(RequestContext.Catalog.User.name() + account).increment(1L); + log.info("写入Redis耗时{}ms", System.currentTimeMillis() - start); + String num = redisTemplate.opsForValue().get(RequestContext.Catalog.User.name() + account); + loginResult = new GeneralResult<>(false, loginResult.getMessage() + ",已操作【" + num + "】次"); } - } else { - start = System.currentTimeMillis(); - redisTemplate.boundValueOps(RequestContext.Catalog.User.name() + account).increment(1L); - log.info("写入Redis耗时{}ms", System.currentTimeMillis() - start); - String num = redisTemplate.opsForValue().get(RequestContext.Catalog.User.name() + account); - loginResult = new GeneralResult<>(false, loginResult.getMessage() + ",已操作【" + num + "】次"); + log.info("登录请求处理完成"); + return loginResult; + } catch (Exception e) { + log.error("Get login error message:", e); + log.info("登录请求处理完成"); + return loginResult; } - log.info("登录请求处理完成"); - return loginResult; - } catch (Exception e) { - log.error("Get login error message:", e); - log.info("登录请求处理完成"); - return loginResult; } } + start = System.currentTimeMillis(); + redisTemplate.delete(RequestContext.Catalog.User.name() + account); + log.info("删除Redis耗时{}ms", System.currentTimeMillis() - start); + log.info("登录请求处理完成"); + return loginResult; + }catch (Exception e){ + log.error("登录异常",e); + return new GeneralResult<>(false,"登录异常:" + e.getMessage()); } - start = System.currentTimeMillis(); - redisTemplate.delete(RequestContext.Catalog.User.name() + account); - log.info("删除Redis耗时{}ms", System.currentTimeMillis() - start); - log.info("登录请求处理完成"); - return loginResult; } /**