src/main/java/com/ycl/jxkg/config/spring/security/RestAuthenticationProvider.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/ycl/jxkg/utils/CaffeineUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
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()); } src/main/java/com/ycl/jxkg/utils/CaffeineUtil.java
New file @@ -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:xp * @date:2024/7/10 11:50 */ @Slf4j @Component @RequiredArgsConstructor public class CaffeineUtil { private final Cache<String, Object> caffeineCache; /** * 保存 * * @param database 数据库/ caffeine map的key * @param key map的key * @param value map的value */ 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的key * @param key map的key */ 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); } }