All Downloads are FREE. Search and download functionalities are using the official Maven repository.

work.wangjw.util.AuthUtil Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package work.wangjw.util;

import work.wangjw.bean.AuthSession;
import work.wangjw.bean.Constant;
import work.wangjw.config.ConfigCenter;
import work.wangjw.config.ConfigData;
import work.wangjw.dao.AuthDao;
import work.wangjw.exception.InvalidTokenException;
import work.wangjw.exception.NotLoginException;

import java.util.Objects;

/**
 * @author Gavin
 * @since 2021/7/21 9:55
 **/
public class AuthUtil {
    public static final String USERTYPE = "default";

    private static final AuthDao authDao = ConfigCenter.authDao;

    private static final ConfigData configData = ConfigCenter.configData;

    public static Boolean checkLogin(String userType) {
        try {
            getIdAsString(userType);
        } catch (RuntimeException e) {
            return false;
        }
        return true;
    }
    public static Boolean checkLogin(){
        return checkLogin(Constant.DEFAULT_USERTYPE);
    }

    public static String getIdAsString(String userType) {
        String token = getToken(userType);
        if (Objects.isNull(token)) {
            throw new NotLoginException("未登录");
        }
        String id = getIdByToken(token, userType);
        if (Objects.isNull(id)) {
            throw new InvalidTokenException("token无效");
        }
        updateActivityTime(userType);
        return id;
    }

    public static Integer getIdAsInt(String userType) {
        return Integer.valueOf(getIdAsString(userType));
    }

    public static Long getIdAsLong(String userType) {
        return Long.valueOf(getIdAsString(userType));
    }

    public static String getIdAsString() {
        return getIdAsString(Constant.DEFAULT_USERTYPE);
    }

    public static Integer getIdAsInt() {
        return getIdAsInt(Constant.DEFAULT_USERTYPE);
    }

    public static Long getIdAsLong() {
        return getIdAsLong(Constant.DEFAULT_USERTYPE);
    }

    /**
     * 通过token查询对应的id
     *
     * @param token token
     * @return id
     */
    private static String getIdByToken(String token, String userType) {
        AuthSession authSession = authDao.get(connectTokenKey(token, userType));
        if (Objects.isNull(authSession)) {
            return null;
        }
        long now = System.currentTimeMillis();
        Long expireTime = authSession.getExpireTime();
        Long activityExpireTime = authSession.getActivityExpireTime();
        if ((expireTime == -1L || expireTime >= now) && (activityExpireTime == -1L || activityExpireTime >= now)) {
            return authSession.getId();
        } else {
            delete(authSession);
        }
        return null;
    }

    private static String connectIdKey(String id, String userType) {
        return configData.getTokenName() + ":" + userType + ":id:" + id;
    }

    private static String connectTokenKey(String token, String userType) {
        return configData.getTokenName() + ":" + userType + ":token:" + token;
    }

    private static void delete(AuthSession authSession) {
        authDao.delete(connectIdKey(authSession.getId(),authSession.getUserType()));
        authDao.delete(connectTokenKey(authSession.getTokenValue(),authSession.getUserType()));
    }

    private static String getToken(String userType) {
        return Objects.requireNonNull(ConfigCenter.exchange.getRequest().getHeaders().get(getTokenName(userType))).get(0);
    }

    private static void updateActivityTime(String userType) {
        if (configData.getActivityExpireDuration() > 0) {
            AuthSession authSession = authDao.get(connectTokenKey(getToken(userType),userType));
            authSession.setActivityExpireTime(System.currentTimeMillis() + configData.getActivityExpireDuration() * 1000);
            authDao.set(connectIdKey(authSession.getId(),userType), authSession);
            authDao.set(connectTokenKey(authSession.getTokenValue(),userType), authSession);
        }
    }

    private static String getTokenName(String userType) {
        return configData.getTokenName() + "-" + userType;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy