|
@@ -7,10 +7,17 @@ import cn.hutool.core.util.CharsetUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import cn.hutool.crypto.symmetric.AES;
|
|
|
+import com.sxtvs.open.OpenApplication;
|
|
|
+import com.sxtvs.open.api.user.entity.User;
|
|
|
+import com.sxtvs.open.api.user.service.impl.UserServiceImpl;
|
|
|
import com.sxtvs.open.core.advice.BizException;
|
|
|
import com.sxtvs.open.core.conf.Constant;
|
|
|
+import jakarta.annotation.PostConstruct;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.ZoneOffset;
|
|
|
|
|
|
public class AESUtil {
|
|
|
private static final AES aes = SecureUtil.aes(Constant.AES_KEY.getBytes(StandardCharsets.UTF_8));
|
|
@@ -19,7 +26,7 @@ public class AESUtil {
|
|
|
//加入了随机旋转和时间戳 避免加密后得到的是同样的数据 得到的token比传统的jwt要小很多
|
|
|
int randomInt = RandomUtil.randomInt(data.length());
|
|
|
data = shift(data, randomInt);
|
|
|
- return aes.encryptHex(randomInt + "," + data + "," + (getCurrentTime() + addSecond));
|
|
|
+ return aes.encryptHex(randomInt + "," + data + "," + (getCurrentTime() + addSecond)+ "," + (getCurrentTime()));
|
|
|
}
|
|
|
|
|
|
public static String encryptHex(String data){
|
|
@@ -29,10 +36,12 @@ public class AESUtil {
|
|
|
public static String decryptStr(String token) {
|
|
|
String[] dataArray;
|
|
|
long time;
|
|
|
+ long create;
|
|
|
try {
|
|
|
token = aes.decryptStr(token, CharsetUtil.CHARSET_UTF_8);
|
|
|
dataArray = token.split(",");
|
|
|
time = Long.parseLong(dataArray[2]);
|
|
|
+ create = Long.parseLong(dataArray[3]);
|
|
|
} catch (Exception e) {
|
|
|
throw new BizException(Constant.TOKEN_PARSE_ERROR, "token 异常");
|
|
|
}
|
|
@@ -40,7 +49,12 @@ public class AESUtil {
|
|
|
throw new BizException(Constant.TOKEN_EXPIRE_ERROR, "token 已过期");
|
|
|
}
|
|
|
int i = Integer.parseInt(dataArray[0]);
|
|
|
- return shift(dataArray[1], i * -1);
|
|
|
+ String uid = shift(dataArray[1], i * -1);
|
|
|
+ User user = ApplicationContextHolder.getContext().getBean(UserServiceImpl.class).getById(uid);
|
|
|
+ if (null != user.getLogoutTime() && user.getLogoutTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli() / 1000 - Constant.SUB_TIME >= create){
|
|
|
+ throw new BizException(Constant.TOKEN_EXPIRE_ERROR, "token 已过期");
|
|
|
+ }
|
|
|
+ return uid;
|
|
|
}
|
|
|
|
|
|
public static boolean isOk(String token){
|