Commit 6d117c30 by weisong
parents 859c5c90 e2c6eb65
......@@ -5,6 +5,9 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;
@Service
public class AccountLockService {
......@@ -35,7 +38,7 @@ public class AccountLockService {
private void lockAccount(String account, int lockMinutes) {
String lockKey = getLockKey(account);
String lockValue = LocalDateTime.now().toString() + "|" + lockMinutes;
redisTemplate.opsForValue().set(lockKey, lockValue, lockMinutes, TimeUnit.MINUTES);//这个超时时间需要考虑一下
redisTemplate.opsForValue().set(lockKey, lockValue, lockMinutes*60, TimeUnit.SECONDS);//这个超时时间需要考虑一下
}
/**
* 手动解锁账号
......@@ -72,11 +75,13 @@ public class AccountLockService {
//long failureCount = jedis.incr(attemptKey);
String failureCountStr = redisTemplate.opsForValue().get(attemptKey)!=null?redisTemplate.opsForValue().get(attemptKey).toString():"0";
long failureCount = Long.parseLong(failureCountStr)+1;
redisTemplate.opsForValue().set(attemptKey,String.valueOf(failureCount));
LocalDateTime now = LocalDateTime.now();
LocalDateTime midnight = now.toLocalDate().atTime(LocalTime.MAX);
redisTemplate.opsForValue().set(attemptKey,String.valueOf(failureCount),ChronoUnit.SECONDS.between(now, midnight), TimeUnit.SECONDS);
// 设置计数过期时间(避免无限增长)
if (failureCount == 1) {
redisTemplate.expire(attemptKey, 24 * 60, TimeUnit.MINUTES); // 24小时过期
redisTemplate.expire(attemptKey, 24 * 60 * 60, TimeUnit.SECONDS); // 24小时过期
}
// 根据失败次数确定锁定时间
......@@ -98,13 +103,16 @@ public class AccountLockService {
/**
* 获取锁定剩余时间
*/
public long getLockRemainingTime(String account) {
public String getLockRemainingTime(String account) {
String lockKey = getLockKey(account);
boolean isExist = Boolean.TRUE.equals(redisTemplate.hasKey(lockKey));
if(isExist){
return redisTemplate.getExpire(lockKey,TimeUnit.MINUTES);
LocalDateTime now = LocalDateTime.now();
LocalDateTime futureTime = now.plusSeconds(redisTemplate.getExpire(lockKey,TimeUnit.SECONDS));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
return futureTime.format(formatter);
}else{
return 0;
return "";
}
}
}
......@@ -223,6 +223,8 @@ public class LoginServiceImpl implements LoginService {
if(expertInfoResp == null){
throw new BusinessException(GlobalErrorCodeConstants.USER_NOT_EXISTS.getCode(),GlobalErrorCodeConstants.USER_NOT_EXISTS.getMsg());
}
//expertInfoResp = new ExpertInfoResp();
//expertInfoResp.setZhuanJiaGuid("1234");
boolean isAccountLocked = accountLockService.isAccountLocked(expertInfoResp.getZhuanJiaGuid());
if(isAccountLocked){
//如果账号锁定了,返回错误信息
......@@ -236,8 +238,6 @@ public class LoginServiceImpl implements LoginService {
}
//3.生成相应的uuid作为redis的key
String uuidKey = UUID.randomUUID().toString();
//expertInfoResp = new ExpertInfoResp();
//expertInfoResp.setZhuanJiaGuid("1234");
ZhuanJiaUser zhuanJiaUser = convert2ZhuanjiaUser( expertInfoResp );
redisTemplate.opsForValue().set(TokenConstants.LOGIN_USER_KEY_ + expertInfoResp.getZhuanJiaGuid(), zhuanJiaUser, 48, TimeUnit.HOURS);
UserUtils.setUserId(zhuanJiaUser);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment