1
luodangjia
2025-01-23 698ae84adaf1b8d0e8dd61d7279863fe17c1e81d
ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
@@ -23,14 +23,18 @@
import com.ruoyi.company.api.model.RegisterUser;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.AppUser;
import com.ruoyi.system.api.model.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 登录校验方法
@@ -164,12 +168,13 @@
                throw new ServiceException("验证码错误");
            }
            String[] split = captcha.split(":");
            long l = Long.parseLong(split[2]);
            long l = Long.parseLong(split[1]);
            long l1 = System.currentTimeMillis();
            // 判断是否大于两分钟
            if (l1 - l > 2 * 60 * 1000) {
                throw new CaptchaException("验证码已失效");
            }
            captcha = split[0];
            if (!smsCode.equalsIgnoreCase(captcha)) {
                throw new CaptchaException("验证码错误");
            }
@@ -214,12 +219,45 @@
    public void resetPwd(RegisterUser registerUser) {
        User user = check(registerUser);
        String verifyKey = CacheConstants.PHONE_CODE_KEY + StringUtils.nvl(registerUser.getPhone(), "");
        String captcha = redisService.getCacheObject(verifyKey);
        if (captcha == null) {
            throw new CaptchaException("验证码错误");
        }
        String[] split = captcha.split(":");
        long l = Long.parseLong(split[1]);
        long l1 = System.currentTimeMillis();
        // 判断是否大于两分钟
        if (l1 - l > 2 * 60 * 1000) {
            throw new CaptchaException("验证码已失效");
        }
        captcha = split[0];
        if (!registerUser.getCode().equalsIgnoreCase(captcha)) {
            throw new CaptchaException("验证码错误");
        }
        String password = SecurityUtils.encryptPassword(registerUser.getPassword());
        user.setPassword(password);
        R<Void> r = remoteCompanyUserService.updateUser(user, SecurityConstants.INNER);
        if (R.isError(r)) {
            throw new ServiceException(r.getMsg());
        }
        forceLogout(user.getUserId());
    }
    public void forceLogout(Long userId) {
        Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        for (String key : keys) {
            Object user = redisService.getCacheObject(key);
            if (user instanceof AppUser) {
                AppUser appUser = (AppUser) user;
                if (appUser.getUserId().equals(userId)) {
                    redisService.deleteObject(key);
                }
            }
        }
    }