|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|