mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 22:57:17 +08:00 
			
		
		
		
	perf: 使用 CompletableFuture 实现异步加载用户权限、角色代码和角色信息,以提高登录时的性能和响应速度
This commit is contained in:
		| @@ -18,6 +18,7 @@ package top.continew.admin.common.model.dto; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
| import top.continew.admin.common.constant.SysConstants; | ||||
|  | ||||
| import java.io.Serial; | ||||
| @@ -32,6 +33,7 @@ import java.util.Set; | ||||
|  * @since 2022/12/24 13:01 | ||||
|  */ | ||||
| @Data | ||||
| @NoArgsConstructor | ||||
| public class LoginUser implements Serializable { | ||||
|  | ||||
|     @Serial | ||||
| @@ -97,6 +99,12 @@ public class LoginUser implements Serializable { | ||||
|      */ | ||||
|     private LocalDateTime loginTime; | ||||
|  | ||||
|     public LoginUser(Set<String> permissions, Set<String> roleCodes, Set<RoleDTO> roles) { | ||||
|         this.permissions = permissions; | ||||
|         this.roleCodes = roleCodes; | ||||
|         this.roles = roles; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 是否为管理员 | ||||
|      * | ||||
|   | ||||
| @@ -30,6 +30,7 @@ import cn.hutool.json.JSONUtil; | ||||
| import jakarta.servlet.http.HttpServletRequest; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import me.zhyd.oauth.model.AuthUser; | ||||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
| @@ -44,6 +45,7 @@ import top.continew.admin.common.enums.GenderEnum; | ||||
| import top.continew.admin.common.enums.MenuTypeEnum; | ||||
| import top.continew.admin.common.enums.MessageTypeEnum; | ||||
| import top.continew.admin.common.model.dto.LoginUser; | ||||
| import top.continew.admin.common.model.dto.RoleDTO; | ||||
| import top.continew.admin.common.util.helper.LoginHelper; | ||||
| import top.continew.admin.system.enums.MessageTemplateEnum; | ||||
| import top.continew.admin.system.enums.PasswordPolicyEnum; | ||||
| @@ -64,6 +66,7 @@ import top.continew.starter.messaging.websocket.util.WebSocketUtils; | ||||
| import java.time.Duration; | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.*; | ||||
| import java.util.concurrent.CompletableFuture; | ||||
|  | ||||
| /** | ||||
|  * 登录业务实现 | ||||
| @@ -86,6 +89,7 @@ public class LoginServiceImpl implements LoginService { | ||||
|     private final MessageService messageService; | ||||
|     private final PasswordEncoder passwordEncoder; | ||||
|     private final OptionService optionService; | ||||
|     private final ThreadPoolTaskExecutor threadPoolTaskExecutor; | ||||
|  | ||||
|     @Override | ||||
|     public String accountLogin(String username, String password, HttpServletRequest request) { | ||||
| @@ -199,10 +203,15 @@ public class LoginServiceImpl implements LoginService { | ||||
|      */ | ||||
|     private String login(UserDO user) { | ||||
|         Long userId = user.getId(); | ||||
|         LoginUser loginUser = BeanUtil.copyProperties(user, LoginUser.class); | ||||
|         loginUser.setPermissions(permissionService.listPermissionByUserId(userId)); | ||||
|         loginUser.setRoleCodes(permissionService.listRoleCodeByUserId(userId)); | ||||
|         loginUser.setRoles(roleService.listByUserId(userId)); | ||||
|         CompletableFuture<Set<String>> permissionFuture = CompletableFuture.supplyAsync(() -> permissionService | ||||
|             .listPermissionByUserId(userId), threadPoolTaskExecutor); | ||||
|         CompletableFuture<Set<String>> roleCodeFuture = CompletableFuture.supplyAsync(() -> permissionService | ||||
|             .listRoleCodeByUserId(userId), threadPoolTaskExecutor); | ||||
|         CompletableFuture<Set<RoleDTO>> roleFuture = CompletableFuture.supplyAsync(() -> roleService | ||||
|             .listByUserId(userId), threadPoolTaskExecutor); | ||||
|         CompletableFuture.allOf(permissionFuture, roleCodeFuture, roleFuture); | ||||
|         LoginUser loginUser = new LoginUser(permissionFuture.join(), roleCodeFuture.join(), roleFuture.join()); | ||||
|         BeanUtil.copyProperties(user, loginUser); | ||||
|         return LoginHelper.login(loginUser); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -55,6 +55,10 @@ continew-starter.thread-pool: | ||||
|   queue-capacity: 128 | ||||
|   # 活跃时间(单位:秒) | ||||
|   keep-alive-seconds: 300 | ||||
|   # 关闭线程池是否等待任务完成 | ||||
|   wait-for-tasks-to-complete-on-shutdown: true | ||||
|   # 执行器在关闭时阻塞的最长毫秒数,以等待剩余任务完成执行(单位:毫秒) | ||||
|   await-termination-millis: 30000 | ||||
|  | ||||
| --- ### 接口文档配置 | ||||
| springdoc: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user