? ? ? ? ?kaptcha是一个优秀的验证码框架?,在不多的项目中使用了很多年很稳定,功能强大配置项很丰富,可以根据不同的需求配置不同的效果。
?以下最常用的配置? 字母+数字 验证码实现的效果图:?
?
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>
@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配置项:
<!-- 图片边框 -->
<prop key="kaptcha.border">no</prop>
<!-- 图片宽度 -->
<prop key="kaptcha.image.width">95</prop>
<!-- 图片高度 -->
<prop key="kaptcha.image.height">45</prop>
<!-- 验证码背景颜色渐变,开始颜色 -->
<prop key="kaptcha.background.clear.from">248,248,248</prop>
<!-- 验证码背景颜色渐变,结束颜色 -->
<prop key="kaptcha.background.clear.to">248,248,248</prop>
<!-- 验证码的字符 -->
<prop key="kaptcha.textproducer.char.string">23456789abcdefhijkmnpqrstuvwxyz</prop>
<!-- 验证码字体颜色 -->
<prop key="kaptcha.textproducer.font.color">0,0,255</prop>
<!-- 验证码的效果,水纹 -->
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>
<!-- 验证码字体大小 -->
<prop key="kaptcha.textproducer.font.size">35</prop>
<!-- 验证码字数 -->
<prop key="kaptcha.textproducer.char.length">4</prop>
<!-- 验证码文字间距 -->
<prop key="kaptcha.textproducer.char.space">2</prop>
<!-- 验证码字体 -->
<prop key="kaptcha.textproducer.font.names">new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
</prop>
<!-- 不加噪声 -->
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
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();
}
}
}