|
@@ -1,6 +1,7 @@
|
|
package com.usky.dxtop.framework.web.service;
|
|
package com.usky.dxtop.framework.web.service;
|
|
|
|
|
|
import com.usky.dxtop.common.constant.Constants;
|
|
import com.usky.dxtop.common.constant.Constants;
|
|
|
|
+import com.usky.dxtop.common.constant.UserConstants;
|
|
import com.usky.dxtop.common.core.domain.model.LoginUser;
|
|
import com.usky.dxtop.common.core.domain.model.LoginUser;
|
|
import com.usky.dxtop.common.core.redis.RedisCache;
|
|
import com.usky.dxtop.common.core.redis.RedisCache;
|
|
import com.usky.dxtop.common.exception.CustomException;
|
|
import com.usky.dxtop.common.exception.CustomException;
|
|
@@ -13,9 +14,12 @@ import com.usky.dxtop.common.utils.ServletUtils;
|
|
import com.usky.dxtop.common.utils.ip.IpUtils;
|
|
import com.usky.dxtop.common.utils.ip.IpUtils;
|
|
import com.usky.dxtop.framework.manager.AsyncManager;
|
|
import com.usky.dxtop.framework.manager.AsyncManager;
|
|
import com.usky.dxtop.framework.manager.factory.AsyncFactory;
|
|
import com.usky.dxtop.framework.manager.factory.AsyncFactory;
|
|
|
|
+import com.usky.dxtop.model.AccountLock;
|
|
import com.usky.dxtop.model.SysUser;
|
|
import com.usky.dxtop.model.SysUser;
|
|
|
|
+import com.usky.dxtop.service.AccountLockService;
|
|
import com.usky.dxtop.service.ISysConfigService;
|
|
import com.usky.dxtop.service.ISysConfigService;
|
|
import com.usky.dxtop.service.ISysUserService;
|
|
import com.usky.dxtop.service.ISysUserService;
|
|
|
|
+import com.usky.dxtop.service.vo.UserAccountVO;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
@@ -24,6 +28,8 @@ import org.springframework.security.core.Authentication;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -49,6 +55,8 @@ public class SysLoginService
|
|
@Autowired
|
|
@Autowired
|
|
private ISysConfigService configService;
|
|
private ISysConfigService configService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private AccountLockService accountLockService;
|
|
/**
|
|
/**
|
|
* 登录验证
|
|
* 登录验证
|
|
*
|
|
*
|
|
@@ -81,7 +89,7 @@ public class SysLoginService
|
|
if (e instanceof BadCredentialsException)
|
|
if (e instanceof BadCredentialsException)
|
|
{
|
|
{
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
- throw new UserPasswordNotMatchException();
|
|
|
|
|
|
+ lockAccount(username);
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -130,4 +138,42 @@ public class SysLoginService
|
|
user.setLoginDate(DateUtils.getNowDate());
|
|
user.setLoginDate(DateUtils.getNowDate());
|
|
userService.updateUserProfile(user);
|
|
userService.updateUserProfile(user);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 锁定账号
|
|
|
|
+ * @param userName
|
|
|
|
+ */
|
|
|
|
+ public void lockAccount(String userName){
|
|
|
|
+ AccountLock accountLock = accountLockService.isEnableOne();
|
|
|
|
+ if (null != accountLock){
|
|
|
|
+ Object cacheObj = redisCache.getCacheObject(getLockAccountKey(userName));
|
|
|
|
+ if (Objects.nonNull(cacheObj))
|
|
|
|
+ {
|
|
|
|
+ UserAccountVO userAccountVO = (UserAccountVO) cacheObj;
|
|
|
|
+ userAccountVO.setUseCount(userAccountVO.getUseCount()+1);
|
|
|
|
+ int i = userAccountVO.getTotalCount() - userAccountVO.getUseCount();
|
|
|
|
+ if (userAccountVO.getTotalCount() > userAccountVO.getUseCount()){
|
|
|
|
+ redisCache.setCacheObject(getLockAccountKey(userName),userAccountVO);
|
|
|
|
+ throw new CustomException("密码输入错误还剩下"+i+"次机会");
|
|
|
|
+ } else {
|
|
|
|
+ redisCache.setCacheObject(getLockAccountKey(userName),userAccountVO,accountLock.getLoseDate(), TimeUnit.MINUTES);
|
|
|
|
+ throw new CustomException(userAccountVO.getTotalCount()+"次机会已经用完,账号被锁定"+accountLock.getLoseDate()+"分钟");
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ UserAccountVO userAccountVO = new UserAccountVO();
|
|
|
|
+ userAccountVO.setTotalCount(accountLock.getLockNumber());
|
|
|
|
+ userAccountVO.setUseCount(1);
|
|
|
|
+ redisCache.setCacheObject(getLockAccountKey(userName),userAccountVO);
|
|
|
|
+ int i = userAccountVO.getTotalCount() - 1;
|
|
|
|
+ throw new CustomException("密码输入错误还剩下"+i+"次机会");
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ throw new CustomException("密码输入不正确");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getLockAccountKey(String userName){
|
|
|
|
+ return UserConstants.ACCOUNT_LOCK + "_" + userName;
|
|
|
|
+ }
|
|
}
|
|
}
|