管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-25 4ddfd502023662f6d25c4be416d88751e206d91a
src/main/java/com/lf/server/service/sys/TokenService.java
@@ -8,6 +8,7 @@
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WebHelper;
import com.lf.server.mapper.sys.TokenMapper;
import com.lf.server.service.all.PermsService;
import com.lf.server.service.all.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -27,9 +28,6 @@
@Service
public class TokenService implements TokenMapper {
    @Autowired
    public RedisService redisService;
    @Autowired
    TokenMapper tokenMapper;
    @Autowired
@@ -37,6 +35,12 @@
    @Autowired
    LoginService loginService;
    @Autowired
    public RedisService redisService;
    @Autowired
    public  PermsService permsService;
    @Override
    public Integer selectCount(String name, Integer type) {
@@ -142,8 +146,8 @@
            return false;
        }
        // 清除Cookie WebHelper.saveCookie(StaticData.TOKEN_COOKIE_KEY, "", 60, res)
        WebHelper.deleteAll(req, res);
        // 清除Cookie
        WebHelper.deleteCookies(req, res);
        // 获取当前用户
        UserEntity ue = getCurrentUser(req);
@@ -153,9 +157,13 @@
        // 清除缓存
        String tokenKey = RedisCacheKey.signTokenKey(token);
        redisService.delete(tokenKey);
        if (redisService.hasKey(tokenKey)) {
            redisService.delete(tokenKey);
        }
        String userKey = RedisCacheKey.signUserKey(te.getToken());
        redisService.delete(userKey);
        if (redisService.hasKey(userKey)) {
            redisService.delete(userKey);
        }
        // db,设置令牌过期
        te.setUpdateUser(ue.getId());
@@ -165,7 +173,7 @@
        }
        // 写日志
        LoginEntity le = loginService.getNewLogin(ue.getId(), 3, req);
        LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 3, 1, req);
        rows = loginService.insertLogin(le);
        return rows > 0;
@@ -183,7 +191,7 @@
        // redis
        Object obj = redisService.get(tokenKey);
        if (obj != null && obj instanceof TokenEntity) {
        if (obj instanceof TokenEntity) {
            return (TokenEntity) obj;
        }
@@ -214,9 +222,6 @@
    /**
     * 获取当前用户
     *
     * @param req
     * @return
     */
    public UserEntity getCurrentUser(HttpServletRequest req) {
        String token = WebHelper.getToken(req);
@@ -228,7 +233,7 @@
        // redis
        Object obj = redisService.get(userKey);
        if (obj != null && obj instanceof UserEntity) {
        if (obj instanceof UserEntity) {
            return (UserEntity) obj;
        }
@@ -243,4 +248,37 @@
        return ue;
    }
    /**
     * 设置密码错误缓存
     */
    public void setPwdErrCache(UserEntity ue) {
        String key = RedisCacheKey.signPwdError(ue.getUid());
        Object objCount = redisService.get(key);
        int count = objCount == null ? 1 : (int) objCount + 1;
        redisService.put(key, count, SettingData.PWD_ERR_TIME, TimeUnit.MINUTES);
        // 记录日志
        HttpServletRequest req = WebHelper.getRequest();
        LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 1, 0, req);
        le.setDescr("密码不正确");
        loginService.insertLogin(le);
        if (count >= SettingData.PWD_ERR_COUNT) {
            String token = WebHelper.getToken(req);
            HttpServletResponse res = WebHelper.getResponse();
            logout(token, req, res);
        }
    }
    /**
     * 用户ID是/否禁用
     */
    public boolean isUidDisable(UserEntity ue) {
        String key = RedisCacheKey.signPwdError(ue.getUid());
        Object objCount = redisService.get(key);
        return objCount != null && (int) objCount >= SettingData.PWD_ERR_COUNT;
    }
}