~
This commit is contained in:
@@ -42,7 +42,6 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 登录Service接口实现类
|
||||
*
|
||||
*
|
||||
* @date 2021/12/23 21:52
|
||||
*/
|
||||
@Service
|
||||
@@ -52,6 +51,8 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
private static final String AUTH_VALID_CODE_CACHE_KEY = "auth-validCode:";
|
||||
|
||||
private static final String LOGIN_ERROR_TIMES_KEY_PREFIX = "login-error-times:";
|
||||
|
||||
@Resource(name = "loginUserApi")
|
||||
private SaBaseLoginUserApi loginUserApi;
|
||||
|
||||
@@ -120,7 +121,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
/**
|
||||
* 校验验证码方法
|
||||
*
|
||||
*
|
||||
*
|
||||
* @date 2022/8/25 15:26
|
||||
**/
|
||||
private void validValidCode(String phoneOrEmail, String validCode, String validCodeReqNo) {
|
||||
@@ -150,7 +151,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
/**
|
||||
* 校验手机号与验证码等参数
|
||||
*
|
||||
*
|
||||
*
|
||||
* @date 2022/8/25 14:29
|
||||
**/
|
||||
private void validPhoneValidCodeParam(String phoneOrEmail, String validCode, String validCodeReqNo, String type) {
|
||||
@@ -180,6 +181,8 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
@Override
|
||||
public String doLogin(AuthAccountPasswordLoginParam authAccountPasswordLoginParam, String type) {
|
||||
// 判断账号是否被封禁
|
||||
isDisableTime(authAccountPasswordLoginParam.getAccount());
|
||||
// 获取账号
|
||||
String account = authAccountPasswordLoginParam.getAccount();
|
||||
// 获取密码
|
||||
@@ -227,8 +230,12 @@ public class AuthServiceImpl implements AuthService {
|
||||
throw new CommonException(AuthExceptionEnum.ACCOUNT_ERROR.getValue());
|
||||
}
|
||||
if (!saBaseLoginUser.getPassword().equals(passwordHash)) {
|
||||
// 记录登录次数 和 过期时间
|
||||
saveLoginTimes(account);
|
||||
throw new CommonException(AuthExceptionEnum.PWD_ERROR.getValue());
|
||||
}
|
||||
// 删除redis 中的key
|
||||
clearLoginErrorTimes(account);
|
||||
// 执行B端登录
|
||||
return execLoginB(saBaseLoginUser, device);
|
||||
} else {
|
||||
@@ -276,10 +283,57 @@ public class AuthServiceImpl implements AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否封禁状态
|
||||
* 如果被封禁了,执行以下逻辑,返回前端还需等待的时间
|
||||
*/
|
||||
private void isDisableTime(String userAccount) {
|
||||
// disableTime = -2表示未被封禁
|
||||
long disableTime = StpUtil.getDisableTime(userAccount);
|
||||
if (disableTime > 0) {
|
||||
if (disableTime > 60) {
|
||||
throw new CommonException(userAccount + "账号已被封禁, 请再"+ disableTime/60+ "分钟后重新尝试登录!!");
|
||||
}
|
||||
throw new CommonException(userAccount + "账号已被封禁, 请再"+ disableTime+ "秒后重新尝试登录!!");
|
||||
}
|
||||
}
|
||||
|
||||
// redis中保存登录错误次数
|
||||
private void saveLoginTimes(String userAccount){
|
||||
String loginErrorKey = LOGIN_ERROR_TIMES_KEY_PREFIX + userAccount;
|
||||
Integer number = (Integer) commonCacheOperator.get(loginErrorKey);
|
||||
if (number == null) {
|
||||
// 如果redis中没有保存,代表失败第一次
|
||||
number = 2;
|
||||
commonCacheOperator.put(loginErrorKey, number,5 * 60);
|
||||
return;
|
||||
}
|
||||
if (number < 5) {
|
||||
number++;
|
||||
commonCacheOperator.put(loginErrorKey, number,5 * 60);
|
||||
return;
|
||||
}
|
||||
// 第五次封禁账号,第六次进入isDisableTime方法,返回用户还需等待时间
|
||||
StpUtil.disable(userAccount, 5 * 60);
|
||||
// 删除redis 中的key
|
||||
clearLoginErrorTimes(userAccount);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录成功、清空登录次数
|
||||
* @param userAccount 账号
|
||||
*/
|
||||
private void clearLoginErrorTimes(String userAccount) {
|
||||
String loginErrorKey = LOGIN_ERROR_TIMES_KEY_PREFIX + userAccount;
|
||||
// 删除redis中的key
|
||||
commonCacheOperator.remove(loginErrorKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行B端登录
|
||||
*
|
||||
*
|
||||
*
|
||||
* @date 2022/8/25 14:36
|
||||
**/
|
||||
private String execLoginB(SaBaseLoginUser saBaseLoginUser, String device) {
|
||||
@@ -318,7 +372,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
/**
|
||||
* 执行C端登录
|
||||
*
|
||||
*
|
||||
*
|
||||
* @date 2022/8/25 14:37
|
||||
**/
|
||||
private String execLoginC(SaBaseClientLoginUser saBaseClientLoginUser, String device) {
|
||||
@@ -357,7 +411,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
/**
|
||||
* 获取B端登录用户信息
|
||||
*
|
||||
*
|
||||
*
|
||||
* @date 2021/10/12 15:59
|
||||
**/
|
||||
@Override
|
||||
@@ -372,7 +426,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
/**
|
||||
* 获取C端登录用户信息
|
||||
*
|
||||
*
|
||||
*
|
||||
* @date 2021/10/12 15:59
|
||||
**/
|
||||
@Override
|
||||
|
||||
@@ -96,10 +96,10 @@ public class AuthSessionServiceImpl implements AuthSessionService {
|
||||
Page<AuthSessionPageResult> defaultPage = CommonPageRequest.defaultPage();
|
||||
long current = defaultPage.getCurrent();
|
||||
int total = StpUtil.searchSessionId("", -1, Convert.toInt(defaultPage.getSize()), true).size();
|
||||
if(ObjectUtil.isNotEmpty(total)) {
|
||||
if (ObjectUtil.isNotEmpty(total)) {
|
||||
defaultPage = new Page<>(current, defaultPage.getSize(), total);
|
||||
String keyword = "";
|
||||
if(ObjectUtil.isNotEmpty(authSessionPageParam.getUserId())) {
|
||||
if (ObjectUtil.isNotEmpty(authSessionPageParam.getUserId())) {
|
||||
keyword = authSessionPageParam.getUserId();
|
||||
}
|
||||
List<String> userIdList = StpUtil.searchSessionId(keyword,
|
||||
@@ -113,31 +113,37 @@ public class AuthSessionServiceImpl implements AuthSessionService {
|
||||
authSessionPageResult.setSessionId(saSession.getId());
|
||||
authSessionPageResult.setSessionCreateTime(DateTime.of(saSession.getCreateTime()));
|
||||
long sessionTimeOut = saSession.getTimeout();
|
||||
if(sessionTimeOut == -1) {
|
||||
if (sessionTimeOut == -1) {
|
||||
authSessionPageResult.setSessionTimeout("永久");
|
||||
} else {
|
||||
authSessionPageResult.setSessionTimeout(CommonTimeFormatUtil.formatSeconds(saSession.getTimeout()));
|
||||
}
|
||||
List<AuthSessionPageResult.TokenSignInfo> tokenInfoList = saSession.getTokenSignList().stream().map(tokenSign -> {
|
||||
AuthSessionPageResult.TokenSignInfo tokenSignInfo = new AuthSessionPageResult.TokenSignInfo();
|
||||
tokenSignInfo.setTokenValue(tokenSign.getValue());
|
||||
tokenSignInfo.setTokenDevice(tokenSign.getDevice());
|
||||
long tokenTimeout = SaManager.getSaTokenDao().getTimeout(StpUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()));
|
||||
long tokenTimeoutConfig = StpUtil.stpLogic.getConfig().getTimeout();
|
||||
if(tokenTimeout == -1) {
|
||||
tokenSignInfo.setTokenTimeout("永久");
|
||||
tokenSignInfo.setTokenTimeoutPercent(100d);
|
||||
} else {
|
||||
tokenSignInfo.setTokenTimeout(CommonTimeFormatUtil.formatSeconds(SaManager.getSaTokenDao()
|
||||
.getTimeout(StpUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()))));
|
||||
if(tokenTimeoutConfig == -1) {
|
||||
tokenSignInfo.setTokenTimeoutPercent(0d);
|
||||
} else {
|
||||
tokenSignInfo.setTokenTimeoutPercent(NumberUtil.div(tokenTimeout, tokenTimeoutConfig));
|
||||
}
|
||||
}
|
||||
return tokenSignInfo;
|
||||
}).collect(Collectors.toList());
|
||||
List<AuthSessionPageResult.TokenSignInfo> tokenInfoList = saSession.getTokenSignList().stream()
|
||||
.filter(tokenSign -> {
|
||||
long tokenTimeout = SaManager.getSaTokenDao().getTimeout(StpUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()));
|
||||
return tokenTimeout != -2; // 过滤掉tokenTimeout为-2的元素
|
||||
})
|
||||
.map(tokenSign -> {
|
||||
AuthSessionPageResult.TokenSignInfo tokenSignInfo = new AuthSessionPageResult.TokenSignInfo();
|
||||
tokenSignInfo.setTokenValue(tokenSign.getValue());
|
||||
tokenSignInfo.setTokenDevice(tokenSign.getDevice());
|
||||
long tokenTimeout = SaManager.getSaTokenDao().getTimeout(StpUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()));
|
||||
long tokenTimeoutConfig = StpUtil.stpLogic.getConfig().getTimeout();
|
||||
if (tokenTimeout == -1) {
|
||||
tokenSignInfo.setTokenTimeout("永久");
|
||||
tokenSignInfo.setTokenTimeoutPercent(100d);
|
||||
} else {
|
||||
tokenSignInfo.setTokenTimeout(CommonTimeFormatUtil.formatSeconds(SaManager.getSaTokenDao()
|
||||
.getTimeout(StpUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()))));
|
||||
if (tokenTimeoutConfig == -1) {
|
||||
tokenSignInfo.setTokenTimeoutPercent(0d);
|
||||
} else {
|
||||
tokenSignInfo.setTokenTimeoutPercent(NumberUtil.div(tokenTimeout, tokenTimeoutConfig));
|
||||
}
|
||||
}
|
||||
return tokenSignInfo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
authSessionPageResult.setTokenCount(tokenInfoList.size());
|
||||
authSessionPageResult.setTokenSignList(tokenInfoList);
|
||||
return authSessionPageResult;
|
||||
@@ -153,10 +159,10 @@ public class AuthSessionServiceImpl implements AuthSessionService {
|
||||
Page<AuthSessionPageResult> defaultPage = CommonPageRequest.defaultPage();
|
||||
long current = defaultPage.getCurrent();
|
||||
int total = StpClientUtil.searchSessionId("", -1, Convert.toInt(defaultPage.getSize()), true).size();
|
||||
if(ObjectUtil.isNotEmpty(total)) {
|
||||
if (ObjectUtil.isNotEmpty(total)) {
|
||||
defaultPage = new Page<>(current, defaultPage.getSize(), total);
|
||||
String keyword = "";
|
||||
if(ObjectUtil.isNotEmpty(authSessionPageParam.getUserId())) {
|
||||
if (ObjectUtil.isNotEmpty(authSessionPageParam.getUserId())) {
|
||||
keyword = authSessionPageParam.getUserId();
|
||||
}
|
||||
List<String> userIdList = StpClientUtil.searchSessionId(keyword,
|
||||
@@ -170,24 +176,27 @@ public class AuthSessionServiceImpl implements AuthSessionService {
|
||||
authSessionPageResult.setSessionId(saSession.getId());
|
||||
authSessionPageResult.setSessionCreateTime(DateTime.of(saSession.getCreateTime()));
|
||||
long sessionTimeOut = saSession.getTimeout();
|
||||
if(sessionTimeOut == -1) {
|
||||
if (sessionTimeOut == -1) {
|
||||
authSessionPageResult.setSessionTimeout("永久");
|
||||
} else {
|
||||
authSessionPageResult.setSessionTimeout(CommonTimeFormatUtil.formatSeconds(saSession.getTimeout()));
|
||||
}
|
||||
List<AuthSessionPageResult.TokenSignInfo> tokenInfoList = saSession.getTokenSignList().stream().map(tokenSign -> {
|
||||
List<AuthSessionPageResult.TokenSignInfo> tokenInfoList = saSession.getTokenSignList().stream().filter(tokenSign -> {
|
||||
long tokenTimeout = SaManager.getSaTokenDao().getTimeout(StpUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()));
|
||||
return tokenTimeout != -2; // 过滤掉tokenTimeout为-2的元素
|
||||
}).map(tokenSign -> {
|
||||
AuthSessionPageResult.TokenSignInfo tokenSignInfo = new AuthSessionPageResult.TokenSignInfo();
|
||||
tokenSignInfo.setTokenValue(tokenSign.getValue());
|
||||
tokenSignInfo.setTokenDevice(tokenSign.getDevice());
|
||||
long tokenTimeout = SaManager.getSaTokenDao().getTimeout(StpClientUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()));
|
||||
long tokenTimeoutConfig = StpClientUtil.stpLogic.getConfig().getTimeout();
|
||||
if(tokenTimeout == -1) {
|
||||
if (tokenTimeout == -1) {
|
||||
tokenSignInfo.setTokenTimeout("永久");
|
||||
tokenSignInfo.setTokenTimeoutPercent(100d);
|
||||
} else {
|
||||
tokenSignInfo.setTokenTimeout(CommonTimeFormatUtil.formatSeconds(SaManager.getSaTokenDao()
|
||||
.getTimeout(StpClientUtil.stpLogic.splicingKeyTokenValue(tokenSign.getValue()))));
|
||||
if(tokenTimeoutConfig == -1) {
|
||||
if (tokenTimeoutConfig == -1) {
|
||||
tokenSignInfo.setTokenTimeoutPercent(0d);
|
||||
} else {
|
||||
tokenSignInfo.setTokenTimeoutPercent(NumberUtil.div(tokenTimeout, tokenTimeoutConfig));
|
||||
|
||||
Reference in New Issue
Block a user