From 87b4f140865c61929de02b156e43bd9edb29cc19 Mon Sep 17 00:00:00 2001 From: Handiwork Date: Sat, 25 Nov 2023 10:29:11 +0800 Subject: [PATCH] refactor: enable profile to specify max quantity of jwt login devices --- src/main/java/plus/maa/backend/config/external/Jwt.java | 4 ++++ src/main/java/plus/maa/backend/service/UserService.java | 9 +++------ src/main/resources/application-template.yml | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/plus/maa/backend/config/external/Jwt.java b/src/main/java/plus/maa/backend/config/external/Jwt.java index f3718e39..94f6f578 100644 --- a/src/main/java/plus/maa/backend/config/external/Jwt.java +++ b/src/main/java/plus/maa/backend/config/external/Jwt.java @@ -19,4 +19,8 @@ public class Jwt { * JwtToken的加密密钥 */ private String secret; + /** + * Jwt 最大同时登录设备数 + */ + private int maxLogin = 1; } \ No newline at end of file diff --git a/src/main/java/plus/maa/backend/service/UserService.java b/src/main/java/plus/maa/backend/service/UserService.java index 764c55e4..9c80e178 100644 --- a/src/main/java/plus/maa/backend/service/UserService.java +++ b/src/main/java/plus/maa/backend/service/UserService.java @@ -10,11 +10,11 @@ import org.springframework.stereotype.Service; import plus.maa.backend.common.MaaStatusCode; import plus.maa.backend.common.utils.converter.MaaUserConverter; +import plus.maa.backend.config.external.MaaCopilotProperties; import plus.maa.backend.controller.request.user.*; import plus.maa.backend.controller.response.MaaResultException; import plus.maa.backend.controller.response.user.MaaLoginRsp; import plus.maa.backend.controller.response.user.MaaUserInfo; -import plus.maa.backend.repository.RedisCache; import plus.maa.backend.repository.UserRepository; import plus.maa.backend.repository.entity.MaaUser; import plus.maa.backend.service.jwt.JwtExpiredException; @@ -34,16 +34,13 @@ @Service @RequiredArgsConstructor public class UserService { - - // 未来转为配置项 - private static final int LOGIN_LIMIT = 1; - private final UserRepository userRepository; private final EmailService emailService; private final PasswordEncoder passwordEncoder; private final UserDetailServiceImpl userDetailService; private final JwtService jwtService; private final MaaUserConverter maaUserConverter; + private final MaaCopilotProperties properties; /** * 登录方法 @@ -64,7 +61,7 @@ public MaaLoginRsp login(LoginDTO loginDTO) { var jwtId = UUID.randomUUID().toString(); var jwtIds = user.getRefreshJwtIds(); jwtIds.add(jwtId); - while (jwtIds.size() > LOGIN_LIMIT) jwtIds.remove(0); + while (jwtIds.size() > properties.getJwt().getMaxLogin()) jwtIds.remove(0); userRepository.save(user); var authorities = userDetailService.collectAuthoritiesFor(user); diff --git a/src/main/resources/application-template.yml b/src/main/resources/application-template.yml index bea8501b..6d8ae4f3 100644 --- a/src/main/resources/application-template.yml +++ b/src/main/resources/application-template.yml @@ -30,6 +30,7 @@ maa-copilot: expire: 21600 # JwtToken的加密密钥 secret: $I_Am_The_Bone_Of_My_Sword!Steel_Is_My_Body_And_Fire_Is_My_Blood!$ + max-login: 1 github: # GitHub api token token: github_pat_xxx