|
|
@ -8,8 +8,10 @@ import com.example.demo.repository.UserSecurityQuestionRepository;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
@ -22,6 +24,9 @@ public class UserService {
|
|
|
|
private final UserSecurityQuestionRepository userSecurityQuestionRepository;
|
|
|
|
private final UserSecurityQuestionRepository userSecurityQuestionRepository;
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
public UserService(UserRepository userRepository,
|
|
|
|
public UserService(UserRepository userRepository,
|
|
|
|
ArticleRepository articleRepository,
|
|
|
|
ArticleRepository articleRepository,
|
|
|
@ -31,6 +36,7 @@ public class UserService {
|
|
|
|
this.articleRepository = articleRepository;
|
|
|
|
this.articleRepository = articleRepository;
|
|
|
|
this.userSecurityQuestionRepository = userSecurityQuestionRepository;
|
|
|
|
this.userSecurityQuestionRepository = userSecurityQuestionRepository;
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
|
|
|
|
logger.info("UserService dependencies injected successfully.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -49,6 +55,11 @@ public class UserService {
|
|
|
|
throw new IllegalArgumentException("Password cannot be null or empty.");
|
|
|
|
throw new IllegalArgumentException("Password cannot be null or empty.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置默认 accountType 如果未指定或无效
|
|
|
|
|
|
|
|
if (user.getAccountType() == null || !Arrays.asList(User.AccountType.USER, User.AccountType.ADMIN).contains(user.getAccountType())) {
|
|
|
|
|
|
|
|
user.setAccountType(User.AccountType.USER);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化 authorityStrings 和 securityQuestions 集合为空集合
|
|
|
|
// 初始化 authorityStrings 和 securityQuestions 集合为空集合
|
|
|
|
if (user.getAuthorityStrings() == null) {
|
|
|
|
if (user.getAuthorityStrings() == null) {
|
|
|
|
user.setAuthorityStrings(Collections.emptySet());
|
|
|
|
user.setAuthorityStrings(Collections.emptySet());
|
|
|
@ -68,6 +79,17 @@ public class UserService {
|
|
|
|
|
|
|
|
|
|
|
|
return userRepository.save(user);
|
|
|
|
return userRepository.save(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 用户登录验证。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
|
|
|
|
* @param password 用户密码
|
|
|
|
|
|
|
|
* @return 包含用户的 Optional 对象,如果找不到或密码不匹配则为空
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public Optional<User> loginUser(String loginAccount, String password) {
|
|
|
|
|
|
|
|
return userRepository.findByLoginAccount(loginAccount)
|
|
|
|
|
|
|
|
.filter(user -> passwordEncoder.matches(password, user.getPassword()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 根据登录账号查找用户。
|
|
|
|
* 根据登录账号查找用户。
|
|
|
@ -136,9 +158,9 @@ public class UserService {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 已登录用户请求重置密码。
|
|
|
|
* 已登录用户请求重置密码。
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
* @param currentPassword 用户当前密码
|
|
|
|
* @param currentPassword 用户当前密码
|
|
|
|
* @param newPassword 用户的新密码
|
|
|
|
* @param newPassword 用户的新密码
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void resetPasswordForLoggedInUser(String loginAccount, String currentPassword, String newPassword) {
|
|
|
|
public void resetPasswordForLoggedInUser(String loginAccount, String currentPassword, String newPassword) {
|
|
|
|
userRepository.findByLoginAccount(loginAccount).ifPresentOrElse(
|
|
|
|
userRepository.findByLoginAccount(loginAccount).ifPresentOrElse(
|
|
|
@ -167,7 +189,7 @@ public class UserService {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 添加安全问题到用户账户。
|
|
|
|
* 添加安全问题到用户账户。
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
* @param securityQuestions 安全问题集合
|
|
|
|
* @param securityQuestions 安全问题集合
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void addSecurityQuestions(String loginAccount, Set<UserSecurityQuestion> securityQuestions) {
|
|
|
|
public void addSecurityQuestions(String loginAccount, Set<UserSecurityQuestion> securityQuestions) {
|
|
|
@ -192,7 +214,7 @@ public class UserService {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 更新用户的安全问题。
|
|
|
|
* 更新用户的安全问题。
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
* @param loginAccount 用户登录账号
|
|
|
|
* @param securityQuestions 新的安全问题集合
|
|
|
|
* @param securityQuestions 新的安全问题集合
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void updateSecurityQuestions(String loginAccount, Set<UserSecurityQuestion> securityQuestions) {
|
|
|
|
public void updateSecurityQuestions(String loginAccount, Set<UserSecurityQuestion> securityQuestions) {
|
|
|
|