|
@@ -0,0 +1,175 @@
|
|
|
|
+package com.qmth.themis.exam.api;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.qmth.themis.business.constant.SystemConstant;
|
|
|
|
+import com.qmth.themis.business.dto.AuthDto;
|
|
|
|
+import com.qmth.themis.business.entity.TBSession;
|
|
|
|
+import com.qmth.themis.business.entity.TEStudent;
|
|
|
|
+import com.qmth.themis.business.enums.MqEnum;
|
|
|
|
+import com.qmth.themis.business.enums.RoleEnum;
|
|
|
|
+import com.qmth.themis.business.enums.SystemOperationEnum;
|
|
|
|
+import com.qmth.themis.business.service.EhcacheService;
|
|
|
|
+import com.qmth.themis.business.service.TEStudentService;
|
|
|
|
+import com.qmth.themis.business.util.EhcacheUtil;
|
|
|
|
+import com.qmth.themis.business.util.JacksonUtil;
|
|
|
|
+import com.qmth.themis.business.util.RedisUtil;
|
|
|
|
+import com.qmth.themis.business.util.SessionUtil;
|
|
|
|
+import com.qmth.themis.common.contanst.Constants;
|
|
|
|
+import com.qmth.themis.common.enums.ExceptionResultEnum;
|
|
|
|
+import com.qmth.themis.common.enums.Platform;
|
|
|
|
+import com.qmth.themis.common.enums.Source;
|
|
|
|
+import com.qmth.themis.common.exception.BusinessException;
|
|
|
|
+import com.qmth.themis.common.signature.SignatureInfo;
|
|
|
|
+import com.qmth.themis.common.signature.SignatureType;
|
|
|
|
+import com.qmth.themis.common.util.AesUtil;
|
|
|
|
+import com.qmth.themis.common.util.Result;
|
|
|
|
+import com.qmth.themis.common.util.ResultUtil;
|
|
|
|
+import com.qmth.themis.exam.config.DictionaryConfig;
|
|
|
|
+import com.qmth.themis.exam.util.ServletUtil;
|
|
|
|
+import com.qmth.themis.mq.service.MqDtoService;
|
|
|
|
+import io.swagger.annotations.*;
|
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: 考生 前端控制器
|
|
|
|
+ * @Param:
|
|
|
|
+ * @return:
|
|
|
|
+ * @Author: wangliang
|
|
|
|
+ * @Date: 2020/6/25
|
|
|
|
+ */
|
|
|
|
+@Api(tags = "考生Controller")
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("/${prefix.url.exam}/student")
|
|
|
|
+public class TEStudentController {
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(TEStudentController.class);
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ TEStudentService teStudentService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ EhcacheService ehcacheService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ DictionaryConfig dictionaryConfig;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ MqDtoService mqDtoService;
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "学生登录接口")
|
|
|
|
+ @RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
|
|
+ @ApiResponses({@ApiResponse(code = 200, message = "学生信息", response = TEStudent.class)})
|
|
|
|
+ public Result login(@ApiParam(value = "学生信息", required = true) @RequestBody TEStudent teStudent) throws NoSuchAlgorithmException {
|
|
|
|
+ if (Objects.isNull(teStudent)) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.STUDENT_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ if (Objects.isNull(teStudent.getIdentity()) || Objects.equals(teStudent.getIdentity(), "")) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.LOGIN_NAME_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ if (Objects.isNull(teStudent.getPassword()) || Objects.equals(teStudent.getPassword(), "")) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.PASSWORD_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ String identity = teStudent.getIdentity();
|
|
|
|
+ String password = teStudent.getPassword();
|
|
|
|
+
|
|
|
|
+ QueryWrapper<TEStudent> wrapper = new QueryWrapper<>();
|
|
|
|
+ wrapper.lambda().eq(TEStudent::getIdentity, identity);
|
|
|
|
+ TEStudent user = teStudentService.getOne(wrapper);
|
|
|
|
+ //学生不存在
|
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.STUDENT_NO);
|
|
|
|
+ }
|
|
|
|
+ String loginPassword = AesUtil.decryptCs7(password, Constants.AES_RULE);
|
|
|
|
+ //密码错误
|
|
|
|
+ String aesPassword = AesUtil.decryptCs7(user.getPassword(), Constants.AES_RULE);
|
|
|
|
+ if (!Objects.equals(loginPassword, aesPassword)) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.PASSWORD_NO);
|
|
|
|
+ }
|
|
|
|
+ return userLoginCommon(user);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 学生登录公用
|
|
|
|
+ *
|
|
|
|
+ * @param teStudent
|
|
|
|
+ * @return
|
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
|
+ */
|
|
|
|
+ public Result userLoginCommon(TEStudent teStudent) throws NoSuchAlgorithmException {
|
|
|
|
+ //停用
|
|
|
|
+ if (teStudent.getEnable().intValue() == 0) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.STUDENT_ENABLE);
|
|
|
|
+ }
|
|
|
|
+ Platform platform = Platform.valueOf(ServletUtil.getRequestPlatform());
|
|
|
|
+ String deviceId = ServletUtil.getRequestDeviceId();
|
|
|
|
+ //添加用户鉴权缓存
|
|
|
|
+ AuthDto authDto = ehcacheService.addStudentCache(teStudent);
|
|
|
|
+ //生成token
|
|
|
|
+ String token = RandomStringUtils.randomAlphanumeric(32);
|
|
|
|
+ //添加用户缓存
|
|
|
|
+ redisUtil.setStudent(teStudent.getId(), teStudent);
|
|
|
|
+ //添加用户会话缓存
|
|
|
|
+ String sessionId = SessionUtil.digest(teStudent.getId(), authDto.getRoleCodes().toString().replaceAll(",", "&&").replaceAll(" ", ""), platform.getSource());
|
|
|
|
+
|
|
|
|
+ Date expire = SystemConstant.getExpireTime(platform);
|
|
|
|
+ TBSession tbSession = new TBSession(sessionId, String.valueOf(teStudent.getId()), authDto.getRoleCodes().toString(), platform.getSource(), platform.name(), deviceId, ServletUtil.getRequest().getLocalAddr(), token, expire);
|
|
|
|
+ redisUtil.setUserSession(sessionId, tbSession);
|
|
|
|
+ //mq发送消息start
|
|
|
|
+ mqDtoService.assembleSendOneWayMsg(dictionaryConfig.mqConfigDomain().getSessionTopic(), platform.getSource(), tbSession, MqEnum.SESSION.name(), tbSession.getId(), teStudent.getIdentity());
|
|
|
|
+ mqDtoService.assembleSendOneWayMsg(dictionaryConfig.mqConfigDomain().getUserLogTopic(), authDto.getRoleCodes().toString().contains(RoleEnum.STUDENT.name()) ? dictionaryConfig.mqConfigDomain().getUserLogTopicStudentTag() : dictionaryConfig.mqConfigDomain().getUserLogTopicUserTag(), SystemOperationEnum.LOGIN, MqEnum.EXAM_STUDENT_LOG.name(), teStudent.getId(), teStudent.getIdentity());
|
|
|
|
+ //mq发送消息end
|
|
|
|
+ //测试
|
|
|
|
+ String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+// map.put(SystemConstant.ACCESS_TOKEN, token);
|
|
|
|
+ map.put(SystemConstant.ACCESS_TOKEN, test);
|
|
|
|
+ map.put(SystemConstant.STUDENT_ACCOUNT, teStudent);
|
|
|
|
+ map.put(SystemConstant.SESSION_ID, sessionId);
|
|
|
|
+ return ResultUtil.ok(map);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "登出接口")
|
|
|
|
+ @RequestMapping(value = "/logout", method = RequestMethod.GET)
|
|
|
|
+ @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
|
|
|
|
+ public Result logout() throws NoSuchAlgorithmException {
|
|
|
|
+ TEStudent teStudent = (TEStudent) ServletUtil.getRequestStudentAccount();
|
|
|
|
+ TBSession tbSession = (TBSession) ServletUtil.getRequestSession();
|
|
|
|
+ AuthDto authDto = (AuthDto) EhcacheUtil.get(SystemConstant.STUDENT_CACHE, teStudent.getId());
|
|
|
|
+ if (Objects.isNull(tbSession)) {
|
|
|
|
+ throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
|
|
|
|
+ }
|
|
|
|
+ redisUtil.deleteUserSession(tbSession.getId());
|
|
|
|
+ //循环检查该用户下其他平台是否存在session,不存在则删除用户缓存和鉴权缓存
|
|
|
|
+ boolean delete = true;
|
|
|
|
+ for (Source s : Source.values()) {
|
|
|
|
+ String sessionId = SessionUtil.digest(teStudent.getId(), authDto.getRoleCodes().toString().replaceAll(",", "&&").replaceAll(" ", ""), s.name());
|
|
|
|
+ if (Objects.nonNull(redisUtil.getUserSession(sessionId))) {
|
|
|
|
+ delete = false;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (delete) {
|
|
|
|
+ redisUtil.deleteStudent(teStudent.getId());
|
|
|
|
+ ehcacheService.removeStudentCache(teStudent.getId());
|
|
|
|
+ }
|
|
|
|
+ //mq发送消息start
|
|
|
|
+ mqDtoService.assembleSendOneWayMsg(dictionaryConfig.mqConfigDomain().getUserLogTopic(), authDto.getRoleCodes().toString().contains(RoleEnum.STUDENT.name()) ? dictionaryConfig.mqConfigDomain().getUserLogTopicStudentTag() : dictionaryConfig.mqConfigDomain().getUserLogTopicUserTag(), SystemOperationEnum.LOGOUT, MqEnum.EXAM_STUDENT_LOG.name(), teStudent.getId(), teStudent.getIdentity());
|
|
|
|
+ //mq发送消息end
|
|
|
|
+ return ResultUtil.ok(JacksonUtil.parseJson(SystemConstant.SUCCESS));
|
|
|
|
+ }
|
|
|
|
+}
|