This commit is contained in:
caozehui
2024-11-26 10:03:29 +08:00
parent 8637eb946c
commit ce2a8c3bc6
3 changed files with 19 additions and 10 deletions

View File

@@ -3,7 +3,6 @@ package com.njcn.common.bean;
import cn.hutool.cache.CacheUtil; import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache; import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUnit;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Objects; import java.util.Objects;
@@ -60,6 +59,7 @@ public class CustomCacheUtil {
/** /**
* 获取缓存值, * 获取缓存值,
*
* @param key 缓存key * @param key 缓存key
* @param flush 是否重置生命周期 true 重置false 不重置 * @param flush 是否重置生命周期 true 重置false 不重置
*/ */
@@ -72,4 +72,12 @@ public class CustomCacheUtil {
} }
return timedCache.get(key, flush); return timedCache.get(key, flush);
} }
public void remove(String key) {
if (Objects.isNull(timedCache)) {
this.timedCache = initTimedCache();
timedCache.schedulePrune(60 * 1000);
}
timedCache.remove(key);
}
} }

View File

@@ -42,7 +42,7 @@ public interface SecurityConstants {
/** /**
* 认证成功后,返回信息包含的内容- * 认证成功后,返回信息包含的内容-
*/ */
String USER_INDEX_KEY = "userIndex"; String USER_ID = "userId";
String USER_TYPE = "userType"; String USER_TYPE = "userType";
String USER_NAME_KEY = "user_name"; String USER_NAME_KEY = "user_name";
String USER_HEAD_KEY = "headSculpture"; String USER_HEAD_KEY = "headSculpture";

View File

@@ -3,6 +3,7 @@ package com.njcn.common.utils;
import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUnit;
import cn.hutool.jwt.JWT; import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil; import cn.hutool.jwt.JWTUtil;
import com.njcn.common.pojo.constant.SecurityConstants;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; import java.time.Instant;
@@ -36,7 +37,7 @@ public class JwtUtil {
headers.put("typ", "JWT"); headers.put("typ", "JWT");
headers.put("alg", "HS256"); headers.put("alg", "HS256");
Map<String, Object> payload = new HashMap<>(); Map<String, Object> payload = new HashMap<>();
payload.put("userId", userId); payload.put(SecurityConstants.USER_ID, userId);
payload.put("exp", Instant.now().plusSeconds(ACCESS_EXPIRE).getEpochSecond()); payload.put("exp", Instant.now().plusSeconds(ACCESS_EXPIRE).getEpochSecond());
payload.put("sub", SUBJECT); payload.put("sub", SUBJECT);
payload.put("iss", JWT_ISS); payload.put("iss", JWT_ISS);