From 254c2a69441dbd9ee9bcb1d134a05eb9da407d17 Mon Sep 17 00:00:00 2001
From: fuliqi <fuliqi@qq.com>
Date: 星期三, 10 七月 2024 14:03:05 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev

---
 src/main/java/com/ycl/jxkg/config/spring/security/RestAuthenticationProvider.java |   10 ++++
 src/main/java/com/ycl/jxkg/utils/CaffeineUtil.java                                |   68 ++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/ycl/jxkg/config/spring/security/RestAuthenticationProvider.java b/src/main/java/com/ycl/jxkg/config/spring/security/RestAuthenticationProvider.java
index e2ff848..9e5ae5d 100644
--- a/src/main/java/com/ycl/jxkg/config/spring/security/RestAuthenticationProvider.java
+++ b/src/main/java/com/ycl/jxkg/config/spring/security/RestAuthenticationProvider.java
@@ -1,11 +1,14 @@
 package com.ycl.jxkg.config.spring.security;
 
 
+import com.github.benmanes.caffeine.cache.Cache;
+import com.ycl.jxkg.constants.CaffeineConstant;
 import com.ycl.jxkg.context.WebContext;
 import com.ycl.jxkg.enums.RoleEnum;
 import com.ycl.jxkg.enums.UserStatusEnum;
 import com.ycl.jxkg.service.AuthenticationService;
 import com.ycl.jxkg.service.UserService;
+import com.ycl.jxkg.utils.CaffeineUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.authentication.AuthenticationProvider;
 import org.springframework.security.authentication.BadCredentialsException;
@@ -34,6 +37,7 @@
     private final AuthenticationService authenticationService;
     private final UserService userService;
     private final WebContext webContext;
+    private final CaffeineUtil caffeineUtil;
 
     /**
      * Instantiates a new Rest authentication provider.
@@ -43,10 +47,11 @@
      * @param webContext            the web context
      */
     @Autowired
-    public RestAuthenticationProvider(AuthenticationService authenticationService, UserService userService, WebContext webContext) {
+    public RestAuthenticationProvider(AuthenticationService authenticationService, UserService userService, WebContext webContext, CaffeineUtil caffeineUtil) {
         this.authenticationService = authenticationService;
         this.userService = userService;
         this.webContext = webContext;
+        this.caffeineUtil = caffeineUtil;
     }
 
     @Override
@@ -73,6 +78,9 @@
         grantedAuthorities.add(new SimpleGrantedAuthority(RoleEnum.fromCode(user.getRole()).getRoleName()));
 
         User authUser = new User(user.getUserName(), user.getPassword(), grantedAuthorities);
+
+        // 鐧诲綍涔嬪悗淇濆瓨鍒板唴瀛樹腑
+        caffeineUtil.put(CaffeineConstant.AUTH, authUser.getUsername(), authUser);
         return new UsernamePasswordAuthenticationToken(authUser, authUser.getPassword(), authUser.getAuthorities());
     }
 
diff --git a/src/main/java/com/ycl/jxkg/utils/CaffeineUtil.java b/src/main/java/com/ycl/jxkg/utils/CaffeineUtil.java
new file mode 100644
index 0000000..05ba205
--- /dev/null
+++ b/src/main/java/com/ycl/jxkg/utils/CaffeineUtil.java
@@ -0,0 +1,68 @@
+package com.ycl.jxkg.utils;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * caffeine 淇濆瓨銆佽幏鍙栧伐鍏�
+ *
+ * @author锛歺p
+ * @date锛�2024/7/10 11:50
+ */
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class CaffeineUtil {
+
+    private final Cache<String, Object> caffeineCache;
+
+    /**
+     * 淇濆瓨
+     *
+     * @param database 鏁版嵁搴�/ caffeine map鐨刱ey
+     * @param key map鐨刱ey
+     * @param value map鐨剉alue
+     */
+    public void put(String database, String key, Object value) {
+        Map<String, Object> map = (Map<String, Object>) caffeineCache.getIfPresent(database);
+        if (Objects.isNull(map)) {
+            log.error("缂撳瓨寮傚父");
+            map = new HashMap<>(128);
+            this.createDatabase(database, map);
+            return;
+        }
+        map.put(key, value);
+    }
+
+    /**
+     * 鑾峰彇
+     *
+     * @param database 鏁版嵁搴�/ caffeine map鐨刱ey
+     * @param key map鐨刱ey
+     */
+    public Object get(String database, String key) {
+        Map<String, Object> map = (Map<String, Object>) caffeineCache.getIfPresent(database);
+        if (Objects.isNull(map)) {
+            log.error("缂撳瓨寮傚父");
+            return null;
+        }
+        return map.get(key);
+    }
+
+    /**
+     * 鍒涘缓key
+     *
+     * @param database
+     * @param map
+     */
+    private void createDatabase(String database, Map<String, Object> map) {
+        caffeineCache.put(database, map);
+    }
+
+}

--
Gitblit v1.8.0