no message
Showing
2 changed files
with
88 additions
and
33 deletions
| ... | @@ -30,7 +30,6 @@ import java.util.Map; | ... | @@ -30,7 +30,6 @@ import java.util.Map; |
| 30 | @RequestMapping("/core/user") | 30 | @RequestMapping("/core/user") |
| 31 | public class RegisterUserController extends BaseController { | 31 | public class RegisterUserController extends BaseController { |
| 32 | private final SysRegisterService registerService; | 32 | private final SysRegisterService registerService; |
| 33 | private final SysLoginService loginService; | ||
| 34 | /** | 33 | /** |
| 35 | * 用户注册 | 34 | * 用户注册 |
| 36 | */ | 35 | */ |
| ... | @@ -50,8 +49,7 @@ public class RegisterUserController extends BaseController { | ... | @@ -50,8 +49,7 @@ public class RegisterUserController extends BaseController { |
| 50 | public R<Map<String, Object>> login(@Validated @RequestBody LoginBody loginBody) { | 49 | public R<Map<String, Object>> login(@Validated @RequestBody LoginBody loginBody) { |
| 51 | Map<String, Object> ajax = new HashMap<>(); | 50 | Map<String, Object> ajax = new HashMap<>(); |
| 52 | // 生成令牌 | 51 | // 生成令牌 |
| 53 | String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), | 52 | String token = registerService.login(loginBody.getUsername(), loginBody.getPassword()); |
| 54 | loginBody.getUuid()); | ||
| 55 | ajax.put(Constants.TOKEN, token); | 53 | ajax.put(Constants.TOKEN, token); |
| 56 | return R.ok(ajax); | 54 | return R.ok(ajax); |
| 57 | } | 55 | } | ... | ... |
| 1 | package com.lego.system.service; | 1 | package com.lego.system.service; |
| 2 | 2 | ||
| 3 | import cn.dev33.satoken.secure.BCrypt; | 3 | import cn.dev33.satoken.secure.BCrypt; |
| 4 | import cn.dev33.satoken.stp.StpUtil; | ||
| 5 | import cn.hutool.core.bean.BeanUtil; | ||
| 6 | import cn.hutool.core.collection.CollectionUtil; | ||
| 7 | import cn.hutool.core.util.ObjectUtil; | ||
| 8 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
| 4 | import com.lego.common.constant.CacheConstants; | 9 | import com.lego.common.constant.CacheConstants; |
| 5 | import com.lego.common.constant.Constants; | 10 | import com.lego.common.constant.Constants; |
| 11 | import com.lego.common.core.domain.dto.RoleDTO; | ||
| 12 | import com.lego.common.core.domain.entity.SysDept; | ||
| 6 | import com.lego.common.core.domain.event.LogininforEvent; | 13 | import com.lego.common.core.domain.event.LogininforEvent; |
| 7 | import com.lego.common.core.domain.entity.SysUser; | 14 | import com.lego.common.core.domain.entity.SysUser; |
| 15 | import com.lego.common.core.domain.model.LoginUser; | ||
| 8 | import com.lego.common.core.domain.model.RegisterBody; | 16 | import com.lego.common.core.domain.model.RegisterBody; |
| 17 | import com.lego.common.enums.DeviceType; | ||
| 18 | import com.lego.common.enums.LoginType; | ||
| 19 | import com.lego.common.enums.UserStatus; | ||
| 9 | import com.lego.common.enums.UserType; | 20 | import com.lego.common.enums.UserType; |
| 10 | import com.lego.common.exception.user.CaptchaException; | 21 | import com.lego.common.exception.user.CaptchaException; |
| 11 | import com.lego.common.exception.user.CaptchaExpireException; | 22 | import com.lego.common.exception.user.CaptchaExpireException; |
| 12 | import com.lego.common.exception.user.UserException; | 23 | import com.lego.common.exception.user.UserException; |
| 13 | import com.lego.common.utils.MessageUtils; | 24 | import com.lego.common.helper.LoginHelper; |
| 14 | import com.lego.common.utils.ServletUtils; | 25 | import com.lego.common.utils.*; |
| 15 | import com.lego.common.utils.StringUtils; | ||
| 16 | import com.lego.common.utils.redis.RedisUtils; | 26 | import com.lego.common.utils.redis.RedisUtils; |
| 17 | import com.lego.common.utils.spring.SpringUtils; | 27 | import com.lego.common.utils.spring.SpringUtils; |
| 28 | import com.lego.system.mapper.SysUserMapper; | ||
| 18 | import lombok.RequiredArgsConstructor; | 29 | import lombok.RequiredArgsConstructor; |
| 30 | import lombok.extern.slf4j.Slf4j; | ||
| 31 | import org.springframework.beans.factory.annotation.Value; | ||
| 19 | import org.springframework.stereotype.Service; | 32 | import org.springframework.stereotype.Service; |
| 20 | 33 | ||
| 34 | import java.time.Duration; | ||
| 35 | import java.util.List; | ||
| 36 | import java.util.function.Supplier; | ||
| 37 | import java.util.stream.Collectors; | ||
| 38 | |||
| 21 | /** | 39 | /** |
| 22 | * 注册校验方法 | 40 | * 注册校验方法 |
| 23 | * | 41 | * |
| ... | @@ -25,10 +43,19 @@ import org.springframework.stereotype.Service; | ... | @@ -25,10 +43,19 @@ import org.springframework.stereotype.Service; |
| 25 | */ | 43 | */ |
| 26 | @RequiredArgsConstructor | 44 | @RequiredArgsConstructor |
| 27 | @Service | 45 | @Service |
| 46 | @Slf4j | ||
| 28 | public class SysRegisterService { | 47 | public class SysRegisterService { |
| 29 | 48 | ||
| 30 | private final ISysUserService userService; | 49 | private final ISysUserService userService; |
| 31 | private final ISysConfigService configService; | 50 | private final ISysConfigService configService; |
| 51 | private final SysUserMapper userMapper; | ||
| 52 | private final SysPermissionService permissionService; | ||
| 53 | |||
| 54 | private final ISysDeptService deptService; | ||
| 55 | @Value("${user.password.maxRetryCount}") | ||
| 56 | private Integer maxRetryCount; | ||
| 57 | @Value("${user.password.lockTime}") | ||
| 58 | private Integer lockTime; | ||
| 32 | 59 | ||
| 33 | /** | 60 | /** |
| 34 | * 注册 | 61 | * 注册 |
| ... | @@ -39,11 +66,6 @@ public class SysRegisterService { | ... | @@ -39,11 +66,6 @@ public class SysRegisterService { |
| 39 | // 校验用户类型是否存在 | 66 | // 校验用户类型是否存在 |
| 40 | String userType = UserType.getUserType(registerBody.getUserType()).getUserType(); | 67 | String userType = UserType.getUserType(registerBody.getUserType()).getUserType(); |
| 41 | 68 | ||
| 42 | // boolean captchaEnabled = configService.selectCaptchaEnabled(); | ||
| 43 | // // 验证码开关 | ||
| 44 | // if (captchaEnabled) { | ||
| 45 | // validateCaptcha(username, registerBody.getCode(), registerBody.getUuid()); | ||
| 46 | // } | ||
| 47 | SysUser sysUser = new SysUser(); | 69 | SysUser sysUser = new SysUser(); |
| 48 | sysUser.setUserName(username); | 70 | sysUser.setUserName(username); |
| 49 | sysUser.setNickName(username); | 71 | sysUser.setNickName(username); |
| ... | @@ -59,28 +81,6 @@ public class SysRegisterService { | ... | @@ -59,28 +81,6 @@ public class SysRegisterService { |
| 59 | } | 81 | } |
| 60 | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")); | 82 | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")); |
| 61 | } | 83 | } |
| 62 | |||
| 63 | /** | ||
| 64 | * 校验验证码 | ||
| 65 | * | ||
| 66 | * @param username 用户名 | ||
| 67 | * @param code 验证码 | ||
| 68 | * @param uuid 唯一标识 | ||
| 69 | */ | ||
| 70 | public void validateCaptcha(String username, String code, String uuid) { | ||
| 71 | String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); | ||
| 72 | String captcha = RedisUtils.getCacheObject(verifyKey); | ||
| 73 | RedisUtils.deleteObject(verifyKey); | ||
| 74 | if (captcha == null) { | ||
| 75 | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire")); | ||
| 76 | throw new CaptchaExpireException(); | ||
| 77 | } | ||
| 78 | if (!code.equalsIgnoreCase(captcha)) { | ||
| 79 | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error")); | ||
| 80 | throw new CaptchaException(); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | 84 | /** |
| 85 | * 记录登录信息 | 85 | * 记录登录信息 |
| 86 | * | 86 | * |
| ... | @@ -98,4 +98,61 @@ public class SysRegisterService { | ... | @@ -98,4 +98,61 @@ public class SysRegisterService { |
| 98 | SpringUtils.context().publishEvent(logininforEvent); | 98 | SpringUtils.context().publishEvent(logininforEvent); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | public String login(String username, String password) { | ||
| 102 | // 框架登录不限制从什么表查询 只要最终构建出 LoginUser 即可 | ||
| 103 | SysUser user = loadUserByUsername(username); | ||
| 104 | checkLogin(LoginType.PASSWORD, username, () -> !BCrypt.checkpw(password, user.getPassword())); | ||
| 105 | return EncryptUtils.encryptByAes(String.valueOf(System.currentTimeMillis()+1000*60*60*24*180)); | ||
| 106 | } | ||
| 107 | |||
| 108 | /** | ||
| 109 | * 构建登录用户 | ||
| 110 | */ | ||
| 111 | |||
| 112 | |||
| 113 | private SysUser loadUserByUsername(String username) { | ||
| 114 | SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() | ||
| 115 | .select(SysUser::getUserName, SysUser::getStatus) | ||
| 116 | .eq(SysUser::getUserName, username)); | ||
| 117 | if (ObjectUtil.isNull(user)) { | ||
| 118 | log.info("登录用户:{} 不存在.", username); | ||
| 119 | throw new UserException("user.not.exists", username); | ||
| 120 | } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { | ||
| 121 | log.info("登录用户:{} 已被停用.", username); | ||
| 122 | throw new UserException("user.blocked", username); | ||
| 123 | } | ||
| 124 | return userMapper.selectUserByUserName(username); | ||
| 125 | } | ||
| 126 | |||
| 127 | private void checkLogin(LoginType loginType, String username, Supplier<Boolean> supplier) { | ||
| 128 | String errorKey = CacheConstants.PWD_ERR_CNT_KEY + username; | ||
| 129 | String loginFail = Constants.LOGIN_FAIL; | ||
| 130 | |||
| 131 | // 获取用户登录错误次数,默认为0 (可自定义限制策略 例如: key + username + ip) | ||
| 132 | int errorNumber = ObjectUtil.defaultIfNull(RedisUtils.getCacheObject(errorKey), 0); | ||
| 133 | // 锁定时间内登录 则踢出 | ||
| 134 | if (errorNumber >= maxRetryCount) { | ||
| 135 | recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime)); | ||
| 136 | throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime); | ||
| 137 | } | ||
| 138 | |||
| 139 | if (supplier.get()) { | ||
| 140 | // 错误次数递增 | ||
| 141 | errorNumber++; | ||
| 142 | RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(lockTime)); | ||
| 143 | // 达到规定错误次数 则锁定登录 | ||
| 144 | if (errorNumber >= maxRetryCount) { | ||
| 145 | recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime)); | ||
| 146 | throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime); | ||
| 147 | } else { | ||
| 148 | // 未达到规定错误次数 | ||
| 149 | recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber)); | ||
| 150 | throw new UserException(loginType.getRetryLimitCount(), errorNumber); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | // 登录成功 清空错误次数 | ||
| 155 | RedisUtils.deleteObject(errorKey); | ||
| 156 | } | ||
| 157 | |||
| 101 | } | 158 | } | ... | ... |
-
Please register or sign in to post a comment