|
@@ -0,0 +1,125 @@
|
|
|
|
+package com.qmth.exam.reserve.service.impl;
|
|
|
|
+
|
|
|
|
+import com.qmth.boot.core.exception.StatusException;
|
|
|
|
+import com.qmth.boot.core.security.annotation.AuthorizationComponent;
|
|
|
|
+import com.qmth.boot.core.security.service.AuthorizationService;
|
|
|
|
+import com.qmth.boot.tools.signature.SignatureType;
|
|
|
|
+import com.qmth.boot.tools.uuid.FastUUID;
|
|
|
|
+import com.qmth.exam.reserve.bean.login.LoginReq;
|
|
|
|
+import com.qmth.exam.reserve.bean.login.LoginUser;
|
|
|
|
+import com.qmth.exam.reserve.bean.login.WechatLoginReq;
|
|
|
|
+import com.qmth.exam.reserve.cache.LoginSessionManager;
|
|
|
|
+import com.qmth.exam.reserve.entity.StudentEntity;
|
|
|
|
+import com.qmth.exam.reserve.entity.UserEntity;
|
|
|
|
+import com.qmth.exam.reserve.enums.Role;
|
|
|
|
+import com.qmth.exam.reserve.service.AuthService;
|
|
|
|
+import com.qmth.exam.reserve.service.StudentService;
|
|
|
|
+import com.qmth.exam.reserve.service.UserService;
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+@AuthorizationComponent
|
|
|
|
+public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthService {
|
|
|
|
+
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(AuthServiceImpl.class);
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserService userService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StudentService studentService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LoginUser userLogin(LoginReq req) {
|
|
|
|
+ if (StringUtils.isBlank(req.getAccount())) {
|
|
|
|
+ throw new StatusException("登录账号不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isBlank(req.getPassword())) {
|
|
|
|
+ throw new StatusException("登录密码不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("[USER_LOGIN] account:{}", req.getAccount());
|
|
|
|
+ UserEntity user = userService.findUserByLoginName(req.getOrgId(), req.getAccount());
|
|
|
|
+ if (user == null) {
|
|
|
|
+ throw new StatusException("登录用户不存在");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String encodePassword = DigestUtils.sha256Hex(req.getPassword()).toUpperCase();
|
|
|
|
+ if (!encodePassword.equals(user.getPassword())) {
|
|
|
|
+ throw new StatusException("登录账号或密码错误");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
|
+ loginUser.setId(user.getId());
|
|
|
|
+ loginUser.setOrgId(user.getOrgId());
|
|
|
|
+ loginUser.setAccount(user.getLoginName());
|
|
|
|
+ loginUser.setName(user.getName());
|
|
|
|
+ loginUser.setRole(user.getRole());
|
|
|
|
+
|
|
|
|
+ loginUser.setSessionId("U_" + user.getId());
|
|
|
|
+ loginUser.setToken(FastUUID.get());
|
|
|
|
+ LoginSessionManager.addLoginSession(loginUser);
|
|
|
|
+
|
|
|
|
+ log.info("[USER_LOGIN] account:{} {} {}", loginUser.getAccount(), loginUser.getName(), loginUser.getRole());
|
|
|
|
+ return loginUser;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LoginUser studentLogin(LoginReq req) {
|
|
|
|
+ if (StringUtils.isBlank(req.getAccount())) {
|
|
|
|
+ throw new StatusException("登录账号不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isBlank(req.getPassword())) {
|
|
|
|
+ throw new StatusException("登录密码不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("[STUDENT_LOGIN] account:{}", req.getAccount());
|
|
|
|
+ StudentEntity student = studentService.findStudentByStudentCode(req.getOrgId(), req.getAccount());
|
|
|
|
+ if (student == null) {
|
|
|
|
+ throw new StatusException("登录用户不存在");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String encodePassword = DigestUtils.sha256Hex(req.getPassword()).toUpperCase();
|
|
|
|
+ if (!encodePassword.equals(student.getPassword())) {
|
|
|
|
+ throw new StatusException("登录账号或密码错误");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
|
+ loginUser.setId(student.getId());
|
|
|
|
+ loginUser.setOrgId(student.getOrgId());
|
|
|
|
+ loginUser.setAccount(student.getStudentCode());
|
|
|
|
+ loginUser.setName(student.getName());
|
|
|
|
+ loginUser.setRole(Role.STUDENT);
|
|
|
|
+
|
|
|
|
+ loginUser.setSessionId("S_" + student.getId());
|
|
|
|
+ loginUser.setToken(FastUUID.get());
|
|
|
|
+ LoginSessionManager.addLoginSession(loginUser);
|
|
|
|
+
|
|
|
|
+ log.info("[STUDENT_LOGIN] account:{} {}", loginUser.getAccount(), loginUser.getName());
|
|
|
|
+ return loginUser;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LoginUser wechatLogin(WechatLoginReq req) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void logout(LoginUser loginUser) {
|
|
|
|
+ LoginSessionManager.removeLoginSession(loginUser.getSessionId());
|
|
|
|
+ log.warn("用户退出登录!account:{},{}", loginUser.getAccount(), loginUser.getName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LoginUser findByIdentity(String identity, SignatureType type, String path) {
|
|
|
|
+ return LoginSessionManager.getLoginSession(identity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|