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

com.seepine.auth.util.AuthUtil Maven / Gradle / Ivy

There is a newer version: 2.0.0-beta.18
Show newest version
package com.seepine.auth.util;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.seepine.auth.entity.AuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/** @author seepine */
@Component
@DependsOn({"redisTemplate", "authProperties"})
public class AuthUtil {
  private static AuthUtil authUtil;
  @Autowired private RedisTemplate redisTemplate;
  @Autowired private AuthProperties authProperties;

  private final ThreadLocal THREAD_LOCAL_TENANT = new ThreadLocal<>();
  private final ThreadLocal THREAD_LOCAL_TOKEN = new ThreadLocal<>();

  private AuthUtil() {}

  @PostConstruct
  public void init() {
    authUtil = this;
    authUtil.redisTemplate = this.redisTemplate;
    authUtil.authProperties = this.authProperties;
  }

  public static void clear() {
    authUtil.THREAD_LOCAL_TENANT.remove();
    authUtil.THREAD_LOCAL_TOKEN.remove();
  }

  /**
   * 在controller/service中使用,直接获取当前登录者用户信息
   *
   * @param  范型
   * @return user
   */
  @SuppressWarnings("unchecked")
  public static  T getUser() {
    return (T) authUtil.THREAD_LOCAL_TENANT.get();
  }

  /**
   * 登录成功后设置用户信息,并返回token
   *
   * @param user user
   * @return token
   */
  public static String loginSuccess(Object user) {
    String token = IdUtil.fastSimpleUUID();
    putIntoCache(token, user);
    // 设置过期时间
    authUtil.redisTemplate.expire(
        authUtil.authProperties.getCacheKey() + token,
        authUtil.authProperties.getTimeout(),
        authUtil.authProperties.getUnit());
    return token;
  }

  /**
   * 通过token获取用户信息
   *
   * @param token token
   * @param  范型
   * @return user
   */
  @SuppressWarnings("unchecked")
  public static  T getUserByToken(String token) {
    if (StrUtil.isBlank(token)) {
      return null;
    }
    Object user =
        authUtil
            .redisTemplate
            .opsForHash()
            .get(authUtil.authProperties.getCacheKey() + token, token);
    if (user == null) {
      return null;
    }
    if (authUtil.authProperties.getResetTimeout()) {
      authUtil.redisTemplate.expire(
          authUtil.authProperties.getCacheKey() + token,
          authUtil.authProperties.getTimeout(),
          authUtil.authProperties.getUnit());
    }
    authUtil.THREAD_LOCAL_TENANT.set(user);
    authUtil.THREAD_LOCAL_TOKEN.set(token);
    return (T) user;
  }

  /**
   * 刷新用户信息,当进行了更新等操作
   * 支持该方法为未登录线程赋值user对象,提供getUser()取值用
   *
   * @param user user
   * @return boolean
   */
  public static boolean refreshUser(Object user) {
    authUtil.THREAD_LOCAL_TENANT.set(user);
    String token = authUtil.THREAD_LOCAL_TOKEN.get();
    if (token == null || "".equals(token)) {
      return false;
    }
    putIntoCache(token, user);
    return true;
  }

  private static void putIntoCache(String token, Object user) {
    authUtil.THREAD_LOCAL_TENANT.set(user);
    authUtil
        .redisTemplate
        .opsForHash()
        .put(authUtil.authProperties.getCacheKey() + token, token, user);
  }
}