Search in sources :

Example 1 with UserException

use of com.ruoyi.common.exception.user.UserException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class SysLoginService method login.

/**
 * 登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param code     验证码
 * @param uuid     唯一标识
 * @return 结果
 */
public String login(String username, String password, String code, String uuid) {
    HttpServletRequest request = ServletUtils.getRequest();
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, code, uuid, request);
    }
    // 获取用户登录错误次数(可自定义限制策略 例如: 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);
    }
    SysUser user = loadUserByUsername(username);
    if (!BCrypt.checkpw(password, user.getPassword())) {
        // 是否第一次
        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);
        } else {
            // 未达到规定错误次数 则递增
            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);
        }
    }
    // 登录成功 清空错误次数
    RedisUtils.deleteObject(Constants.LOGIN_ERROR + username);
    LoginUser loginUser = buildLoginUser(user);
    // 生成token
    LoginHelper.loginByDevice(loginUser, DeviceType.PC);
    asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
    recordLoginInfo(user.getUserId(), username);
    return StpUtil.getTokenValue();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SysUser(com.ruoyi.common.core.domain.entity.SysUser) UserException(com.ruoyi.common.exception.user.UserException) LoginUser(com.ruoyi.common.core.domain.model.LoginUser)

Example 2 with UserException

use of com.ruoyi.common.exception.user.UserException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class SysRegisterService method register.

/**
 * 注册
 */
public void register(RegisterBody registerBody) {
    HttpServletRequest request = ServletUtils.getRequest();
    String username = registerBody.getUsername();
    String password = registerBody.getPassword();
    // 校验用户类型是否存在
    String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, registerBody.getCode(), registerBody.getUuid(), request);
    }
    if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) {
        throw new UserException("user.register.save.error", username);
    }
    SysUser sysUser = new SysUser();
    sysUser.setUserName(username);
    sysUser.setNickName(username);
    sysUser.setPassword(BCrypt.hashpw(password));
    sysUser.setUserType(userType);
    boolean regFlag = userService.registerUser(sysUser);
    if (!regFlag) {
        throw new UserException("user.register.error");
    }
    asyncService.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"), request);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SysUser(com.ruoyi.common.core.domain.entity.SysUser) UserException(com.ruoyi.common.exception.user.UserException)

Example 3 with UserException

use of com.ruoyi.common.exception.user.UserException in project RuoYi-Vue-Plus by JavaLionLi.

the class SysLoginService method login.

/**
 * 登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param code     验证码
 * @param uuid     唯一标识
 * @return 结果
 */
public String login(String username, String password, String code, String uuid) {
    HttpServletRequest request = ServletUtils.getRequest();
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, code, uuid, request);
    }
    // 获取用户登录错误次数(可自定义限制策略 例如: 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);
    }
    SysUser user = loadUserByUsername(username);
    if (!BCrypt.checkpw(password, user.getPassword())) {
        // 是否第一次
        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);
        } else {
            // 未达到规定错误次数 则递增
            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);
        }
    }
    // 登录成功 清空错误次数
    RedisUtils.deleteObject(Constants.LOGIN_ERROR + username);
    LoginUser loginUser = buildLoginUser(user);
    // 生成token
    LoginHelper.loginByDevice(loginUser, DeviceType.PC);
    asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
    recordLoginInfo(user.getUserId(), username);
    return StpUtil.getTokenValue();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SysUser(com.ruoyi.common.core.domain.entity.SysUser) UserException(com.ruoyi.common.exception.user.UserException) LoginUser(com.ruoyi.common.core.domain.model.LoginUser)

Example 4 with UserException

use of com.ruoyi.common.exception.user.UserException in project RuoYi-Vue-Plus by JavaLionLi.

the class SysRegisterService method register.

/**
 * 注册
 */
public void register(RegisterBody registerBody) {
    HttpServletRequest request = ServletUtils.getRequest();
    String username = registerBody.getUsername();
    String password = registerBody.getPassword();
    // 校验用户类型是否存在
    String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, registerBody.getCode(), registerBody.getUuid(), request);
    }
    if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) {
        throw new UserException("user.register.save.error", username);
    }
    SysUser sysUser = new SysUser();
    sysUser.setUserName(username);
    sysUser.setNickName(username);
    sysUser.setPassword(BCrypt.hashpw(password));
    sysUser.setUserType(userType);
    boolean regFlag = userService.registerUser(sysUser);
    if (!regFlag) {
        throw new UserException("user.register.error");
    }
    asyncService.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"), request);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SysUser(com.ruoyi.common.core.domain.entity.SysUser) UserException(com.ruoyi.common.exception.user.UserException)

Aggregations

SysUser (com.ruoyi.common.core.domain.entity.SysUser)4 UserException (com.ruoyi.common.exception.user.UserException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)2