AuthServiceImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.qmth.exam.reserve.service.impl;
  2. import com.qmth.boot.core.exception.StatusException;
  3. import com.qmth.boot.core.security.annotation.AuthorizationComponent;
  4. import com.qmth.boot.core.security.service.AuthorizationService;
  5. import com.qmth.boot.tools.signature.SignatureType;
  6. import com.qmth.boot.tools.uuid.FastUUID;
  7. import com.qmth.exam.reserve.bean.login.LoginReq;
  8. import com.qmth.exam.reserve.bean.login.LoginUser;
  9. import com.qmth.exam.reserve.bean.login.WechatLoginReq;
  10. import com.qmth.exam.reserve.cache.CacheConstants;
  11. import com.qmth.exam.reserve.cache.LoginSessionManager;
  12. import com.qmth.exam.reserve.entity.StudentEntity;
  13. import com.qmth.exam.reserve.entity.UserEntity;
  14. import com.qmth.exam.reserve.enums.Role;
  15. import com.qmth.exam.reserve.service.AuthService;
  16. import com.qmth.exam.reserve.service.StudentService;
  17. import com.qmth.exam.reserve.service.UserService;
  18. import org.apache.commons.codec.digest.DigestUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. @Service
  25. @AuthorizationComponent
  26. public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthService {
  27. private final static Logger log = LoggerFactory.getLogger(AuthServiceImpl.class);
  28. @Autowired
  29. private LoginSessionManager loginSessionManager;
  30. @Autowired
  31. private UserService userService;
  32. @Autowired
  33. private StudentService studentService;
  34. @Override
  35. public LoginUser userLogin(LoginReq req) {
  36. if (StringUtils.isBlank(req.getAccount())) {
  37. throw new StatusException("登录账号不能为空");
  38. }
  39. if (StringUtils.isBlank(req.getPassword())) {
  40. throw new StatusException("登录密码不能为空");
  41. }
  42. log.debug("[USER_LOGIN] verifying, account:{}", req.getAccount());
  43. UserEntity user = userService.findUserByLoginName(req.getOrgId(), req.getAccount());
  44. if (user == null) {
  45. throw new StatusException("登录用户不存在");
  46. }
  47. String encodePassword = DigestUtils.sha256Hex(req.getPassword()).toUpperCase();
  48. if (!encodePassword.equals(user.getPassword())) {
  49. throw new StatusException("登录账号或密码错误");
  50. }
  51. LoginUser loginUser = new LoginUser();
  52. loginUser.setId(user.getId());
  53. loginUser.setOrgId(user.getOrgId());
  54. loginUser.setCategoryId(user.getCategoryId());
  55. loginUser.setAccount(user.getLoginName());
  56. loginUser.setName(user.getName());
  57. loginUser.setRole(user.getRole());
  58. loginUser.setSessionId(CacheConstants.CACHE_USER_LOGIN + user.getId());
  59. loginUser.setToken(FastUUID.get());
  60. loginSessionManager.addLoginSession(loginUser);
  61. log.info("[USER_LOGIN] success! account:{} {} {}", loginUser.getAccount(), loginUser.getName(), loginUser.getRole());
  62. return loginUser;
  63. }
  64. @Override
  65. public LoginUser studentLogin(LoginReq req) {
  66. if (StringUtils.isBlank(req.getAccount())) {
  67. throw new StatusException("登录账号不能为空");
  68. }
  69. if (StringUtils.isBlank(req.getPassword())) {
  70. throw new StatusException("登录密码不能为空");
  71. }
  72. log.debug("[STUDENT_LOGIN] verifying, account:{}", req.getAccount());
  73. StudentEntity student = studentService.findByStudentCode(req.getOrgId(), req.getAccount());
  74. if (student == null) {
  75. throw new StatusException("登录用户不存在");
  76. }
  77. String encodePassword = DigestUtils.sha256Hex(req.getPassword()).toUpperCase();
  78. if (!encodePassword.equals(student.getPassword())) {
  79. throw new StatusException("登录账号或密码错误");
  80. }
  81. LoginUser loginUser = new LoginUser();
  82. loginUser.setId(student.getId());
  83. loginUser.setOrgId(student.getOrgId());
  84. loginUser.setCategoryId(student.getCategoryId());
  85. loginUser.setAccount(student.getStudentCode());
  86. loginUser.setName(student.getName());
  87. loginUser.setRole(Role.STUDENT);
  88. loginUser.setOpenId(student.getOpenId());
  89. loginUser.setSessionId(CacheConstants.CACHE_STUDENT_LOGIN + student.getId());
  90. loginUser.setToken(FastUUID.get());
  91. loginSessionManager.addLoginSession(loginUser);
  92. log.info("[STUDENT_LOGIN] success! account:{} {}", loginUser.getAccount(), loginUser.getName());
  93. return loginUser;
  94. }
  95. @Override
  96. public LoginUser wechatLogin(WechatLoginReq req) {
  97. if (StringUtils.isBlank(req.getOpenId())) {
  98. throw new StatusException("微信OID不能为空");
  99. }
  100. log.debug("[WECHAT_LOGIN] verifying, openId:{} uid:{}", req.getOpenId(), req.getUid());
  101. StudentEntity student = studentService.findByOpenIdAndUid(req.getOpenId(), req.getUid());
  102. if (student == null) {
  103. throw new StatusException("登录用户不存在");
  104. }
  105. LoginUser loginUser = new LoginUser();
  106. loginUser.setId(student.getId());
  107. loginUser.setOrgId(student.getOrgId());
  108. loginUser.setCategoryId(student.getCategoryId());
  109. loginUser.setAccount(student.getStudentCode());
  110. loginUser.setName(student.getName());
  111. loginUser.setRole(Role.STUDENT);
  112. loginUser.setOpenId(student.getOpenId());
  113. loginUser.setSessionId(CacheConstants.CACHE_STUDENT_LOGIN + student.getId());
  114. loginUser.setToken(FastUUID.get());
  115. loginSessionManager.addLoginSession(loginUser);
  116. log.info("[WECHAT_LOGIN] success! account:{} {}", loginUser.getAccount(), loginUser.getName());
  117. return loginUser;
  118. }
  119. @Override
  120. public void logout(LoginUser loginUser) {
  121. loginSessionManager.removeLoginSession(loginUser.getSessionId());
  122. log.warn("[LOGOUT] account:{} {}", loginUser.getAccount(), loginUser.getName());
  123. }
  124. @Override
  125. public LoginUser findByIdentity(String identity, SignatureType type, String path) {
  126. return loginSessionManager.getLoginSession(identity);
  127. }
  128. }