81 lines
3.1 KiB
Java
81 lines
3.1 KiB
Java
package com.njcn.auth.controller;
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
import com.google.code.kaptcha.Producer;
|
|
import com.google.code.kaptcha.util.Config;
|
|
import com.njcn.auth.utils.AuthPubUtil;
|
|
import com.njcn.common.pojo.constant.SecurityConstants;
|
|
import com.njcn.redis.utils.RedisUtil;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
|
|
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.io.IOException;
|
|
import java.util.Properties;
|
|
|
|
/**
|
|
* @author hongawen
|
|
* @version 1.0.0
|
|
* @date 2021年06月04日 15:25
|
|
*/
|
|
@Api(tags = "认证中心")
|
|
@Slf4j
|
|
@Controller
|
|
@RequestMapping("/auth")
|
|
@AllArgsConstructor
|
|
public class KaptchaController {
|
|
|
|
private final RedisUtil redisUtil;
|
|
|
|
@ApiIgnore
|
|
@ApiOperation("获取图形验证码")
|
|
@GetMapping("/getImgCode")
|
|
public void getImgCode(@ApiIgnore HttpServletResponse resp, @ApiIgnore HttpServletRequest request) {
|
|
ServletOutputStream out = null;
|
|
try {
|
|
out = resp.getOutputStream();
|
|
// resp.setContentType("image/jpeg");"/pqs-auth/auth/getImgCode",
|
|
if (null != out) {
|
|
Properties props = new Properties();
|
|
Producer kaptchaProducer;
|
|
ImageIO.setUseCache(false);
|
|
props.put("kaptcha.border", "no");
|
|
props.put("kaptcha.textproducer.font.color", "black");
|
|
/*props.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");*/
|
|
/*props.put("kaptcha.noise.impl", "com.sso.utils.ComplexNoise");*/
|
|
props.put("kaptcha.textproducer.char.space", "5");
|
|
props.put("kaptcha.textproducer.char.length", "4");
|
|
Config config = new Config(props);
|
|
kaptchaProducer = config.getProducerImpl();
|
|
//此处需要固定采用字母和数字混合
|
|
String capText = AuthPubUtil.getKaptchaText(4);
|
|
String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
|
|
String ip = request.getHeader(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP);
|
|
String key = userAgent + ip;
|
|
redisUtil.delete(key);
|
|
redisUtil.saveByKeyWithExpire(key, capText, 30*60L);
|
|
BufferedImage bi = kaptchaProducer.createImage(capText);
|
|
ImageIO.write(bi, "jpg", out);
|
|
out.flush();
|
|
}
|
|
} catch (IOException ioException) {
|
|
log.error("获取图形验证码异常,异常为:{}", ioException.toString());
|
|
} finally {
|
|
IoUtil.close(out);
|
|
}
|
|
|
|
}
|
|
|
|
}
|