Search in sources :

Example 1 with BaseException

use of com.zy.usercenteradmin.common.BaseException in project usercenteradmin by caijiya.

the class LoginServiceImpl method login.

@Override
public String login(LoginDTO loginDTO, String captchaKey) {
    String captchaCode = stringRedisTemplate.opsForValue().get(Constants.REDIS_KEY_CAPTCHA + captchaKey);
    if (StrUtil.isBlank(captchaCode) || !loginDTO.getCaptchaCode().equals(captchaCode)) {
        log.warn("验证码校验失败");
        throw new BaseException(ResultCodeEnum.CAPTCHA_ERROR);
    }
    String username = loginDTO.getUsername();
    User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
    if (user == null) {
        throw new BaseException(ResultCodeEnum.USERNAME_PASSWORD_ERROR);
    }
    String salt = user.getSalt();
    if (!DigestUtil.md5Hex(salt + loginDTO.getPassword()).equals(user.getPassword())) {
        throw new BaseException(ResultCodeEnum.USERNAME_PASSWORD_ERROR);
    }
    // 将用户信息、权限信息缓存到redis todo 暂时只存基本信息
    String ticket = UUID.randomUUID().toString();
    // 存储token与id的映射
    redisTemplate.opsForValue().set(Constants.TOKEN_PREFIX + ticket, user.getId(), 30, TimeUnit.MINUTES);
    // 存储用户信息
    redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_PREFIX + user.getId(), user, 30, TimeUnit.MINUTES);
    return ticket;
}
Also used : BaseException(com.zy.usercenteradmin.common.BaseException) User(com.zy.usercenteradmin.entity.User) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)

Example 2 with BaseException

use of com.zy.usercenteradmin.common.BaseException in project usercenteradmin by caijiya.

the class LoginInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    String tokenHeader = request.getHeader(Constants.TOKEN_HEADER);
    if (StrUtil.isBlank(tokenHeader)) {
        throw new BaseException(ResultCodeEnum.LOGIN_EXPIRATION);
    }
    String userId = stringRedisTemplate.opsForValue().get(Constants.TOKEN_PREFIX + tokenHeader);
    if (StrUtil.isBlank(userId)) {
        throw new BaseException(ResultCodeEnum.LOGIN_EXPIRATION);
    }
    User user = (User) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_PREFIX + userId);
    if (user == null) {
        throw new BaseException(ResultCodeEnum.LOGIN_EXPIRATION);
    }
    UserInfoHelper.add(user);
    return true;
}
Also used : BaseException(com.zy.usercenteradmin.common.BaseException) User(com.zy.usercenteradmin.entity.User)

Aggregations

BaseException (com.zy.usercenteradmin.common.BaseException)2 User (com.zy.usercenteradmin.entity.User)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1