Browse Source

新增获取验证码接口/auth/login/captcha

master
yinzuomei 6 years ago
parent
commit
87e7545970
  1. 40
      epmet-auth/src/main/java/com/epmet/controller/LoginController.java
  2. 6
      epmet-auth/src/main/java/com/epmet/redis/CaptchaRedis.java
  3. 5
      epmet-auth/src/main/java/com/epmet/service/impl/CaptchaServiceImpl.java

40
epmet-auth/src/main/java/com/epmet/controller/LoginController.java

@ -3,26 +3,56 @@ package com.epmet.controller;
import com.epmet.common.token.dto.form.LoginByPassWordFormDTO;
import com.epmet.common.token.dto.form.LoginByWxCodeFormDTO;
import com.epmet.common.token.dto.result.UserTokenResultDTO;
import com.epmet.commons.tools.exception.ErrorCode;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.validator.AssertUtils;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.service.CaptchaService;
import com.epmet.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* @Description 通用登陆接口
* @Description 通用登接口
* @Author yinzuomei
* @Date 2020/3/14 13:58
*/
@RestController
@RequestMapping("login")
public class LoginController {
@Autowired
private CaptchaService captchaService;
@Autowired
private LoginService loginService;
/**
* @return void
* @param response
* @param uuid
* @Author yinzuomei
* @Description 生成验证码
* @Date 2020/3/17 16:08
**/
@GetMapping("captcha")
public void captcha(HttpServletResponse response, String uuid) throws IOException {
//uuid不能为空
AssertUtils.isBlank(uuid, ErrorCode.IDENTIFIER_NOT_NULL);
//生成图片验证码
BufferedImage image = captchaService.create(uuid);
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "jpg", out);
out.close();
}
/**
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<java.lang.String>

6
epmet-auth/src/main/java/com/epmet/redis/CaptchaRedis.java

@ -10,6 +10,8 @@ package com.epmet.redis;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.redis.RedisUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -21,6 +23,7 @@ import org.springframework.stereotype.Component;
*/
@Component
public class CaptchaRedis {
private Logger logger = LogManager.getLogger(CaptchaRedis.class);
/**
* 验证码5分钟过期
*/
@ -30,13 +33,14 @@ public class CaptchaRedis {
public void set(String uuid, String captcha){
String key = RedisKeys.getLoginCaptchaKey(uuid);
logger.info("保存验证码key=["+key+"]");
redisUtils.set(key, captcha, EXPIRE);
}
public String get(String uuid){
String key = RedisKeys.getLoginCaptchaKey(uuid);
String captcha = (String)redisUtils.get(key);
logger.info("获取验证码key=["+key+"]captcha=["+captcha+"]");
//删除验证码
if(captcha != null){
redisUtils.delete(key);

5
epmet-auth/src/main/java/com/epmet/service/impl/CaptchaServiceImpl.java

@ -11,6 +11,8 @@ package com.epmet.service.impl;
import com.google.code.kaptcha.Producer;
import com.epmet.redis.CaptchaRedis;
import com.epmet.service.CaptchaService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -24,6 +26,7 @@ import java.awt.image.BufferedImage;
*/
@Service
public class CaptchaServiceImpl implements CaptchaService {
private Logger logger = LogManager.getLogger(CaptchaServiceImpl.class);
@Autowired
private Producer producer;
@Autowired
@ -33,7 +36,7 @@ public class CaptchaServiceImpl implements CaptchaService {
public BufferedImage create(String uuid) {
//生成验证码
String captcha = producer.createText();
logger.info("uuid:"+uuid+",生成的验证码:"+captcha);
//保存验证码
captchaRedis.set(uuid, captcha);

Loading…
Cancel
Save