qushaowei
2023-12-13 5677ab158fb78d1267c16bbbf29eb690e0b4039f
lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/controller/LoginController.java
@@ -4,6 +4,7 @@
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -14,6 +15,7 @@
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.jeecg.common.util.encryption.EncryptedString;
@@ -29,7 +31,6 @@
import org.jeecg.modules.system.util.RandImageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -119,8 +120,59 @@
      String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
      String syspassword = sysUser.getPassword();
      if (!syspassword.equals(userpassword)) {
         // 获取用户失败最大次数
         //Integer maxAttempts = CommonConstant.MAX_ATTEMPTS;
         List<DictModel> dictModels = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_MAX_ATTEMPTS);
         if (dictModels != null && !dictModels.isEmpty()) {
            Integer maxAttempts = Integer.valueOf(dictModels.get(0).getValue());
            if (redisUtil.hasKey(CommonConstant.PREFIX_LOGIN_COUNT + username)) {
               Integer loginCount = (Integer) redisUtil.get(CommonConstant.PREFIX_LOGIN_COUNT + username) + 1;
               if (loginCount < maxAttempts) {
                  result.error500("用户名或密码错误,请重新尝试。剩余尝试次数:" + (maxAttempts - loginCount));
                  redisUtil.set(CommonConstant.PREFIX_LOGIN_COUNT + username, loginCount + 1, 300);
               } else if (loginCount.equals(maxAttempts)) {
                  // 超过最大登录次数 锁定用户
                  //sysUserService.update(new SysUser().setStatus(CommonConstant.USER_FREEZE), new UpdateWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
                  // 获取用户失败锁定时长
                  List<DictModel> dictModelList = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_USER_FREEZE);
                  if (dictModelList != null && !dictModelList.isEmpty()) {
                     Integer userFreezeTime = Integer.valueOf(dictModelList.get(0).getValue());
                     redisUtil.set(CommonConstant.PREFIX_LOGIN_COUNT + username, loginCount + 1, userFreezeTime * 60);
                     result.error500("用户名或密码错误,已到达最大尝试次数,请稍后尝试!");
                  } else {
                     //修改数据库锁定用户
                     sysUserService.update(new SysUser().setStatus(CommonConstant.USER_FREEZE), new UpdateWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
                     //删除redis信息
                     redisUtil.del(CommonConstant.PREFIX_LOGIN_COUNT + username);
                     result.error500("用户名或密码错误,已到达最大尝试次数,请联系管理员解锁!");
                  }
               } else {
                  result.error500("您的账户已锁定,请稍后尝试!");
               }
            } else {
               redisUtil.set(CommonConstant.PREFIX_LOGIN_COUNT + username, 1, 300);
               result.error500("用户名或密码错误,请重新尝试。剩余尝试次数:" + (maxAttempts - 1));
            }
         } else {
         result.error500("用户名或密码错误");
         }
         //result.error500("用户名或密码错误");
         return result;
      } else {
         if (redisUtil.hasKey(CommonConstant.PREFIX_LOGIN_COUNT + username)) {
            List<DictModel> dictModels = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_MAX_ATTEMPTS);
            if (dictModels != null && !dictModels.isEmpty()) {
               Integer maxAttempts = Integer.valueOf(dictModels.get(0).getValue());
               Integer loginCount = (Integer) redisUtil.get(CommonConstant.PREFIX_LOGIN_COUNT + username);
               if (loginCount < maxAttempts) {
                  redisUtil.del(CommonConstant.PREFIX_LOGIN_COUNT + username);
               } else {
                  result.error500("您的账户已锁定,请稍后尝试!");
                  return result;
               }
            }
         }
      }
            
      //用户登录信息
@@ -429,9 +481,16 @@
      // 生成token
      String token = JwtUtil.sign(username, syspassword);
      // 获取token缓存有效时间
      Integer expireTime = CommonConstant.TOKEN_EXPIRE_TIME;
      List<DictModel> dictModels = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_TOKEN_EXPIRE_TIME);
      if (dictModels != null && !dictModels.isEmpty()) {
         expireTime = Integer.valueOf(dictModels.get(0).getValue());
      }
      // 设置token缓存有效时间
      redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
      redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
      redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token, (long) (expireTime + 1) * 60 * 60);
      //redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, );
      obj.put("token", token);
      // update-begin--Author:sunjianlei Date:20210802 for:获取用户租户信息