package com.backendsys.config.Kaptcha; import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import jakarta.servlet.http.HttpServlet; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class KaptchaConfig extends HttpServlet { @Bean public Producer captcha() { // 配置图形验证码的基本参数 Properties properties = new Properties(); /* properties.put("kaptcha.border", "no"); properties.put("kaptcha.textproducer.char.length","4"); properties.put("kaptcha.image.height","50"); properties.put("kaptcha.image.width","150"); properties.put("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy"); properties.put("kaptcha.textproducer.font.color","black"); properties.put("kaptcha.textproducer.font.size","40"); properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise"); //properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.DefaultNoise"); properties.put("kaptcha.textproducer.char.string","acdefhkmnprtwxy2345678"); // 图片边框 properties.put("kaptcha.border", "yes"); //边框颜色 properties.put("kaptcha.border.color", "blue"); //背景颜色渐变开始 properties.put("kaptcha.background.clear.from", "47,58,255"); //背景颜色渐变结束 properties.put("kaptcha.background.clear.to", "249,207,204"); // 字体颜色 properties.put("kaptcha.textproducer.font.color", "black"); // 文字间隔 properties.put("kaptcha.textproducer.char.space", "5"); // 字体 properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑"); // 参数:https://blog.csdn.net/weixin_46469022/article/details/121931728 参考写法: https://zhuanlan.zhihu.com/p/502277958?utm_id=0 https://www.jianshu.com/p/bc76da9641a0 */ // properties.setProperty("kaptcha.border", "no"); // 是否边框(yes/no) // properties.setProperty("kaptcha.image.width", "90"); // 图片宽度 // properties.setProperty("kaptcha.image.height", "45"); // 图片长度 // properties.setProperty("kaptcha.textproducer.char.string", "acdefhkmnprtwxy0123456789"); // 字符集 // properties.setProperty("kaptcha.textproducer.char.length", "4"); // 字符长度 // 验证码是否带边框 No properties.setProperty("kaptcha.border", "no"); // 验证码字体颜色 properties.setProperty("kaptcha.textproducer.font.color", "black"); // 干扰线颜色 properties.setProperty("kaptcha.noise.color", "black"); // 验证码整体宽度 properties.setProperty("kaptcha.image.width", "120"); // 验证码整体高度 properties.setProperty("kaptcha.image.height", "45"); // 文字个数 properties.setProperty("kaptcha.textproducer.char.length", "4"); // 文字大小 properties.setProperty("kaptcha.textproducer.font.size","40"); // 文字随机字体 properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); // 文字距离 // properties.setProperty("kaptcha.textproducer.char.space","16"); // 自定义验证码背景 properties.setProperty("kaptcha.background.impl", "com.backendsys.KLingConfig.Kaptcha.KaptchaNoBackhround"); Config config = new Config(properties); // 使用默认的图形验证码实现,当然也可以自定义实现 DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(config); return defaultKaptcha; } }