LoginService.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // 过滤按钮
  94. List<MenuTreeDTO> tempList = menuList.stream()
  95. .map(MenuTreeDTO::new).collect(Collectors.toList());
  96. // 菜单列表转树结构
  97. List<MenuTreeDTO> trees = TreeUtil.list2Tree(tempList, MenuTreeDTO::getId, MenuTreeDTO::getPid,
  98. MenuTreeDTO::getChildren, MenuTreeDTO::setChildren, 0, MenuTreeDTO::getSort);
  99. MenuTreeDTO root = new MenuTreeDTO(0, "全部菜单", trees);
  100. // 返回的数据结构构建
  101. UserInfoVO vo = new UserInfoVO(userId, username, roles, root);
  102. vo.setEmail(userInfo.getEmail());
  103. vo.setPhonenumber(userInfo.getPhonenumber());
  104. return vo;
  105. }
  106. /**
  107. * 修改密码
  108. *
  109. * @param command /
  110. */
  111. public void updatePwd(UserUpdatePwdCmd command) {
  112. ShiroUser loginUser = UserUtils.getLoginUser();
  113. Integer userId = loginUser.getUserId();
  114. String oldPwd = this.decryptPwd(command.getOldPassword());
  115. String newPwd = this.decryptPwd(command.getNewPassword());
  116. String confirmPwd = this.decryptPwd(command.getConfirmPassword());
  117. if (!StrUtil.isAllNotBlank(oldPwd, newPwd, confirmPwd)) {
  118. throw new APIException("前端密码加密错误");
  119. }
  120. if (Objects.equals(oldPwd, newPwd)) {
  121. throw new APIException("新密码不能和旧密码一样");
  122. }
  123. if (!Objects.equals(newPwd, confirmPwd)) {
  124. throw new APIException("确认密码和新密码不相等");
  125. }
  126. SysUserDO entity = command.toEntity();
  127. entity.setUserId(userId);
  128. entity.setPassword(newPwd);
  129. this.userAuthService.updatePwd(entity);
  130. }
  131. private String decryptPwd(String pwd) {
  132. DaqProperties.SecurityRsa securityRsa = this.properties.getSecurityRsa();
  133. String privateKey = securityRsa.getPrivateKey();
  134. try {
  135. return new RSA(privateKey, null).decryptStr(pwd, KeyType.PrivateKey);
  136. } catch (Exception e) {
  137. this.logger.error("密码rsa解密错误\n{}", ExceptionUtil.stacktraceToString(e));
  138. }
  139. return null;
  140. }
  141. }