From 1acf9a48021d0af1d81fdf3ed8fcf8dffd020f6b Mon Sep 17 00:00:00 2001 From: hongjli <3117313295@qq.com> Date: 星期二, 15 四月 2025 14:20:51 +0800 Subject: [PATCH] 登录,注册,获取用户信息---接口 --- src/main/java/com/weiwojc/service/impl/UserServiceImpl.java | 140 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 140 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/weiwojc/service/impl/UserServiceImpl.java b/src/main/java/com/weiwojc/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..50455cb --- /dev/null +++ b/src/main/java/com/weiwojc/service/impl/UserServiceImpl.java @@ -0,0 +1,140 @@ +package com.weiwojc.service.impl; + +import com.weiwojc.mapper.UserMapper; +import com.weiwojc.model.dto.UserLoginDTO; +import com.weiwojc.model.dto.UserRegisterDTO; +import com.weiwojc.model.entity.User; +import com.weiwojc.service.UserService; +import com.weiwojc.utils.JwtUtils; +import com.weiwojc.utils.PasswordUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.LockedException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class UserServiceImpl implements UserService { + + private final UserMapper userMapper; + private final JwtUtils jwtUtils; + + @Override + @Transactional + public User register(UserRegisterDTO registerDTO) { + // 妫�鏌ヨ处鍙峰悕鏄惁宸插瓨鍦� + User existingUser = userMapper.findByUsername(registerDTO.getAccountName()); + if (existingUser != null) { + throw new RuntimeException("璐﹀彿鍚嶅凡瀛樺湪"); + } + + // 鍒涘缓鏂扮敤鎴� + User user = new User(); + user.setUuid(UUID.randomUUID().toString()); + user.setUsername(registerDTO.getAccountName()); + user.setNickname(registerDTO.getNickname()); + + // 鐢熸垚鍔犲瘑瀵嗙爜 + String salt = PasswordUtils.generateSalt(); + String hashedPassword = PasswordUtils.hashPassword(registerDTO.getPassword(), salt); + user.setPasswordHash(hashedPassword); + user.setPasswordSalt(salt); // 淇濆瓨鐩愬�� + + // 璁剧疆鍏朵粬瀛楁 + user.setStatus(1); // 姝e父鐘舵�� + user.setRegisteredAt(LocalDateTime.now()); + user.setUpdatedAt(LocalDateTime.now()); + + // 淇濆瓨鐢ㄦ埛 + userMapper.insert(user); + return user; + } + + @Override + public String login(UserLoginDTO loginDTO) { + User user = userMapper.findByUsername(loginDTO.getAccountName()); + + if (user == null) { + throw new BadCredentialsException("璐﹀彿鍚嶆垨瀵嗙爜閿欒"); + } + + // 妫�鏌ヨ处鎴风姸鎬� + if (user.getStatus() == 0) { + throw new LockedException("璐︽埛宸茶绂佺敤"); + } + + // 妫�鏌ユ槸鍚﹁閿佸畾 + if (isUserLocked(user.getUserId())) { + throw new LockedException("璐︽埛宸茶閿佸畾锛岃绋嶅悗鍐嶈瘯"); + } + + // 楠岃瘉瀵嗙爜 + if (!PasswordUtils.verifyPassword(loginDTO.getPassword(), user.getPasswordHash())) { + // 澧炲姞鐧诲綍澶辫触娆℃暟 + incrementLoginAttempts(user.getUsername()); + throw new BadCredentialsException("璐﹀彿鍚嶆垨瀵嗙爜閿欒"); + } + + // 閲嶇疆鐧诲綍澶辫触娆℃暟 + resetLoginAttempts(user.getUsername()); + + // 鏇存柊鏈�鍚庣櫥褰曟椂闂� + updateLastLogin(user.getUserId()); + + // 鐢熸垚JWT浠ょ墝 + return jwtUtils.generateToken(user); + } + + @Override + public User getUserInfo(Long userId) { + return userMapper.findById(userId); + } + + @Override + public void updateLastLogin(Long userId) { + userMapper.updateLastLogin(userId, LocalDateTime.now()); + } + + @Override + public boolean isUserLocked(Long userId) { + User user = userMapper.findById(userId); + if (user == null) { + return false; + } + + // 妫�鏌ヨ处鎴烽攣瀹氱姸鎬� + if (user.getLockedUntil() != null && + LocalDateTime.now().isBefore(user.getLockedUntil())) { + return true; + } + + return false; + } + + @Override + public void incrementLoginAttempts(String username) { + User user = userMapper.findByUsername(username); + + if (user != null) { + int attempts = user.getLoginAttempts() == null ? 0 : user.getLoginAttempts(); + attempts++; + + LocalDateTime lockedUntil = null; + // 濡傛灉澶辫触娆℃暟杈惧埌5娆★紝閿佸畾30鍒嗛挓 + if (attempts >= 5) { + lockedUntil = LocalDateTime.now().plusMinutes(30); + } + + userMapper.updateLoginAttempts(user.getUserId(), attempts, lockedUntil); + } + } + + @Override + public void resetLoginAttempts(String username) { + userMapper.resetLoginAttempts(username); + } +} \ No newline at end of file -- Gitblit v1.9.3