LoginService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.ServletUtils;
  8. import com.simuwang.base.common.util.TreeUtil;
  9. import com.simuwang.base.components.UserAuthService;
  10. import com.simuwang.base.config.DaqProperties;
  11. import com.simuwang.base.pojo.dos.sys.SysMenuDO;
  12. import com.simuwang.base.pojo.dos.sys.SysRoleDO;
  13. import com.simuwang.base.pojo.dos.sys.SysUserDO;
  14. import com.simuwang.base.pojo.dto.sys.UserUpdatePwdCmd;
  15. import com.simuwang.manage.dto.LoginUser;
  16. import com.simuwang.manage.dto.MenuTreeDTO;
  17. import com.simuwang.manage.dto.UserInfoVO;
  18. import com.simuwang.manage.dto.UserRoleDTO;
  19. import com.simuwang.shiro.core.ShiroToken;
  20. import com.simuwang.shiro.core.ShiroUser;
  21. import com.simuwang.shiro.core.jwt.JwtContext;
  22. import com.simuwang.shiro.utils.UserUtils;
  23. import org.apache.shiro.SecurityUtils;
  24. import org.apache.shiro.subject.Subject;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.stereotype.Service;
  28. import java.util.List;
  29. import java.util.Objects;
  30. import java.util.stream.Collectors;
  31. /**
  32. * @author wangzaijun
  33. * @date 2024/9/12 20:28
  34. * @description 系统管理服务
  35. */
  36. @Service
  37. public class LoginService {
  38. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  39. private final JwtContext jwtContext;
  40. private final DaqProperties properties;
  41. private final UserAuthService userAuthService;
  42. public LoginService(JwtContext jwtContext, DaqProperties properties, UserAuthService userAuthService) {
  43. this.jwtContext = jwtContext;
  44. this.properties = properties;
  45. this.userAuthService = userAuthService;
  46. }
  47. public String getRsaPublicKey() {
  48. return this.properties.getSecurityRsa().getPublicKey();
  49. }
  50. /**
  51. * 登录业务
  52. *
  53. * @param loginUser 登录用户密码
  54. * @return /
  55. */
  56. public String login(LoginUser loginUser) {
  57. ShiroToken shiroToken = new ShiroToken(loginUser.getUsername(), loginUser.getPassword());
  58. Subject subject = SecurityUtils.getSubject();
  59. subject.login(shiroToken);
  60. String requestIp = ServletUtils.getIpAddr();
  61. return this.jwtContext.generateToken(loginUser.getUsername(), requestIp);
  62. }
  63. /**
  64. * 登出
  65. *
  66. * @return /
  67. */
  68. public boolean logout() {
  69. Subject subject = SecurityUtils.getSubject();
  70. ShiroUser shiroUser = UserUtils.getLoginUser();
  71. String requestIp = ServletUtils.getIpAddr();
  72. this.jwtContext.cleanUserCache(shiroUser.getUsername(), requestIp);
  73. subject.logout();
  74. return true;
  75. }
  76. /**
  77. * 获取当前用户的权限角色信息
  78. *
  79. * @return /
  80. */
  81. public UserInfoVO getUserInfo() {
  82. ShiroUser shiroUser = UserUtils.getLoginUser();
  83. Integer userId = shiroUser.getUserId();
  84. String username = shiroUser.getUsername();
  85. SysUserDO userInfo = this.userAuthService.getUserInfoByUsername(username);
  86. // 用户角色信息转换
  87. List<SysRoleDO> roleList = this.userAuthService.listUserRoleByUserId(userId);
  88. List<UserRoleDTO> roles = roleList.stream()
  89. .map(e -> new UserRoleDTO(e.getRoleId(), e.getRoleName(), e.getRoleKey()))
  90. .distinct().collect(Collectors.toList());
  91. // 用户权限处理
  92. List<SysMenuDO> menuList = this.userAuthService.listUserMenuByUserId(userId, 1);
  93. // 返回用户的权限菜单ID
  94. List<Integer> menuIds = menuList.stream().map(e -> e.getMenuId()).collect(Collectors.toList());
  95. // 全部菜单列表转树结构
  96. List<SysMenuDO> allMenuList = this.userAuthService.listUserMenuByUserId(0, 1);
  97. List<MenuTreeDTO> allTempList = allMenuList.stream()
  98. .map(MenuTreeDTO::new).collect(Collectors.toList());
  99. List<MenuTreeDTO> trees = TreeUtil.list2Tree(allTempList, MenuTreeDTO::getId, MenuTreeDTO::getPid,
  100. MenuTreeDTO::getChildren, MenuTreeDTO::setChildren, 0, MenuTreeDTO::getSort);
  101. MenuTreeDTO root = new MenuTreeDTO(0, "全部菜单", trees);
  102. UserInfoVO vo = new UserInfoVO(userId, username, roles, root,menuIds);
  103. vo.setEmail(userInfo.getEmail());
  104. vo.setPhonenumber(userInfo.getPhonenumber());
  105. return vo;
  106. }
  107. /**
  108. * 修改密码
  109. *
  110. * @param command /
  111. */
  112. public void updatePwd(UserUpdatePwdCmd command) {
  113. ShiroUser loginUser = UserUtils.getLoginUser();
  114. Integer userId = loginUser.getUserId();
  115. String oldPwd = this.decryptPwd(command.getOldPassword());
  116. String newPwd = this.decryptPwd(command.getNewPassword());
  117. String confirmPwd = this.decryptPwd(command.getConfirmPassword());
  118. if (!StrUtil.isAllNotBlank(oldPwd, newPwd, confirmPwd)) {
  119. throw new APIException("前端密码加密错误");
  120. }
  121. if (Objects.equals(oldPwd, newPwd)) {
  122. throw new APIException("新密码不能和旧密码一样");
  123. }
  124. if (!Objects.equals(newPwd, confirmPwd)) {
  125. throw new APIException("确认密码和新密码不相等");
  126. }
  127. SysUserDO entity = command.toEntity();
  128. entity.setUserId(userId);
  129. entity.setPassword(newPwd);
  130. this.userAuthService.updatePwd(entity);
  131. }
  132. private String decryptPwd(String pwd) {
  133. DaqProperties.SecurityRsa securityRsa = this.properties.getSecurityRsa();
  134. String privateKey = securityRsa.getPrivateKey();
  135. try {
  136. return new RSA(privateKey, null).decryptStr(pwd, KeyType.PrivateKey);
  137. } catch (Exception e) {
  138. this.logger.error("密码rsa解密错误\n{}", ExceptionUtil.stacktraceToString(e));
  139. }
  140. return null;
  141. }
  142. }