添加验证码
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>传统web开发
前后端分离
@Configuration
public class KaptchaConfiguration {
@Bean
public Producer kaptcha() {
// 创建一个新的Properties对象,用于存储Kaptcha的配置
Properties properties = new Properties();
// 设置验证码图片的宽度为150像素
properties.setProperty("kaptcha.image.width", "150");
// 设置验证码图片的高度为50像素
properties.setProperty("kaptcha.image.height", "50");
// 设置验证码的字符集为数字0-9
properties.setProperty("kaptcha.textproducer.char.string", "0123456789");
// 设置验证码的长度为4个字符
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 设置验证码的字体大小为40
properties.setProperty("kaptcha.textproducer.font.size", "40");
// 设置验证码的噪声实现类为NoNoise,即不产生噪声
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
// 设置验证码的干扰实现类为ShadowGimpy,即产生一个阴影效果
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
// 设置验证码的背景颜色从白色渐变到白色(即纯白色背景)
properties.setProperty("kaptcha.background.clear.from", "white");
properties.setProperty("kaptcha.background.clear.to", "white");
// 设置验证码的字体颜色为黑色
properties.setProperty("kaptcha.textproducer.font.color", "black");
// 设置验证码的字符间距为5
properties.setProperty("kaptcha.textproducer.char.space", "5");
// 设置验证码的字体为Arial和Courier
properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
// 设置验证码的边框为无边框
properties.setProperty("kaptcha.border", "no");
// 设置验证码的边框颜色为黑色(实际上由于上一行设置了无边框,所以这一行设置的边框颜色不会生效)
properties.setProperty("kaptcha.border.color", "black");
// 创建一个DefaultKaptcha对象
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
// 将上面设置的属性应用到DefaultKaptcha对象
defaultKaptcha.setConfig(new Config(properties));
// 返回配置好的DefaultKaptcha对象
return defaultKaptcha;
}
}最后更新于