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
214 lines
8.2 KiB
package com.example.demo.serviceImpl;
|
|
|
|
import com.example.demo.domain.entity.Admin;
|
|
import com.example.demo.domain.vo.Password;
|
|
import com.example.demo.domain.vo.Result;
|
|
import com.example.demo.mapper.AdminMapper;
|
|
import com.example.demo.service.AdminService;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.util.Date;
|
|
import java.util.regex.Pattern;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class AdminServiceImpl implements AdminService {
|
|
|
|
@Autowired
|
|
private AuthenticationManager authenticationManager;
|
|
private final AdminMapper adminMapper;
|
|
|
|
@Override
|
|
public Admin login(Admin admin) throws Exception {
|
|
String account = admin.getAccount();
|
|
String inputMachineId = admin.getMachineId();
|
|
|
|
if (StringUtils.isBlank(account)) {
|
|
throw new IllegalArgumentException("账号不能为空");
|
|
}
|
|
|
|
Admin adminInDB = adminMapper.getAdmin(account);
|
|
System.out.println("adminInDB:" + adminInDB);
|
|
System.out.println("adminInDB markets: " + adminInDB.getMarkets()); // 添加日志检查
|
|
if (adminInDB == null) {
|
|
throw new RuntimeException("无此精网号");
|
|
}
|
|
|
|
// 校验机器权限
|
|
if (!hasPermissionToMachine(adminInDB, inputMachineId)) {
|
|
throw new RuntimeException("你没有使用该机器的权限!");
|
|
}
|
|
|
|
try {
|
|
System.out.println("admin:" + account);
|
|
System.out.println("admin:" + admin.getPassword());
|
|
UsernamePasswordAuthenticationToken token =
|
|
new UsernamePasswordAuthenticationToken(account, admin.getPassword());
|
|
Authentication authentication = authenticationManager.authenticate(token);
|
|
SecurityContextHolder.getContext().setAuthentication(authentication); // 存储认证信息
|
|
Admin authenticatedAdmin = (Admin) authentication.getPrincipal();
|
|
System.out.println("authenticatedAdmin markets: " + authenticatedAdmin.getMarkets()); // 添加日志检查
|
|
return authenticatedAdmin;
|
|
|
|
} catch (Exception e) {
|
|
System.out.println("密码错误" + e.getMessage());
|
|
throw new RuntimeException("登录失败,请稍后再试", e);
|
|
}
|
|
}
|
|
|
|
//获取用户ID
|
|
@Override
|
|
public String getId(String account) {
|
|
return adminMapper.getAdmin(account).getId().toString();
|
|
}
|
|
|
|
@Override
|
|
public String getName(String account) {
|
|
return adminMapper.getName(account);
|
|
}
|
|
|
|
private boolean hasPermissionToMachine(Admin admin, String targetMachineId) {
|
|
if (targetMachineId == null || admin.getMachineId() == null) {
|
|
return false;
|
|
}
|
|
String[] machineIds = admin.getMachineId().split(",");
|
|
for (String id : machineIds) {
|
|
if (targetMachineId.equals(id)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static final String PASSWORD_REGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)(?!\\W+$).{8,16}$";
|
|
private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REGEX);
|
|
|
|
//更新密码
|
|
@Override
|
|
public Result updatePassword(Password password) {
|
|
|
|
String oldPassword = password.getOldPassword();
|
|
String newPassword = password.getNewPassword();
|
|
String againPassword = password.getAgainPassword();
|
|
// 校验输入参数是否为空(账号、旧密码、新密码、确认密码均不可为空)
|
|
if(oldPassword == null || newPassword == null || againPassword == null ||password.getAccount() == null) {
|
|
return Result.error("输入不能为空");
|
|
}
|
|
// 检查两次输入的新密码是否一致
|
|
if (!newPassword.equals(againPassword)) {
|
|
return Result.error("两次输入的新密码不一致");
|
|
}
|
|
// 检查新密码是否符合复杂度要求
|
|
if (!PASSWORD_PATTERN.matcher(newPassword).matches()) {
|
|
return Result.error("新密码必须为8-16位数字、字母或符号组成,且至少包含其中两种");
|
|
}
|
|
if(adminMapper.getAdmin(password.getAccount())==null)
|
|
{
|
|
return Result.error("用户不存在");
|
|
}
|
|
// 校验新密码是否与旧密码相同(不允许相同)
|
|
if (oldPassword.equals(newPassword)) {
|
|
return Result.error("新密码与历史密码重复");
|
|
}
|
|
try {
|
|
// 创建认证令牌并验证旧密码
|
|
UsernamePasswordAuthenticationToken token =
|
|
new UsernamePasswordAuthenticationToken(password.getAccount(), oldPassword);
|
|
Authentication authentication = authenticationManager.authenticate(token);
|
|
|
|
// 获取认证后的用户信息
|
|
Admin admin = (Admin) authentication.getPrincipal();
|
|
|
|
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
// 使用Spring管理的密码编码器(不要手动new)
|
|
String encodedPassword = passwordEncoder.encode(newPassword);
|
|
|
|
// 更新数据库中的密码
|
|
admin.setPassword(encodedPassword);
|
|
admin.setUpdateTime(new Date()); // 更新修改时间
|
|
adminMapper.updatePassword(admin);
|
|
|
|
return Result.success("密码修改成功");
|
|
|
|
} catch (BadCredentialsException e) {
|
|
// 旧密码验证失败
|
|
return Result.error("原密码错误");
|
|
}
|
|
catch (Exception e) {
|
|
return Result.error("密码更新失败");
|
|
}
|
|
}
|
|
|
|
|
|
//重置密码
|
|
@Override
|
|
public Result resetPassword(Password password) {
|
|
|
|
String newPassword = "123456";
|
|
if(password.getAccount() == null) {
|
|
return Result.error("账户未输入");
|
|
}
|
|
|
|
if(adminMapper.getAdmin(password.getAccount())==null)
|
|
{
|
|
return Result.error("账户查找不到");
|
|
}
|
|
try {
|
|
// 获取认证后的用户信息
|
|
Admin admin = adminMapper.getAdmin(password.getAccount());
|
|
|
|
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
// 使用Spring管理的密码编码器(不要手动new)
|
|
String encodedPassword = passwordEncoder.encode(newPassword);
|
|
|
|
// 更新数据库中的密码
|
|
admin.setPassword(encodedPassword);
|
|
admin.setUpdateTime(new Date()); // 更新修改时间
|
|
adminMapper.updatePassword(admin);
|
|
|
|
return Result.success("密码重置成功");
|
|
}
|
|
catch (Exception e) {
|
|
return Result.error("其他错误");
|
|
}
|
|
}
|
|
|
|
|
|
public String getCurrentAdminAccount() {
|
|
// 1. 从 Security 上下文获取认证信息
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
// 2. 校验认证状态(未登录则抛出异常)
|
|
if (authentication == null) {
|
|
throw new RuntimeException("当前用户未登录");
|
|
}
|
|
|
|
// 3. 获取登录用户的主体信息(principal)
|
|
Object principal = authentication.getPrincipal();
|
|
|
|
// 4. 校验主体类型是否为 Admin(确保类型匹配)
|
|
if (!(principal instanceof Admin)) {
|
|
throw new RuntimeException("登录用户类型错误,不是 Admin");
|
|
}
|
|
|
|
// 5. 转换为 Admin 对象并获取 account 属性
|
|
Admin currentAdmin = (Admin) principal;
|
|
String account = currentAdmin.getAccount();
|
|
|
|
// 6. 校验 account 不为空(根据业务需求可选)
|
|
if (account == null || account.trim().isEmpty()) {
|
|
throw new RuntimeException("当前 Admin 的 account 属性为空");
|
|
}
|
|
|
|
return account;
|
|
}
|
|
}
|