From f4b1a132caad3002db25c999d931c41e5aa62019 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 21 七月 2023 16:21:05 +0800
Subject: [PATCH] 代理服务的权限验证添加缓存功能以优化性能

---
 src/main/java/com/moon/server/service/sys/ProxyService.java |   28 +++++++++++++++++++---------
 1 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/moon/server/service/sys/ProxyService.java b/src/main/java/com/moon/server/service/sys/ProxyService.java
index ba3845e..aa518a4 100644
--- a/src/main/java/com/moon/server/service/sys/ProxyService.java
+++ b/src/main/java/com/moon/server/service/sys/ProxyService.java
@@ -1,9 +1,7 @@
 package com.moon.server.service.sys;
 
 import com.alibaba.fastjson.JSON;
-import com.moon.server.entity.all.HttpStatus;
-import com.moon.server.entity.all.ResponseMsg;
-import com.moon.server.entity.all.StaticData;
+import com.moon.server.entity.all.*;
 import com.moon.server.entity.sys.ResEntity;
 import com.moon.server.entity.sys.ResLogEntity;
 import com.moon.server.entity.sys.TokenEntity;
@@ -14,6 +12,7 @@
 import com.moon.server.helper.WebHelper;
 import com.moon.server.interceptor.AuthInterceptor;
 import com.moon.server.service.all.PermsService;
+import com.moon.server.service.all.RedisService;
 import com.moon.server.service.all.SysService;
 import org.springframework.stereotype.Service;
 
@@ -22,6 +21,7 @@
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 import java.util.TimerTask;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 浠g悊鏈嶅姟绫�
@@ -30,6 +30,8 @@
  */
 @Service
 public class ProxyService {
+    @Resource
+    RedisService redisService;
 
     @Resource
     private SysService sysService;
@@ -47,11 +49,8 @@
      */
     public void proxyUrl(String token, int resId, HttpServletRequest req, HttpServletResponse res) throws Exception {
         // 3.鑾峰彇鐢ㄦ埛
-        UserEntity ue = getUser(res, token);
+        UserEntity ue = getUser(req, res, token);
         if (null == ue) {
-            return;
-        }
-        if (!check(req, res, ue, token)) {
             return;
         }
 
@@ -72,12 +71,23 @@
     /**
      * 鑾峰彇鐢ㄦ埛
      */
-    private UserEntity getUser(HttpServletResponse res, String token) {
+    private UserEntity getUser(HttpServletRequest req, HttpServletResponse res, String token) {
+        String key = RedisCacheKey.permsProxy(token);
+        Object obj = redisService.get(key);
+        if (obj instanceof UserEntity) {
+            return (UserEntity) obj;
+        }
+
         UserEntity ue = sysService.tokenService.getUserByToken(token);
-        if (ue == null) {
+        if (null == ue) {
             WebHelper.writeStr2Page(res, AuthInterceptor.NO_LOGIN);
             return null;
         }
+        if (!check(req, res, ue, token)) {
+            return null;
+        }
+
+        redisService.put(key, ue, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES);
 
         return ue;
     }

--
Gitblit v1.9.3