|
@@ -1,10 +1,11 @@
|
|
|
package com.ym.mec.teacher.controller;
|
|
|
|
|
|
-import com.google.code.kaptcha.Constants;
|
|
|
-import com.google.code.kaptcha.Producer;
|
|
|
-import com.google.code.kaptcha.servlet.KaptchaServlet;
|
|
|
+import com.wf.captcha.SpecCaptcha;
|
|
|
+import com.wf.captcha.utils.CaptchaUtil;
|
|
|
+import com.ym.mec.biz.dal.enums.MessageTypeEnum;
|
|
|
import com.ym.mec.biz.service.SmsCodeService;
|
|
|
import com.ym.mec.common.controller.BaseController;
|
|
|
+import com.ym.mec.common.exception.BizException;
|
|
|
import com.ym.mec.common.security.SecurityConstants;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
@@ -13,15 +14,11 @@ import io.swagger.annotations.ApiOperation;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
-
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@RestController
|
|
@@ -32,8 +29,6 @@ public class SmsCodeController extends BaseController {
|
|
|
@Autowired
|
|
|
private SmsCodeService smsCodeService;
|
|
|
@Autowired
|
|
|
- private Producer captchaProducer;
|
|
|
- @Autowired
|
|
|
private RedisTemplate<String,String> redisTemplate;
|
|
|
|
|
|
@ApiOperation(value = "发送登录短信验证码")
|
|
@@ -66,7 +61,7 @@ public class SmsCodeController extends BaseController {
|
|
|
if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
|
|
|
return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
|
|
|
}
|
|
|
- String redisKey = Constants.KAPTCHA_SESSION_KEY + phone;
|
|
|
+ String redisKey = MessageTypeEnum.KAPTCHA_SESSION_KEY + phone;
|
|
|
if(redisTemplate.hasKey(redisKey)){
|
|
|
if(StringUtils.equalsIgnoreCase(redisTemplate.opsForValue().get(redisKey),code)){
|
|
|
return succeed();
|
|
@@ -75,37 +70,15 @@ public class SmsCodeController extends BaseController {
|
|
|
return failed(SecurityConstants.VERIFY_FAILURE);
|
|
|
}
|
|
|
|
|
|
- @GetMapping(value = "/getLoginImage")
|
|
|
+ @RequestMapping("/getLoginImage")
|
|
|
@ApiOperation("获取登录图片验证码")
|
|
|
@ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String")
|
|
|
- public void getKaptchaImage(HttpServletResponse response,String phone) throws Exception {
|
|
|
+ public void getLoginImage(HttpServletRequest request, HttpServletResponse response, String phone) throws Exception {
|
|
|
if(StringUtils.isEmpty(phone)){
|
|
|
- return;
|
|
|
- }
|
|
|
- response.setDateHeader("Expires", 0);
|
|
|
-
|
|
|
-
|
|
|
- response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
|
|
|
-
|
|
|
- response.addHeader("Cache-Control", "post-check=0, pre-check=0");
|
|
|
-
|
|
|
- response.setHeader("Pragma", "no-cache");
|
|
|
-
|
|
|
- response.setContentType("image/jpeg");
|
|
|
-
|
|
|
- String capText = captchaProducer.createText();
|
|
|
-
|
|
|
- redisTemplate.opsForValue().set(Constants.KAPTCHA_SESSION_KEY + phone,capText,3, TimeUnit.MINUTES);
|
|
|
-
|
|
|
- BufferedImage bi = captchaProducer.createImage(capText);
|
|
|
- KaptchaServlet kaptchaServlet = new KaptchaServlet();
|
|
|
- kaptchaServlet.init();
|
|
|
- ServletOutputStream out = response.getOutputStream();
|
|
|
- try {
|
|
|
- ImageIO.write(bi, "jpg", out);
|
|
|
- out.flush();
|
|
|
- } finally {
|
|
|
- out.close();
|
|
|
+ throw new BizException("请输入手机号");
|
|
|
}
|
|
|
+ SpecCaptcha specCaptcha = new SpecCaptcha(125, 45, 5);
|
|
|
+ redisTemplate.opsForValue().set(MessageTypeEnum.KAPTCHA_SESSION_KEY + phone,specCaptcha.text(),3, TimeUnit.MINUTES);
|
|
|
+ CaptchaUtil.out(specCaptcha, request, response);
|
|
|
}
|
|
|
}
|