|
@@ -1,13 +1,24 @@
|
|
|
package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
|
|
|
import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.UserType;
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
import cn.com.qmth.examcloud.commons.util.UUID;
|
|
|
import cn.com.qmth.examcloud.core.basic.base.util.VerifyCode;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.AuthService;
|
|
|
import cn.com.qmth.examcloud.core.basic.service.VerifyCodeService;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.LoginInfo;
|
|
|
import cn.com.qmth.examcloud.core.basic.service.bean.VerifyCodeLoginInfo;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.bean.OnlineStudentReport;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.bean.OnlineUserReport;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.util.ReportsUtil;
|
|
|
+import cn.com.qmth.examcloud.support.cache.bean.VerifyCodeCacheBean;
|
|
|
+import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
/**
|
|
@@ -16,7 +27,19 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
public class VerifyCodeServiceImpl implements VerifyCodeService {
|
|
|
|
|
|
- private static final ExamCloudLog log = ExamCloudLogFactory.getLog(VerifyCodeServiceImpl.class);
|
|
|
+ private static final ExamCloudLog log = ExamCloudLogFactory.getLog("INTERFACE_LOGGER");
|
|
|
+
|
|
|
+ private static final String CACHE_KEY_VERIFY_CODE = "B_VERIFY_CODE:%s_%s";// “验证码”缓存KEY
|
|
|
+
|
|
|
+ private static final String CACHE_KEY_VERIFY_CODE_LIMIT = "B_VERIFY_CODE_LIMIT:%s_%s";// “验证码限流”缓存KEY
|
|
|
+
|
|
|
+ private static final String CACHE_KEY_VERIFY_CODE_RESOURCE = "B_VERIFY_CODE_RESOURCE:%s";// “验证码资源标识”缓存KEY
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthService authService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisClient redisClient;
|
|
|
|
|
|
/**
|
|
|
* 验证码生成接口
|
|
@@ -25,15 +48,27 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
|
|
|
*/
|
|
|
@Override
|
|
|
public String verifyCodeGenerate(Long rootOrgId, String accountValue) {
|
|
|
- VerifyCode.Result result = VerifyCode.generateVerifyCode();
|
|
|
- log.info(String.format("accountValue = %s, generateVerifyCode = %s", accountValue, result.getResult()));
|
|
|
+ final String cacheKeyVerifyCodeLimit = String.format(CACHE_KEY_VERIFY_CODE_LIMIT, rootOrgId, accountValue);
|
|
|
+ Boolean limit = redisClient.get(cacheKeyVerifyCodeLimit, Boolean.class);
|
|
|
+ if (limit != null) {
|
|
|
+ throw new StatusException("500", "操作太频繁,请稍后重试!");
|
|
|
+ }
|
|
|
+ redisClient.set(cacheKeyVerifyCodeLimit, true, 3);// N秒
|
|
|
|
|
|
+ VerifyCode.Result result = VerifyCode.generateVerifyCode();
|
|
|
byte[] bytes = VerifyCode.imageToBytes(result.getImage());
|
|
|
String base64 = Base64.encodeBase64String(bytes);
|
|
|
+ String uuid = UUID.randomUUID();
|
|
|
+
|
|
|
+ log.info(String.format("accountValue = %s, uuid = %s, verifyCode = %s", accountValue, uuid, result.getResult()));
|
|
|
+ VerifyCodeCacheBean cacheBean = new VerifyCodeCacheBean();
|
|
|
+ cacheBean.setUuid(uuid);
|
|
|
+ cacheBean.setVerifyCode(result.getResult());
|
|
|
|
|
|
- //todo
|
|
|
+ final String cacheKeyVerifyCode = String.format(CACHE_KEY_VERIFY_CODE, rootOrgId, accountValue);
|
|
|
+ redisClient.set(cacheKeyVerifyCode, cacheBean, 60 * 3);// N秒
|
|
|
|
|
|
- return UUID.randomUUID() + base64;
|
|
|
+ return uuid + base64;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -43,11 +78,41 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
|
|
|
*/
|
|
|
@Override
|
|
|
public User verifyCodeLogin(VerifyCodeLoginInfo info) {
|
|
|
+ if (info.getRootOrgId() == null) {
|
|
|
+ throw new StatusException("400", "顶级机构ID不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(info.getAccountType())) {
|
|
|
+ throw new StatusException("400", "登陆账号类型不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(info.getAccountValue())) {
|
|
|
+ throw new StatusException("400", "登陆账号不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(info.getPassword())) {
|
|
|
+ throw new StatusException("400", "登陆密码不能为空");
|
|
|
+ }
|
|
|
+ if (info.getVerifyCode() == null) {
|
|
|
+ throw new StatusException("400", "验证码不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // todo
|
|
|
+
|
|
|
+ LoginInfo loginInfo = new LoginInfo();
|
|
|
+ loginInfo.setRootOrgId(info.getRootOrgId());
|
|
|
+ loginInfo.setAccountType(info.getAccountType());
|
|
|
+ loginInfo.setAccountValue(info.getAccountValue());
|
|
|
+ loginInfo.setPassword(info.getPassword());
|
|
|
+ loginInfo.setClientIp(info.getClientIp());
|
|
|
+ User user = authService.login(loginInfo);
|
|
|
|
|
|
- log.info(info.getAccountValue() + " - " + info.getVerifyCode());
|
|
|
- //todo
|
|
|
+ if (UserType.STUDENT.equals(user.getUserType())) {
|
|
|
+ // 在线学生登录打点
|
|
|
+ ReportsUtil.report(new OnlineStudentReport(user.getRootOrgId(), user.getUserId()));
|
|
|
+ } else if (UserType.COMMON.equals(user.getUserType())) {
|
|
|
+ // 在线用户登录打点
|
|
|
+ ReportsUtil.report(new OnlineUserReport(user.getRootOrgId(), user.getUserId()));
|
|
|
+ }
|
|
|
|
|
|
- return null;
|
|
|
+ return user;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -55,8 +120,8 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
|
|
|
*/
|
|
|
@Override
|
|
|
public void verifyCodeResource(String uuid) {
|
|
|
- log.info(uuid);
|
|
|
- //todo
|
|
|
+ final String cacheKeyVerifyCodeResource = String.format(CACHE_KEY_VERIFY_CODE_RESOURCE, uuid);
|
|
|
+ redisClient.set(cacheKeyVerifyCodeResource, true, 60 * 3);// N秒
|
|
|
}
|
|
|
|
|
|
}
|