deason 5 年之前
父節點
當前提交
e142219cc8

+ 12 - 1
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/VerifyCodeController.java

@@ -7,9 +7,12 @@ import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.web.support.Naked;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * 验证码相关接口
  */
@@ -31,7 +34,15 @@ public class VerifyCodeController extends ControllerSupport {
     @Naked
     @ApiOperation(value = "验证码登录接口")
     @PostMapping(value = "/verifyCode/login")
-    public User login(@RequestBody VerifyCodeLoginInfo info) {
+    public User login(@RequestBody VerifyCodeLoginInfo info, HttpServletRequest request) {
+        String ip = request.getHeader("x-forwarded-for");
+        if (StringUtils.isBlank(ip)) {
+            ip = request.getHeader("x-real-ip");
+        }
+        if (StringUtils.isNotBlank(ip)) {
+            info.setClientIp(ip);
+        }
+
         return verifyCodeService.verifyCodeLogin(info);
     }
 

+ 11 - 12
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/bean/VerifyCodeLoginInfo.java

@@ -19,15 +19,15 @@ public class VerifyCodeLoginInfo implements JsonSerializable {
     @ApiModelProperty("登陆账号")
     private String accountValue;
 
-    @ApiModelProperty("登陆账号密码")
+    @ApiModelProperty("登陆密码")
     private String password;
 
-    @ApiModelProperty("验证码编号")
-    private String uuid;
-
     @ApiModelProperty("验证码结果")
     private Integer verifyCode;
 
+    @ApiModelProperty(value = "客户端IP", hidden = true)
+    private String clientIp;
+
     public Long getRootOrgId() {
         return rootOrgId;
     }
@@ -60,14 +60,6 @@ public class VerifyCodeLoginInfo implements JsonSerializable {
         this.password = password;
     }
 
-    public String getUuid() {
-        return uuid;
-    }
-
-    public void setUuid(String uuid) {
-        this.uuid = uuid;
-    }
-
     public Integer getVerifyCode() {
         return verifyCode;
     }
@@ -76,4 +68,11 @@ public class VerifyCodeLoginInfo implements JsonSerializable {
         this.verifyCode = verifyCode;
     }
 
+    public String getClientIp() {
+        return clientIp;
+    }
+
+    public void setClientIp(String clientIp) {
+        this.clientIp = clientIp;
+    }
 }

+ 75 - 10
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/VerifyCodeServiceImpl.java

@@ -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秒
     }
 
 }