You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
8.2 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. package com.example.demo.serviceImpl;
  2. import com.example.demo.domain.entity.Admin;
  3. import com.example.demo.domain.vo.Password;
  4. import com.example.demo.domain.vo.Result;
  5. import com.example.demo.mapper.AdminMapper;
  6. import com.example.demo.service.AdminService;
  7. import lombok.RequiredArgsConstructor;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.security.authentication.AuthenticationManager;
  10. import org.springframework.security.authentication.BadCredentialsException;
  11. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  12. import org.springframework.security.core.Authentication;
  13. import org.springframework.security.core.context.SecurityContextHolder;
  14. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  15. import org.springframework.stereotype.Service;
  16. import org.apache.commons.lang3.StringUtils;
  17. import java.util.Date;
  18. import java.util.regex.Pattern;
  19. @Service
  20. @RequiredArgsConstructor
  21. public class AdminServiceImpl implements AdminService {
  22. @Autowired
  23. private AuthenticationManager authenticationManager;
  24. private final AdminMapper adminMapper;
  25. @Override
  26. public Admin login(Admin admin) throws Exception {
  27. String account = admin.getAccount();
  28. String inputMachineId = admin.getMachineId();
  29. if (StringUtils.isBlank(account)) {
  30. throw new IllegalArgumentException("账号不能为空");
  31. }
  32. Admin adminInDB = adminMapper.getAdmin(account);
  33. System.out.println("adminInDB:" + adminInDB);
  34. System.out.println("adminInDB markets: " + adminInDB.getMarkets()); // 添加日志检查
  35. if (adminInDB == null) {
  36. throw new RuntimeException("无此精网号");
  37. }
  38. // 校验机器权限
  39. if (!hasPermissionToMachine(adminInDB, inputMachineId)) {
  40. throw new RuntimeException("你没有使用该机器的权限!");
  41. }
  42. try {
  43. System.out.println("admin:" + account);
  44. System.out.println("admin:" + admin.getPassword());
  45. UsernamePasswordAuthenticationToken token =
  46. new UsernamePasswordAuthenticationToken(account, admin.getPassword());
  47. Authentication authentication = authenticationManager.authenticate(token);
  48. SecurityContextHolder.getContext().setAuthentication(authentication); // 存储认证信息
  49. Admin authenticatedAdmin = (Admin) authentication.getPrincipal();
  50. System.out.println("authenticatedAdmin markets: " + authenticatedAdmin.getMarkets()); // 添加日志检查
  51. return authenticatedAdmin;
  52. } catch (Exception e) {
  53. System.out.println("密码错误" + e.getMessage());
  54. throw new RuntimeException("登录失败,请稍后再试", e);
  55. }
  56. }
  57. //获取用户ID
  58. @Override
  59. public String getId(String account) {
  60. return adminMapper.getAdmin(account).getId().toString();
  61. }
  62. @Override
  63. public String getName(String account) {
  64. return adminMapper.getName(account);
  65. }
  66. private boolean hasPermissionToMachine(Admin admin, String targetMachineId) {
  67. if (targetMachineId == null || admin.getMachineId() == null) {
  68. return false;
  69. }
  70. String[] machineIds = admin.getMachineId().split(",");
  71. for (String id : machineIds) {
  72. if (targetMachineId.equals(id)) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. private static final String PASSWORD_REGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)(?!\\W+$).{8,16}$";
  79. private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REGEX);
  80. //更新密码
  81. @Override
  82. public Result updatePassword(Password password) {
  83. String oldPassword = password.getOldPassword();
  84. String newPassword = password.getNewPassword();
  85. String againPassword = password.getAgainPassword();
  86. // 校验输入参数是否为空(账号、旧密码、新密码、确认密码均不可为空)
  87. if(oldPassword == null || newPassword == null || againPassword == null ||password.getAccount() == null) {
  88. return Result.error("输入不能为空");
  89. }
  90. // 检查两次输入的新密码是否一致
  91. if (!newPassword.equals(againPassword)) {
  92. return Result.error("两次输入的新密码不一致");
  93. }
  94. // 检查新密码是否符合复杂度要求
  95. if (!PASSWORD_PATTERN.matcher(newPassword).matches()) {
  96. return Result.error("新密码必须为8-16位数字、字母或符号组成,且至少包含其中两种");
  97. }
  98. if(adminMapper.getAdmin(password.getAccount())==null)
  99. {
  100. return Result.error("用户不存在");
  101. }
  102. // 校验新密码是否与旧密码相同(不允许相同)
  103. if (oldPassword.equals(newPassword)) {
  104. return Result.error("新密码与历史密码重复");
  105. }
  106. try {
  107. // 创建认证令牌并验证旧密码
  108. UsernamePasswordAuthenticationToken token =
  109. new UsernamePasswordAuthenticationToken(password.getAccount(), oldPassword);
  110. Authentication authentication = authenticationManager.authenticate(token);
  111. // 获取认证后的用户信息
  112. Admin admin = (Admin) authentication.getPrincipal();
  113. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  114. // 使用Spring管理的密码编码器(不要手动new)
  115. String encodedPassword = passwordEncoder.encode(newPassword);
  116. // 更新数据库中的密码
  117. admin.setPassword(encodedPassword);
  118. admin.setUpdateTime(new Date()); // 更新修改时间
  119. adminMapper.updatePassword(admin);
  120. return Result.success("密码修改成功");
  121. } catch (BadCredentialsException e) {
  122. // 旧密码验证失败
  123. return Result.error("原密码错误");
  124. }
  125. catch (Exception e) {
  126. return Result.error("密码更新失败");
  127. }
  128. }
  129. //重置密码
  130. @Override
  131. public Result resetPassword(Password password) {
  132. String newPassword = "123456";
  133. if(password.getAccount() == null) {
  134. return Result.error("账户未输入");
  135. }
  136. if(adminMapper.getAdmin(password.getAccount())==null)
  137. {
  138. return Result.error("账户查找不到");
  139. }
  140. try {
  141. // 获取认证后的用户信息
  142. Admin admin = adminMapper.getAdmin(password.getAccount());
  143. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  144. // 使用Spring管理的密码编码器(不要手动new)
  145. String encodedPassword = passwordEncoder.encode(newPassword);
  146. // 更新数据库中的密码
  147. admin.setPassword(encodedPassword);
  148. admin.setUpdateTime(new Date()); // 更新修改时间
  149. adminMapper.updatePassword(admin);
  150. return Result.success("密码重置成功");
  151. }
  152. catch (Exception e) {
  153. return Result.error("其他错误");
  154. }
  155. }
  156. public String getCurrentAdminAccount() {
  157. // 1. 从 Security 上下文获取认证信息
  158. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  159. // 2. 校验认证状态(未登录则抛出异常)
  160. if (authentication == null) {
  161. throw new RuntimeException("当前用户未登录");
  162. }
  163. // 3. 获取登录用户的主体信息(principal)
  164. Object principal = authentication.getPrincipal();
  165. // 4. 校验主体类型是否为 Admin(确保类型匹配)
  166. if (!(principal instanceof Admin)) {
  167. throw new RuntimeException("登录用户类型错误,不是 Admin");
  168. }
  169. // 5. 转换为 Admin 对象并获取 account 属性
  170. Admin currentAdmin = (Admin) principal;
  171. String account = currentAdmin.getAccount();
  172. // 6. 校验 account 不为空(根据业务需求可选)
  173. if (account == null || account.trim().isEmpty()) {
  174. throw new RuntimeException("当前 Admin 的 account 属性为空");
  175. }
  176. return account;
  177. }
  178. }