登录接口增加日志

develop
Hoshi 2024-11-12 21:22:41 +08:00
parent f611a65475
commit 756a4370d4
1 changed files with 57 additions and 52 deletions

View File

@ -63,65 +63,70 @@ public class LoginController {
@PostMapping("/users/login")
public GeneralResult<Map<String, Object>> userLogin(@RequestBody LoginModel loginModel,
HttpServletRequest request) {
log.info("开始执行登录请求");
long start;
String account = loginModel.getAccount();
String password = loginModel.getPassword();
log.info("开始调用UserService");
GeneralResult<Map<String, Object>> 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<Map<String, Object>> 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;
}
/**