孙永军 1 年之前
父节点
当前提交
50c106468f

+ 1 - 1
src/main/java/com/sxtvs/open/api/news/controller/YoumeiDataController.java

@@ -41,6 +41,6 @@ public class YoumeiDataController {
 
     @RequestMapping("category")
     public List<String> category(){
-        return Arrays.asList("时政", "财经", "教育", "科技", "社会",  "体育", "游戏", "股票", "娱乐", "时尚", "彩票", "房产", "星座");
+        return Arrays.asList("时政", "财经", "教育", "科技", "社会",  "体育", "游戏", "股票", "娱乐"/*, "时尚", "彩票", "房产", "星座"*/);
     }
 }

+ 5 - 0
src/main/java/com/sxtvs/open/api/user/controller/UserController.java

@@ -38,5 +38,10 @@ public class UserController {
         return userService.getLoginUser();
     }
 
+    @RequestMapping("/logout")
+    @LoginRequired
+    public void logout() {
+        userService.logout();
+    }
 
 }

+ 2 - 0
src/main/java/com/sxtvs/open/api/user/entity/User.java

@@ -40,6 +40,8 @@ public class User implements Serializable {
 
     private LocalDateTime updateTime;
 
+    private LocalDateTime logoutTime;
+
     @TableField(exist = false)
     private UserRole userRole;
 }

+ 15 - 0
src/main/java/com/sxtvs/open/api/user/service/impl/UserServiceImpl.java

@@ -18,6 +18,7 @@ import jakarta.annotation.Resource;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
 import java.util.Arrays;
 import java.util.List;
 
@@ -100,4 +101,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
         user.setEncodePassword(AESUtil.encryptHex(pwd));
         save(user);
     }
+
+    public void logout(){
+        var token = HttpContextUtil.getToken();
+        if (token.isEmpty()) {
+            throw new RuntimeException("获取token异常");
+        }
+        var userId = AESUtil.decryptStr(token.get());
+        var user = this.lambdaQuery().eq(User::getId, Long.parseLong(userId)).one();
+        if (user == null) {
+            throw new RuntimeException("用户不存在");
+        }
+        user.setLogoutTime(LocalDateTime.now());
+        updateById(user);
+    }
 }

+ 16 - 2
src/main/java/com/sxtvs/open/core/auth/AESUtil.java

@@ -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){

+ 20 - 0
src/main/java/com/sxtvs/open/core/auth/ApplicationContextHolder.java

@@ -0,0 +1,20 @@
+package com.sxtvs.open.core.auth;
+
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ApplicationContextHolder implements ApplicationContextAware {
+    private static ApplicationContext context;
+
+    public void setApplicationContext(ApplicationContext context) throws BeansException {
+        ApplicationContextHolder.context = context;
+    }
+
+    public static ApplicationContext getContext() {
+        return context;
+    }
+}