Sfoglia il codice sorgente

【CHG】修改验证码接口

zhaomn 2 anni fa
parent
commit
1066732527

+ 0 - 36
railway-admin/src/main/java/com/railway/web/controller/common/CaptchaController.java

@@ -101,40 +101,4 @@ public class CaptchaController extends BaseController {
     return ajax;
   }
 
-  /**
-   * 生成验证码
-   */
-  @ApiOperation(value = "yzm", notes = "yzm", response = ModelAndView.class)
-  @ApiImplicitParam(name = "type", value = "math 或 char", paramType = "query", dataType = "string")
-  @GetMapping("/yZm")
-  public AjaxResult getYzm(String type) {
-    AjaxResult ajax = AjaxResult.success();
-    boolean captchaOnOff = configService.selectCaptchaOnOff();
-    if (!captchaOnOff) {
-      return ajax;
-    }
-
-    // 保存验证码信息
-    String uuid = IdUtils.simpleUUID();
-    String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
-    String code = null;
-
-    if(null == type){
-      type = this.captchaType;
-    }
-    String math = "math", string = "char";
-    // 生成验证码
-    if (math.equals(type)) {
-      String capText = captchaProducerMath.createText();
-      code = capText.substring(capText.lastIndexOf("@") + 1);
-    } else if (string.equals(captchaType)) {
-      code = captchaProducer.createText();
-    }
-    log.debug("\n\"code\" : \"{}\",\n\t\"uuid\" : \"{}\"", code, uuid);
-    redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
-
-    ajax.put("uuid", uuid);
-    ajax.put("code", code);
-    return ajax;
-  }
 }

+ 85 - 0
railway-admin/src/main/java/com/railway/web/controller/common/YzmController.java

@@ -0,0 +1,85 @@
+package com.railway.web.controller.common;
+
+import com.google.code.kaptcha.Producer;
+import com.railway.common.constant.Constants;
+import com.railway.common.core.controller.BaseController;
+import com.railway.common.core.domain.AjaxResult;
+import com.railway.common.core.redis.RedisCache;
+import com.railway.common.utils.uuid.IdUtils;
+import com.railway.system.service.ISysConfigService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Resource;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.ModelAndView;
+
+/**
+ * 图片验证码(支持算术形式)
+ *
+ * @author railway
+ */
+@Slf4j
+@Api(tags = "测试认证 - 登录码")
+@RestController
+public class YzmController extends BaseController {
+
+  @Resource(name = "captchaProducer")
+  private Producer captchaProducer;
+
+  @Resource(name = "captchaProducerMath")
+  private Producer captchaProducerMath;
+
+  /** 验证码类型 */
+  @Value("${railway.captchaType}")
+  private String captchaType;
+
+  private final RedisCache redisCache;
+  private final ISysConfigService configService;
+
+  public YzmController(RedisCache redisCache, ISysConfigService configService) {
+    this.redisCache = redisCache;
+    this.configService = configService;
+  }
+
+  /**
+   * 生成验证码
+   */
+  @ApiOperation(value = "yzm", notes = "yzm", response = ModelAndView.class)
+  @ApiImplicitParam(name = "type", value = "math 或 char", paramType = "query", dataType = "string")
+  @GetMapping("/yZm")
+  public AjaxResult getYzm(String type) {
+    AjaxResult ajax = AjaxResult.success();
+    boolean captchaOnOff = configService.selectCaptchaOnOff();
+    if (!captchaOnOff) {
+      return ajax;
+    }
+
+    // 保存验证码信息
+    String uuid = IdUtils.simpleUUID();
+    String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
+    String code = null;
+
+    if(null == type){
+      type = this.captchaType;
+    }
+    String math = "math", string = "char";
+    // 生成验证码
+    if (math.equals(type)) {
+      String capText = captchaProducerMath.createText();
+      code = capText.substring(capText.lastIndexOf("@") + 1);
+    } else if (string.equals(captchaType)) {
+      code = captchaProducer.createText();
+    }
+    log.debug("\n\"code\" : \"{}\",\n\t\"uuid\" : \"{}\"", code, uuid);
+    redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
+
+    ajax.put("uuid", uuid);
+    ajax.put("code", code);
+    return ajax;
+  }
+}