diff --git a/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java b/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java index c0c3be74b3..b15dbd2735 100644 --- a/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java +++ b/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java @@ -60,6 +60,7 @@ import java.util.LinkedList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import org.springframework.stereotype.Service; @@ -106,6 +107,8 @@ public class UserServiceImpl extends SuperServiceImpl implemen private final TokenService tokenService; private final TokenMapper tokenMapper; + private final ReentrantLock lock = new ReentrantLock(); + @Override public Result registerUser(User user) { User userByUsername = getUserByUsername(user.getUsername()); @@ -199,13 +202,13 @@ public Result loginUser(LoginDTO loginDTO) { // save login log record loginLogService.saveLoginLog(user, Status.LOGIN_SUCCESS); - insertToken(userInfo); + upsertToken(userInfo); // Return the user information along with a success status return Result.succeed(userInfo, Status.LOGIN_SUCCESS); } - private void insertToken(UserDTO userInfo) { + private void upsertToken(UserDTO userInfo) { Integer userId = userInfo.getUser().getId(); SysToken sysToken = new SysToken(); String tokenValue = StpUtil.getTokenValueByLoginId(userId); @@ -221,7 +224,22 @@ private void insertToken(UserDTO userInfo) { sysToken.setCreator(userId); sysToken.setUpdater(userId); sysToken.setSource(SysToken.Source.LOGIN); - tokenMapper.insert(sysToken); + + try { + lock.lock(); + SysToken lastSysToken = + tokenMapper.selectOne(new LambdaQueryWrapper().eq(SysToken::getTokenValue, tokenValue)); + if (Asserts.isNull(lastSysToken)) { + tokenMapper.insert(sysToken); + } else { + sysToken.setId(lastSysToken.getId()); + tokenMapper.updateById(sysToken); + } + } catch (Exception e) { + log.error("update token info failed", e); + } finally { + lock.unlock(); + } } private User localLogin(LoginDTO loginDTO) throws AuthException {