Search in sources :

Example 1 with UserNotFoundException

use of com.jxys.user.exception.UserNotFoundException in project springboot-scaffold by a241978181.

the class UserServiceImpl method signIn.

@Override
public SignInView signIn(SignInData data) {
    // 找到对应name的用户
    User user = userMapper.selectOne(new QueryWrapper<User>().select("*").eq("name", data.getName()));
    // 判断用户是否存在
    if (user != null) {
        // 校验密码
        if (data.getPassword().equals(EncryptConfigUtil.decyptPwd(this.encryptorPassword, user.getPassword()))) {
            // 校验通过,登陆成功,返回Token
            SignInView signInView = new SignInView();
            // 生成AccessToken
            signInView.setAccessToken(tokenService.generate(TokenSubject.ACCESS, user.getId()));
            // 生成RrefreshToken,有效期为24小时
            signInView.setRefreshToken(tokenService.generate(TokenSubject.REFRESH, user.getId(), 24));
            // 将对象序列化为JOSN格式储存在Redis中
            try {
                this.redisTemplate.opsForValue().set(user.getId(), user, Constant.REDIS_OVERDUE_TIME_MINUTES, TimeUnit.MINUTES);
            } catch (Exception e) {
                logger.error("向redis中存入数据失败!", e);
                throw new ErrorServiceException();
            } finally {
                // 释放连接
                RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
            }
            // 登录对象信息
            signInView.setUser(user);
            return signInView;
        } else {
            // 自定义异常示范
            throw new PasswordErrorException();
        }
    } else {
        // 抛出用户不存在的服务异常
        throw new UserNotFoundException();
    }
}
Also used : UserNotFoundException(com.jxys.user.exception.UserNotFoundException) User(com.jxys.entity.User) SignInView(com.jxys.view.SignInView) PasswordErrorException(com.jxys.user.exception.PasswordErrorException) ErrorServiceException(com.jxys.base.exception.ErrorServiceException) ErrorServiceException(com.jxys.base.exception.ErrorServiceException) ServiceException(com.jxys.base.exception.ServiceException) UserNotFoundException(com.jxys.user.exception.UserNotFoundException) PasswordErrorException(com.jxys.user.exception.PasswordErrorException)

Aggregations

ErrorServiceException (com.jxys.base.exception.ErrorServiceException)1 ServiceException (com.jxys.base.exception.ServiceException)1 User (com.jxys.entity.User)1 PasswordErrorException (com.jxys.user.exception.PasswordErrorException)1 UserNotFoundException (com.jxys.user.exception.UserNotFoundException)1 SignInView (com.jxys.view.SignInView)1