2021-09-17 14:48:36 +08:00
|
|
|
package com.ruoyi.system.service;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
2022-02-11 15:03:09 +08:00
|
|
|
import cn.dev33.satoken.secure.BCrypt;
|
2021-09-23 19:13:58 +08:00
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
2022-01-27 09:50:18 +08:00
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
2021-12-02 16:04:45 +08:00
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
2020-02-13 10:48:51 +08:00
|
|
|
import com.ruoyi.common.constant.Constants;
|
2022-01-27 09:50:18 +08:00
|
|
|
import com.ruoyi.common.core.domain.dto.RoleDTO;
|
2021-05-25 10:00:36 +08:00
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
2020-07-20 10:41:32 +08:00
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
2022-03-24 10:28:42 +08:00
|
|
|
import com.ruoyi.common.core.domain.model.XcxLoginUser;
|
2021-09-17 14:48:36 +08:00
|
|
|
import com.ruoyi.common.core.service.LogininforService;
|
2021-09-28 17:55:01 +08:00
|
|
|
import com.ruoyi.common.enums.DeviceType;
|
2021-09-23 19:13:58 +08:00
|
|
|
import com.ruoyi.common.enums.UserStatus;
|
2020-02-13 10:48:51 +08:00
|
|
|
import com.ruoyi.common.exception.user.CaptchaException;
|
|
|
|
import com.ruoyi.common.exception.user.CaptchaExpireException;
|
2021-12-02 16:04:45 +08:00
|
|
|
import com.ruoyi.common.exception.user.UserException;
|
2022-01-17 12:03:15 +08:00
|
|
|
import com.ruoyi.common.helper.LoginHelper;
|
2022-01-27 09:50:18 +08:00
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
import com.ruoyi.common.utils.ServletUtils;
|
2022-02-14 14:31:57 +08:00
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
2022-01-13 11:27:09 +08:00
|
|
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
2022-01-12 21:22:26 +08:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2022-01-13 11:27:09 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2021-09-27 17:58:36 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
2021-05-25 13:40:18 +08:00
|
|
|
|
2021-06-12 20:14:50 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2022-01-27 09:50:18 +08:00
|
|
|
import java.util.List;
|
2021-12-02 16:04:45 +08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 登录校验方法
|
2021-09-27 17:58:36 +08:00
|
|
|
*
|
2021-10-15 15:19:42 +08:00
|
|
|
* @author Lion Li
|
2020-02-13 10:48:51 +08:00
|
|
|
*/
|
2022-01-12 21:22:26 +08:00
|
|
|
@RequiredArgsConstructor
|
2021-09-23 19:13:58 +08:00
|
|
|
@Slf4j
|
2021-09-27 17:58:36 +08:00
|
|
|
@Service
|
|
|
|
public class SysLoginService {
|
2021-10-15 15:19:42 +08:00
|
|
|
|
2022-01-12 21:22:26 +08:00
|
|
|
private final ISysUserService userService;
|
|
|
|
private final ISysConfigService configService;
|
|
|
|
private final LogininforService asyncService;
|
2022-01-13 11:14:33 +08:00
|
|
|
private final SysPermissionService permissionService;
|
2021-06-12 20:14:50 +08:00
|
|
|
|
2020-02-13 10:48:51 +08:00
|
|
|
/**
|
|
|
|
* 登录验证
|
2021-09-27 17:58:36 +08:00
|
|
|
*
|
2020-02-13 10:48:51 +08:00
|
|
|
* @param username 用户名
|
|
|
|
* @param password 密码
|
2021-09-27 17:58:36 +08:00
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
2020-02-13 10:48:51 +08:00
|
|
|
* @return 结果
|
|
|
|
*/
|
2021-09-27 17:58:36 +08:00
|
|
|
public String login(String username, String password, String code, String uuid) {
|
|
|
|
HttpServletRequest request = ServletUtils.getRequest();
|
|
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
2021-07-13 13:34:10 +08:00
|
|
|
// 验证码开关
|
2021-09-27 17:58:36 +08:00
|
|
|
if (captchaOnOff) {
|
2021-07-24 19:03:36 +08:00
|
|
|
validateCaptcha(username, code, uuid, request);
|
2021-07-13 13:34:10 +08:00
|
|
|
}
|
2021-12-02 16:04:45 +08:00
|
|
|
// 获取用户登录错误次数(可自定义限制策略 例如: key + username + ip)
|
|
|
|
Integer errorNumber = RedisUtils.getCacheObject(Constants.LOGIN_ERROR + username);
|
|
|
|
// 锁定时间内登录 则踢出
|
|
|
|
if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
|
|
|
|
throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
|
|
|
|
}
|
2021-12-27 09:50:42 +08:00
|
|
|
|
2022-01-17 17:09:27 +08:00
|
|
|
SysUser user = loadUserByUsername(username);
|
|
|
|
|
2022-02-11 15:03:09 +08:00
|
|
|
if (!BCrypt.checkpw(password, user.getPassword())) {
|
2021-12-27 09:50:42 +08:00
|
|
|
// 是否第一次
|
|
|
|
errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
|
|
|
|
// 达到规定错误次数 则锁定登录
|
|
|
|
if (errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
|
|
|
|
RedisUtils.setCacheObject(Constants.LOGIN_ERROR + username, errorNumber, Constants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
|
|
|
|
throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
|
2021-09-27 17:58:36 +08:00
|
|
|
} else {
|
2021-12-27 09:50:42 +08:00
|
|
|
// 未达到规定错误次数 则递增
|
|
|
|
RedisUtils.setCacheObject(Constants.LOGIN_ERROR + username, errorNumber);
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", errorNumber), request);
|
|
|
|
throw new UserException("user.password.retry.limit.count", errorNumber);
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-27 09:50:42 +08:00
|
|
|
|
2021-12-02 16:04:45 +08:00
|
|
|
// 登录成功 清空错误次数
|
|
|
|
RedisUtils.deleteObject(Constants.LOGIN_ERROR + username);
|
2022-03-23 00:33:16 +08:00
|
|
|
|
|
|
|
// 此处可根据登录用户的数据不同 自行创建 loginUser
|
2022-01-17 17:09:27 +08:00
|
|
|
LoginUser loginUser = buildLoginUser(user);
|
2020-02-13 10:48:51 +08:00
|
|
|
// 生成token
|
2022-01-17 12:03:15 +08:00
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.PC);
|
2022-02-24 10:01:59 +08:00
|
|
|
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
|
|
|
recordLoginInfo(user.getUserId(), username);
|
2021-09-23 19:13:58 +08:00
|
|
|
return StpUtil.getTokenValue();
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|
2021-05-25 10:00:36 +08:00
|
|
|
|
2022-03-23 00:33:16 +08:00
|
|
|
public String smsLogin(String phonenumber, String smsCode) {
|
|
|
|
// 通过手机号查找用户
|
|
|
|
SysUser user = loadUserByPhonenumber(phonenumber);
|
|
|
|
|
|
|
|
HttpServletRequest request = ServletUtils.getRequest();
|
|
|
|
// 获取用户登录错误次数(可自定义限制策略 例如: key + username + ip)
|
|
|
|
Integer errorNumber = RedisUtils.getCacheObject(Constants.LOGIN_ERROR + user.getUserName());
|
|
|
|
// 锁定时间内登录 则踢出
|
|
|
|
if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
|
|
|
|
asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
|
|
|
|
throw new UserException("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validateSmsCode(phonenumber, smsCode)) {
|
|
|
|
// 是否第一次
|
|
|
|
errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
|
|
|
|
// 达到规定错误次数 则锁定登录
|
|
|
|
if (errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
|
|
|
|
RedisUtils.setCacheObject(Constants.LOGIN_ERROR + user.getUserName(), errorNumber, Constants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
|
|
|
|
asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
|
|
|
|
throw new UserException("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
|
|
|
|
} else {
|
|
|
|
// 未达到规定错误次数 则递增
|
|
|
|
RedisUtils.setCacheObject(Constants.LOGIN_ERROR + user.getUserName(), errorNumber);
|
|
|
|
asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("sms.code.retry.limit.count", errorNumber), request);
|
|
|
|
throw new UserException("sms.code.retry.limit.count", errorNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 登录成功 清空错误次数
|
|
|
|
RedisUtils.deleteObject(Constants.LOGIN_ERROR + user.getUserName());
|
|
|
|
|
|
|
|
// 此处可根据登录用户的数据不同 自行创建 loginUser
|
|
|
|
LoginUser loginUser = buildLoginUser(user);
|
|
|
|
// 生成token
|
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.APP);
|
|
|
|
|
|
|
|
asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
|
|
|
recordLoginInfo(user.getUserId(), user.getUserName());
|
|
|
|
return StpUtil.getTokenValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String xcxLogin(String xcxCode) {
|
|
|
|
HttpServletRequest request = ServletUtils.getRequest();
|
|
|
|
// xcxCode 为 小程序调用 wx.login 授权后获取
|
|
|
|
// todo 以下自行实现
|
|
|
|
// 校验 appid + appsrcret + xcxCode 调用登录凭证校验接口 获取 session_key 与 openid
|
|
|
|
String openid = "";
|
|
|
|
SysUser user = loadUserByOpenid(openid);
|
|
|
|
|
|
|
|
// 此处可根据登录用户的数据不同 自行创建 loginUser
|
2022-03-24 10:28:42 +08:00
|
|
|
XcxLoginUser loginUser = new XcxLoginUser();
|
|
|
|
loginUser.setUserId(user.getUserId());
|
|
|
|
loginUser.setUsername(user.getUserName());
|
|
|
|
loginUser.setUserType(user.getUserType());
|
|
|
|
loginUser.setOpenid(openid);
|
2022-03-23 00:33:16 +08:00
|
|
|
// 生成token
|
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.XCX);
|
|
|
|
|
|
|
|
asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
|
|
|
recordLoginInfo(user.getUserId(), user.getUserName());
|
|
|
|
return StpUtil.getTokenValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-21 10:40:28 +08:00
|
|
|
public void logout(String loginName) {
|
|
|
|
asyncService.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest());
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:33:16 +08:00
|
|
|
/**
|
|
|
|
* 校验短信验证码
|
|
|
|
*/
|
|
|
|
private boolean validateSmsCode(String phonenumber, String smsCode) {
|
|
|
|
// todo 此处使用手机号查询redis验证码与参数验证码是否一致 用户自行实现
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-13 13:34:10 +08:00
|
|
|
/**
|
|
|
|
* 校验验证码
|
2021-09-27 17:58:36 +08:00
|
|
|
*
|
2021-07-13 13:34:10 +08:00
|
|
|
* @param username 用户名
|
2021-09-27 17:58:36 +08:00
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
2021-07-13 13:34:10 +08:00
|
|
|
*/
|
2021-07-24 19:03:36 +08:00
|
|
|
public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
|
2022-02-14 14:31:57 +08:00
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
|
2021-09-27 17:58:36 +08:00
|
|
|
String captcha = RedisUtils.getCacheObject(verifyKey);
|
2021-09-07 13:20:24 +08:00
|
|
|
RedisUtils.deleteObject(verifyKey);
|
2021-09-27 17:58:36 +08:00
|
|
|
if (captcha == null) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
|
|
|
|
throw new CaptchaExpireException();
|
|
|
|
}
|
|
|
|
if (!code.equalsIgnoreCase(captcha)) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
|
|
|
|
throw new CaptchaException();
|
|
|
|
}
|
2021-07-13 13:34:10 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 17:09:27 +08:00
|
|
|
private SysUser loadUserByUsername(String username) {
|
|
|
|
SysUser user = userService.selectUserByUserName(username);
|
2022-01-21 10:30:39 +08:00
|
|
|
if (ObjectUtil.isNull(user)) {
|
2022-01-17 17:09:27 +08:00
|
|
|
log.info("登录用户:{} 不存在.", username);
|
|
|
|
throw new UserException("user.not.exists", username);
|
|
|
|
} else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
|
log.info("登录用户:{} 已被删除.", username);
|
|
|
|
throw new UserException("user.password.delete", username);
|
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
|
log.info("登录用户:{} 已被停用.", username);
|
|
|
|
throw new UserException("user.blocked", username);
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:33:16 +08:00
|
|
|
private SysUser loadUserByPhonenumber(String phonenumber) {
|
|
|
|
SysUser user = userService.selectUserByPhonenumber(phonenumber);
|
|
|
|
if (ObjectUtil.isNull(user)) {
|
|
|
|
log.info("登录用户:{} 不存在.", phonenumber);
|
|
|
|
throw new UserException("user.not.exists", phonenumber);
|
|
|
|
} else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
|
log.info("登录用户:{} 已被删除.", phonenumber);
|
|
|
|
throw new UserException("user.password.delete", phonenumber);
|
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
|
log.info("登录用户:{} 已被停用.", phonenumber);
|
|
|
|
throw new UserException("user.blocked", phonenumber);
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SysUser loadUserByOpenid(String openid) {
|
|
|
|
// 使用 openid 查询绑定用户 如未绑定用户 则根据业务自行处理 例如 创建默认用户
|
|
|
|
// todo 自行实现 userService.selectUserByOpenid(openid);
|
|
|
|
SysUser user = new SysUser();
|
|
|
|
if (ObjectUtil.isNull(user)) {
|
|
|
|
log.info("登录用户:{} 不存在.", openid);
|
|
|
|
// todo 用户不存在 业务逻辑自行实现
|
|
|
|
} else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
|
log.info("登录用户:{} 已被删除.", openid);
|
|
|
|
// todo 用户已被删除 业务逻辑自行实现
|
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
|
log.info("登录用户:{} 已被停用.", openid);
|
|
|
|
// todo 用户已被停用 业务逻辑自行实现
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2022-01-17 17:09:27 +08:00
|
|
|
/**
|
|
|
|
* 构建登录用户
|
|
|
|
*/
|
|
|
|
private LoginUser buildLoginUser(SysUser user) {
|
|
|
|
LoginUser loginUser = new LoginUser();
|
|
|
|
loginUser.setUserId(user.getUserId());
|
|
|
|
loginUser.setDeptId(user.getDeptId());
|
|
|
|
loginUser.setUsername(user.getUserName());
|
|
|
|
loginUser.setUserType(user.getUserType());
|
|
|
|
loginUser.setMenuPermission(permissionService.getMenuPermission(user));
|
|
|
|
loginUser.setRolePermission(permissionService.getRolePermission(user));
|
2022-01-27 09:50:18 +08:00
|
|
|
loginUser.setDeptName(user.getDept().getDeptName());
|
|
|
|
List<RoleDTO> roles = BeanUtil.copyToList(user.getRoles(), RoleDTO.class);
|
|
|
|
loginUser.setRoles(roles);
|
2022-01-17 17:09:27 +08:00
|
|
|
return loginUser;
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:00:36 +08:00
|
|
|
/**
|
|
|
|
* 记录登录信息
|
2021-09-26 10:05:29 +08:00
|
|
|
*
|
|
|
|
* @param userId 用户ID
|
2021-05-25 10:00:36 +08:00
|
|
|
*/
|
2021-10-11 16:49:21 +08:00
|
|
|
public void recordLoginInfo(Long userId, String username) {
|
2021-09-26 10:05:29 +08:00
|
|
|
SysUser sysUser = new SysUser();
|
|
|
|
sysUser.setUserId(userId);
|
2021-09-26 10:08:24 +08:00
|
|
|
sysUser.setLoginIp(ServletUtils.getClientIP());
|
2021-09-26 10:05:29 +08:00
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
2021-10-11 16:49:21 +08:00
|
|
|
sysUser.setUpdateBy(username);
|
2021-09-26 10:05:29 +08:00
|
|
|
userService.updateUserProfile(sysUser);
|
2021-05-25 10:00:36 +08:00
|
|
|
}
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|