LoginService.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.simuwang.manage.service;
  2. import cn.hutool.core.exceptions.ExceptionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.crypto.asymmetric.KeyType;
  5. import cn.hutool.crypto.asymmetric.RSA;
  6. import com.simuwang.base.common.exception.APIException;
  7. import com.simuwang.base.common.util.TreeUtil;
  8. import com.simuwang.base.components.UserAuthService;
  9. import com.simuwang.base.config.DaqProperties;
  10. import com.simuwang.base.pojo.dos.sys.SysMenuDO;
  11. import com.simuwang.base.pojo.dos.sys.SysRoleDO;
  12. import com.simuwang.base.pojo.dos.sys.SysUserDO;
  13. import com.simuwang.base.pojo.dto.sys.UserUpdatePwdCmd;
  14. import com.simuwang.manage.dto.MenuTreeDTO;
  15. import com.simuwang.manage.dto.UserInfoVO;
  16. import com.simuwang.manage.dto.UserRoleDTO;
  17. import com.simuwang.shiro.core.ShiroUser;
  18. import com.simuwang.shiro.utils.UserUtils;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.stereotype.Service;
  22. import java.util.List;
  23. import java.util.Objects;
  24. import java.util.stream.Collectors;
  25. /**
  26. * @author wangzaijun
  27. * @date 2024/9/12 20:28
  28. * @description 系统管理服务
  29. */
  30. @Service
  31. public class LoginService {
  32. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  33. private final DaqProperties properties;
  34. private final UserAuthService userAuthService;
  35. public LoginService(DaqProperties properties, UserAuthService userAuthService) {
  36. this.properties = properties;
  37. this.userAuthService = userAuthService;
  38. }
  39. public String getRsaPublicKey() {
  40. return this.properties.getSecurityRsa().getPublicKey();
  41. }
  42. /**
  43. * 获取当前用户的权限角色信息
  44. *
  45. * @return /
  46. */
  47. public UserInfoVO getUserInfo() {
  48. ShiroUser shiroUser = UserUtils.getLoginUser();
  49. Integer userId = shiroUser.getUserId();
  50. String username = shiroUser.getUsername();
  51. SysUserDO userInfo = this.userAuthService.getUserInfoByUsername(username);
  52. // 用户角色信息转换
  53. List<SysRoleDO> roleList = this.userAuthService.listUserRoleByUserId(userId);
  54. List<UserRoleDTO> roles = roleList.stream()
  55. .map(e -> new UserRoleDTO(e.getRoleId(), e.getRoleName(), e.getRoleKey()))
  56. .distinct().collect(Collectors.toList());
  57. // 用户权限处理
  58. List<SysMenuDO> menuList = this.userAuthService.listUserMenuByUserId(userId);
  59. List<MenuTreeDTO> tempList = menuList.stream().map(MenuTreeDTO::new).collect(Collectors.toList());
  60. // 菜单列表转树结构
  61. List<MenuTreeDTO> trees = TreeUtil.list2Tree(tempList, MenuTreeDTO::getId, MenuTreeDTO::getPid,
  62. MenuTreeDTO::getChildren, MenuTreeDTO::setChildren, 0, MenuTreeDTO::getSort);
  63. MenuTreeDTO root = new MenuTreeDTO(0, "全部菜单", trees);
  64. // 返回的数据结构构建
  65. UserInfoVO vo = new UserInfoVO(userId, username, roles, root);
  66. vo.setEmail(userInfo.getEmail());
  67. vo.setPhonenumber(userInfo.getPhonenumber());
  68. return vo;
  69. }
  70. /**
  71. * 修改密码
  72. *
  73. * @param command /
  74. */
  75. public void updatePwd(UserUpdatePwdCmd command) {
  76. ShiroUser loginUser = UserUtils.getLoginUser();
  77. Integer userId = loginUser.getUserId();
  78. String oldPwd = this.decryptPwd(command.getOldPassword());
  79. String newPwd = this.decryptPwd(command.getNewPassword());
  80. String confirmPwd = this.decryptPwd(command.getConfirmPassword());
  81. if (!StrUtil.isAllNotBlank(oldPwd, newPwd, confirmPwd)) {
  82. throw new APIException("前端密码加密错误");
  83. }
  84. if (Objects.equals(oldPwd, newPwd)) {
  85. throw new APIException("新密码不能和旧密码一样");
  86. }
  87. if (!Objects.equals(newPwd, confirmPwd)) {
  88. throw new APIException("确认密码和新密码不相等");
  89. }
  90. SysUserDO entity = command.toEntity();
  91. entity.setUserId(userId);
  92. entity.setPassword(newPwd);
  93. this.userAuthService.updatePwd(entity);
  94. }
  95. private String decryptPwd(String pwd) {
  96. DaqProperties.SecurityRsa securityRsa = this.properties.getSecurityRsa();
  97. String privateKey = securityRsa.getPrivateKey();
  98. try {
  99. return new RSA(privateKey, null).decryptStr(pwd, KeyType.PrivateKey);
  100. } catch (Exception e) {
  101. this.logger.error("密码rsa解密错误\n{}", ExceptionUtil.stacktraceToString(e));
  102. }
  103. return null;
  104. }
  105. }