/**
* 登录方法
*
* @param loginBody 登录信息
* @return 结果
*/
@PostMapping("/login")
public R<String> login(@RequestBody LoginBody loginBody) {
String username = loginBody.getUsername();
String loginErrorGt5Key = StringUtils.format("login_error_gt5:{}", username);
try {
String str = Convert.toStr(redisCache.getCacheObject(loginErrorGt5Key));
if (StringUtils.isNotEmpty(str)) {
return R.failed("密码错误5次,账号锁定10分钟,请稍后再试!");
}
String password = loginBody.getPassword();
String code = loginBody.getCode();
String uuid = loginBody.getUuid();
password = RSAUtils.decryptByPrivateKey(password);
String token = loginService.login(username, password, code, uuid);
ServletUtils.getResponse().setHeader(Constants.TOKEN, token);
String loginErrorCountKey = StringUtils.format("login_error_count:{}", username);
redisCache.deleteObject(loginErrorCountKey);
return R.ok();
} catch (Exception e) {
if (e instanceof UserPasswordNotMatchException) {
String loginErrorCountKey = StringUtils.format("login_error_count:{}", username);
Integer loginErrorCount = Convert.toInt(redisCache.getCacheObject(loginErrorCountKey));
if (loginErrorCount == null) {
redisCache.setCacheObject(loginErrorCountKey, 1);
} else {
loginErrorCount = loginErrorCount + 1;
if (loginErrorCount >= 5) {
redisCache.setCacheObject(loginErrorGt5Key, username, 10, TimeUnit.MINUTES);
redisCache.deleteObject(loginErrorCountKey);
} else {
redisCache.setCacheObject(loginErrorCountKey, loginErrorCount);
}
}
}
return R.failed(e.getMessage());
}
}
版权归属:
javalx
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区