feat: 登录增加h5 和 web

This commit is contained in:
2025-01-12 22:17:03 +08:00
parent 75df9ad402
commit 1e12db79bf
7 changed files with 63 additions and 17 deletions

View File

@@ -55,6 +55,8 @@ public class Constants {
public static String SYSCONFIG_ENABLE_CAPTCHA = "enable_captcha";
public static String SYSCONFIG_ENABLE_REQUEST_SIGN = "enable_request_sign";
public static String SYSCONFIG_SESSION_TIMEOUT = "session_timeout";
public static String SYSCONFIG_SESSION_TIMEOUT_WEB = "session_timeout_web";
public static String SYSCONFIG_SESSION_TIMEOUT_H5 = "session_timeout_h5";
public static String SYSCONFIG_ACCOUNT_MAX_ONLINE = "account_max_online";
public static String SYSCONFIG_RESETPWD_ENABLE = "resetpwd_enable";
public static String SYSCONFIG_PASSWORD_DICT_DETECT = "password_dict_detect";

View File

@@ -21,6 +21,8 @@ public class LoginUserEntity extends CommonEntity {
private String ua;
private String captcha;
private String env;
private Integer timeout;
private LocalDateTime loginTime;
private LocalDateTime lastActiveTime;

View File

@@ -29,6 +29,7 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
@@ -125,14 +126,30 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
LocalDateTime now = LocalDateTime.now();
entity.setLoginTime(now);
int seconds =
Integer.parseInt(
sysConfigService.getConfigVal(Constants.SYSCONFIG_SESSION_TIMEOUT));
String defaultTimeout = sysConfigService.getConfigVal(Constants.SYSCONFIG_SESSION_TIMEOUT);
String envTimeout =
sysConfigService.getConfigVal(
Constants.SYSCONFIG_SESSION_TIMEOUT + "_" + entity.getEnv().toUpperCase());
if (StringUtils.isEmpty(envTimeout)) {
log.warn("环境 {} 未配置会话超时时间, 使用默认超时时间 {} 秒", entity.getEnv(), defaultTimeout);
envTimeout = defaultTimeout;
}
int seconds = Integer.parseInt(envTimeout);
entity.setTimeout(seconds);
entity.setExpireTime(now.plusSeconds(seconds));
entity.setLastActiveTime(now);
List<LoginUserEntity> exists = this.repository.findAllByUser(entity.getUser());
List<LoginUserEntity> exists =
this.repository.findAllByUser(entity.getUser()).stream().toList().stream()
.filter(
x ->
StringUtils.isEmpty(x.getEnv())
|| x.getEnv().equals(entity.getEnv()))
.toList();
if (!exists.isEmpty()) {
@@ -147,8 +164,9 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
.limit(exists.size() - maxOnlineAccount + 1)
.collect(Collectors.toList());
log.warn(
"用户 {} 已经在其他地方登录, 之前 {}个 登录将被强制下线",
"用户 {} 已经在其他地方登录, 环境 , 之前 {} 个登录将被强制下线",
entity.getUser().getUsername(),
entity.getEnv(),
toDelete.size());
for (LoginUserEntity user : toDelete) {
this.deleteLogin(user.getId());
@@ -264,7 +282,7 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
rateLimiterService.destroyRL(sessionId, null);
}
}else {
} else {
log.warn("会话 {} 不存在, 无需删除登录信息", sessionId);
}
@@ -295,12 +313,12 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
if (sessionId != null) {
this.deleteLogin(sessionId);
// try {
//
// userService.clearUserCache(Ctx.currentUser().getId());
// } catch (Exception e) {
// log.warn("清除用户缓存失败", e);
// }
// try {
//
// userService.clearUserCache(Ctx.currentUser().getId());
// } catch (Exception e) {
// log.warn("清除用户缓存失败", e);
// }
}
}

View File

@@ -22,6 +22,7 @@ public class LoginUserHisEntity extends CommonEntity {
private UserEntity user;
private String env;
private String loginType;
private String phone;

View File

@@ -21,4 +21,11 @@ public class LoginDto {
private String ua;
private String userId;
private String sessionId;
/**
* 登录环境
* web
* h5
*/
private String env;
}

View File

@@ -135,8 +135,6 @@ public class SessionService {
throw new BizException("invalidToken", "会话已过期,请重新登录");
}
stopWatch.start("check ip");
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
@@ -208,6 +206,7 @@ public class SessionService {
SecurityContextHolder.setContext(context);
}
@Autowired SmsTemplateService smsTemplateService;
@SneakyThrows
@@ -217,6 +216,8 @@ public class SessionService {
.getRequest();
UserEntity user = null;
dto.setEnv(StringUtils.defaultIfBlank(dto.getEnv(), "web"));
try {
if ((StringUtils.equalsIgnoreCase(dto.getLoginType(), "0")
@@ -288,6 +289,7 @@ public class SessionService {
}
LoginUserEntity entity = new LoginUserEntity();
entity.setEnv(dto.getEnv());
entity.setUser(user);
entity.setCaptcha(dto.getCaptcha());
entity.setIp(RequestUtils.getIp(request));
@@ -311,6 +313,7 @@ public class SessionService {
SecurityContextHolder.setContext(context);
LoginUserHisEntity his = new LoginUserHisEntity();
his.setEnv(dto.getEnv());
his.setLoginType(dto.getLoginType());
his.setPhone(dto.getPhone());
his.setIp(RequestUtils.getIp(request));
@@ -329,6 +332,7 @@ public class SessionService {
} catch (Exception e) {
LoginUserHisEntity his = new LoginUserHisEntity();
his.setEnv(dto.getEnv());
his.setLoginType(dto.getLoginType());
his.setPhone(dto.getPhone());
his.setIp(RequestUtils.getIp(request));
@@ -336,9 +340,6 @@ public class SessionService {
his.setCaptcha(dto.getCaptcha());
his.setUserName(dto.getUsername());
his.setLoginStatus("1");
his.setLocation(ipQueryService.query(his.getIp()));
his.setLoginTime(LocalDateTime.now());

View File

@@ -69,6 +69,21 @@ public class SysConfigService extends BaseService<SysConfigEntity, SysConfigRepo
TimeUnit.HOURS.toSeconds(1) + "",
TimeUnit.MINUTES.toSeconds(1),
TimeUnit.HOURS.toSeconds(24));
addNumberConfig(
all,
Constants.SYSCONFIG_SESSION_TIMEOUT_WEB,
"登录会话超时时间sWEB",
TimeUnit.HOURS.toSeconds(1) + "",
TimeUnit.MINUTES.toSeconds(1),
TimeUnit.HOURS.toSeconds(24));
addNumberConfig(
all,
Constants.SYSCONFIG_SESSION_TIMEOUT_H5,
"登录会话超时时间sH5",
TimeUnit.DAYS.toSeconds(7) + "",
TimeUnit.MINUTES.toSeconds(1),
TimeUnit.DAYS.toSeconds(30));
addNumberConfig(all, Constants.SYSCONFIG_ACCOUNT_MAX_ONLINE, "账户同时登录人数", 1 + "", 1L, 100L);
addDictConfig(all, Constants.SYSCONFIG_RESETPWD_ENABLE, "重置密码状态", "1", "status.type");