first commit
This commit is contained in:
@ -8,14 +8,16 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.useragent.UserAgent;
|
||||
import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.qiaoba.api.auth.service.AuthConfigApiService;
|
||||
import com.qiaoba.api.auth.service.SysUserDetailsApiService;
|
||||
import com.qiaoba.api.system.entity.SysUser;
|
||||
import com.qiaoba.api.system.entity.dto.LoginDto;
|
||||
import com.qiaoba.api.auth.service.SysUserDetailsApiService;
|
||||
import com.qiaoba.auth.constants.SecurityConstant;
|
||||
import com.qiaoba.auth.entity.OnlineUser;
|
||||
import com.qiaoba.auth.service.OnlineUserService;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.auth.utils.TokenUtil;
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.redis.service.RedisService;
|
||||
@ -46,13 +48,15 @@ public class SysLoginServiceImpl implements SysLoginService {
|
||||
private final SysUserDetailsApiService userDetailsService;
|
||||
private final SysUserService sysUserService;
|
||||
private final OnlineUserService onlineUserService;
|
||||
private final AuthConfigApiService authConfigApiService;
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getCaptchaImage() {
|
||||
Map<String, Object> map = new HashMap<String, Object>(4);
|
||||
|
||||
map.put("register", getRegisterConfig());
|
||||
if (!getCaptchaConfig()) {
|
||||
map.put("register", authConfigApiService.getRegisterConfig());
|
||||
if (!authConfigApiService.getCaptchaConfig()) {
|
||||
map.put("captchaEnabled", false);
|
||||
return map;
|
||||
}
|
||||
@ -69,7 +73,7 @@ public class SysLoginServiceImpl implements SysLoginService {
|
||||
@Override
|
||||
public String login(LoginDto dto) {
|
||||
// 校验验证码
|
||||
validateCaptcha(dto.getCode(), dto.getUuid());
|
||||
authConfigApiService.validateCaptcha(dto.getCode(), dto.getUuid());
|
||||
// username查询用户信息
|
||||
SysUser sysUser = sysUserService.selectByUsername(dto.getUsername());
|
||||
// 检查账号信息
|
||||
@ -81,7 +85,7 @@ public class SysLoginServiceImpl implements SysLoginService {
|
||||
// 缓存userDetails
|
||||
userDetailsService.toCache(sysUser.getUsername(), deviceSn);
|
||||
// 生成Token
|
||||
return dto.getUsername() + ":" + deviceSn;
|
||||
return TokenUtil.generateToken(sysUser.getUsername(), deviceSn);
|
||||
}
|
||||
|
||||
private void validatePassword(String username, String password, String inputPassword) {
|
||||
@ -104,52 +108,20 @@ public class SysLoginServiceImpl implements SysLoginService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getCaptchaConfig() {
|
||||
try {
|
||||
return SecurityConstant.CAPTCHA_ON.equals(redisService.get(SecurityConstant.CAPTCHA_ON_OFF_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("Redis中验证码配置不存在!");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getRegisterConfig() {
|
||||
try {
|
||||
return SecurityConstant.REGISTER_ON.equals(redisService.get(SecurityConstant.REGISTER_ON_OFF_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("Redis中注册配置不存在!");
|
||||
}
|
||||
}
|
||||
|
||||
private void validateCaptcha(String code, String uuid) {
|
||||
if (getCaptchaConfig()) {
|
||||
if (StrUtil.isBlank(code) || StrUtil.isBlank(uuid)) {
|
||||
throw new ServiceException("验证码或uuid获取失败!");
|
||||
}
|
||||
try {
|
||||
if (!redisService.hasKey(SecurityConstant.CAPTCHA_KEY + uuid)) {
|
||||
throw new ServiceException("验证码已经过期失效!");
|
||||
} else {
|
||||
if (!code.equalsIgnoreCase(redisService.get(SecurityConstant.CAPTCHA_KEY + uuid).toString())) {
|
||||
throw new ServiceException("验证码输入错误!");
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
redisService.del(SecurityConstant.CAPTCHA_KEY + uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String cacheOnlineUser(String username, String nickname) {
|
||||
String deviceSn = UUID.fastUUID().toString(true);
|
||||
String ip = IpUtil.getIp(request);
|
||||
String address = IpUtil.getIpAddr(ip);
|
||||
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
||||
String browser = userAgent.getBrowser().getName() + userAgent.getVersion();
|
||||
String browser = userAgent.getBrowser().getName() + BaseConstant.LINE_JOIN_STR + userAgent.getVersion();
|
||||
String os = userAgent.getOs().getName();
|
||||
|
||||
redisService.set(SecurityConstant.LOGGED_USER_REDIS_KEY + username, deviceSn, TokenUtil.expireTime * 3600);
|
||||
if (!authConfigApiService.checkAllowBothOnline()) {
|
||||
redisService.set(SecurityConstant.LOGGED_USER_REDIS_KEY + username, deviceSn, TokenUtil.expireTime * 3600);
|
||||
}
|
||||
onlineUserService.insert(new OnlineUser(deviceSn, username, nickname, ip, address, browser, os, new Date()));
|
||||
return deviceSn;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user