feat: 增加生成验证码

dev
huangfeng 11 months ago
parent dc7185bf2b
commit fc2fe00902

@ -0,0 +1,66 @@
package com.shxy.xymanager_admin.controller;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.shxy.xymanager_common.kaptcha.KaptchaCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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 javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
@RestController
@Slf4j
@Api(value = "生成验证码", description = "生成验证码")
public class KaptchaController {
@Autowired
private Producer captchaProducer;
@ApiOperation("生成验证码")
@GetMapping("/kaptcha")
public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
log.info("登录验证码:{}",capText);
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
KaptchaCache.getInstance().put(capText.toLowerCase(), capText);
// KaptchaCache.getInstance().put(capText.toLowerCase());
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out;
try {
out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
out.flush();
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -166,6 +166,12 @@
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
</dependency>
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,38 @@
package com.shxy.xymanager_common.config;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class CaptchaConfig {
@Bean(name = "captchaProducer")
public DefaultKaptcha getKaptchaBean() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial, Helvetica");
properties.setProperty(Constants.KAPTCHA_BORDER, "no");
properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "0,0,0");
properties.setProperty("kaptcha.textproducer.font.color", "36,114,180");
properties.setProperty("kaptcha.textproducer.font.size", "40");
properties.setProperty("kaptcha.image.width", "160");
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.char.string", "zxcvbnmasdfghjkqwertyupZXCVBNMASDFGHJKLQWERTYUP23456789");
properties.setProperty("kaptcha.word.impl", "com.google.code.kaptcha.text.impl.DefaultWordRenderer");
properties.setProperty("kaptcha.textproducer.impl", "com.google.code.kaptcha.text.impl.DefaultTextCreator");
properties.setProperty("kaptcha.background.clear.from", "255,255,255");
properties.setProperty("kaptcha.background.clear.to", "255,255,255");
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}

@ -76,6 +76,7 @@ public class SecurityConfig {
urlWhiteList.add("/xymanager/upload/uploadLog");
urlWhiteList.add("/xymanager/upload/uploadFile");
urlWhiteList.add("/xymanager/henan/**");
urlWhiteList.add("/xymanager/kaptcha");
StreamReadConstraints streamReadConstraints = StreamReadConstraints
.builder()

@ -0,0 +1,48 @@
package com.shxy.xymanager_common.kaptcha;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.concurrent.TimeUnit;
public class KaptchaCache {
/**
* 3
*/
private final Cache<String, String> kaptchas = CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterWrite(3, TimeUnit.MINUTES)
.build();
private KaptchaCache() {
}
public static KaptchaCache getInstance() {
return KaptchaCacheCacheHolder.instance;
}
private static class KaptchaCacheCacheHolder {
private static KaptchaCache instance = new KaptchaCache();
}
public void put(String code, String value) {
kaptchas.put(code, value);
}
public void invalidate(String code) {
kaptchas.invalidate(code);
}
public boolean verificationCode(String code, String value) {
String codeCache = kaptchas.getIfPresent(code);
if (null != codeCache && codeCache.equalsIgnoreCase(code)) {
kaptchas.invalidate(code);
return true;
}
return false;
}
}
Loading…
Cancel
Save