Skip to content

Commit

Permalink
refactor: enable profile to specify max quantity of jwt login devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Handiwork committed Nov 25, 2023
1 parent 565c4d1 commit 87b4f14
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main/java/plus/maa/backend/config/external/Jwt.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public class Jwt {
* JwtToken的加密密钥
*/
private String secret;
/**
* Jwt 最大同时登录设备数
*/
private int maxLogin = 1;
}
9 changes: 3 additions & 6 deletions src/main/java/plus/maa/backend/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
* 登录方法
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87b4f14

Please sign in to comment.