kaptcha是一个优秀的验证码框架 ,在不多的项目中使用了很多年很稳定,功能强大配置项很丰富,可以根据不同的需求配置不同的效果。
以下最常用的配置 字母+数字 验证码实现的效果图:
com.github.penggle
kaptcha
2.3.2
compile
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Bean
public DefaultKaptcha getKaptchaBean() {
// 创建 DefaultKaptcha 实例
DefaultKaptcha bean = new DefaultKaptcha();
// 创建 Properties 实例,用于配置 Kaptcha 属性
Properties properties = new Properties();
// 设置验证码边框为无边框
properties.setProperty("kaptcha.border", "no");
// 设置验证码图片的宽度为 95 像素
properties.setProperty("kaptcha.image.width", "95");
// 设置验证码图片的高度为 45 像素
properties.setProperty("kaptcha.image.height", "45");
// 设置验证码背景颜色为浅灰色
properties.setProperty("kaptcha.background.clear.from", "248,248,248");
// 设置验证码字符范围为数字和小写字母(排除容易混淆的字符)
properties.setProperty("kaptcha.textproducer.char.string", "23456789abcdefhijkmnpqrstuvwxyz");
// 设置验证码文字颜色为蓝色
properties.setProperty("kaptcha.textproducer.font.color", "0,0,255");
// 设置验证码干扰实现类为水波纹效果
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
// 设置验证码文字字体大小为 35 像素
properties.setProperty("kaptcha.textproducer.font.size", "35");
// 设置验证码字符长度为 4
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 设置验证码字符间隔为 2 像素
properties.setProperty("kaptcha.textproducer.char.space", "2");
// 设置验证码噪点生成实现类为无噪点
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
// 使用 Config 类将 Properties 配置应用到 Kaptcha 实例中
com.google.code.kaptcha.util.Config config = new com.google.code.kaptcha.util.Config(properties);
bean.setConfig(config);
// 返回配置后的 Kaptcha 实例
return bean;
}
}
如果不是springboot项目,xml配置项:
no
95
45
248,248,248
248,248,248
23456789abcdefhijkmnpqrstuvwxyz
0,0,255
com.google.code.kaptcha.impl.WaterRipple
35
4
2
new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
com.google.code.kaptcha.impl.NoNoise
import com.google.code.kaptcha.Producer;
import com.jxwifi.kyc.common.tools.cache.CaptcheCacheServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
/**
* 图形验证码控制器
*/
@RequestMapping("/public/")
@RestController
public class CaptchaController {
@Autowired
private CaptcheCacheServiceImpl cacheService;
@Autowired
private Producer captchaProducer;
/**
* 获取图形验证码
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/getCaptcha", method = RequestMethod.GET)
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
String uuid = UUID.randomUUID().toString();
String captcha = captchaProducer.createText();
cacheService.put("captchaKey_"+uuid, captcha);
// 将验证码放置到Cookie中
Cookie cookie = new Cookie("captcha-key", uuid);
cookie.setPath("/");
cookie.setMaxAge(60*1000);
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin",
response.addCookie(cookie);
BufferedImage challenge = captchaProducer.createImage(captcha);
ImageIO.write(challenge, "jpg", jpegOutputStream);
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = null;
responseOutputStream = response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}