KaptchaConfig.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.backendsys.config.Kaptcha;
  2. import com.google.code.kaptcha.Producer;
  3. import com.google.code.kaptcha.impl.DefaultKaptcha;
  4. import com.google.code.kaptcha.util.Config;
  5. import jakarta.servlet.http.HttpServlet;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import java.util.Properties;
  9. @Configuration
  10. public class KaptchaConfig extends HttpServlet {
  11. @Bean
  12. public Producer captcha() {
  13. // 配置图形验证码的基本参数
  14. Properties properties = new Properties();
  15. /*
  16. properties.put("kaptcha.border", "no");
  17. properties.put("kaptcha.textproducer.char.length","4");
  18. properties.put("kaptcha.image.height","50");
  19. properties.put("kaptcha.image.width","150");
  20. properties.put("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");
  21. properties.put("kaptcha.textproducer.font.color","black");
  22. properties.put("kaptcha.textproducer.font.size","40");
  23. properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
  24. //properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.DefaultNoise");
  25. properties.put("kaptcha.textproducer.char.string","acdefhkmnprtwxy2345678");
  26. // 图片边框
  27. properties.put("kaptcha.border", "yes");
  28. //边框颜色
  29. properties.put("kaptcha.border.color", "blue");
  30. //背景颜色渐变开始
  31. properties.put("kaptcha.background.clear.from", "47,58,255");
  32. //背景颜色渐变结束
  33. properties.put("kaptcha.background.clear.to", "249,207,204");
  34. // 字体颜色
  35. properties.put("kaptcha.textproducer.font.color", "black");
  36. // 文字间隔
  37. properties.put("kaptcha.textproducer.char.space", "5");
  38. // 字体
  39. properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
  40. //
  41. 参数:https://blog.csdn.net/weixin_46469022/article/details/121931728
  42. 参考写法:
  43. https://zhuanlan.zhihu.com/p/502277958?utm_id=0
  44. https://www.jianshu.com/p/bc76da9641a0
  45. */
  46. // properties.setProperty("kaptcha.border", "no"); // 是否边框(yes/no)
  47. // properties.setProperty("kaptcha.image.width", "90"); // 图片宽度
  48. // properties.setProperty("kaptcha.image.height", "45"); // 图片长度
  49. // properties.setProperty("kaptcha.textproducer.char.string", "acdefhkmnprtwxy0123456789"); // 字符集
  50. // properties.setProperty("kaptcha.textproducer.char.length", "4"); // 字符长度
  51. // 验证码是否带边框 No
  52. properties.setProperty("kaptcha.border", "no");
  53. // 验证码字体颜色
  54. properties.setProperty("kaptcha.textproducer.font.color", "black");
  55. // 干扰线颜色
  56. properties.setProperty("kaptcha.noise.color", "black");
  57. // 验证码整体宽度
  58. properties.setProperty("kaptcha.image.width", "120");
  59. // 验证码整体高度
  60. properties.setProperty("kaptcha.image.height", "45");
  61. // 文字个数
  62. properties.setProperty("kaptcha.textproducer.char.length", "4");
  63. // 文字大小
  64. properties.setProperty("kaptcha.textproducer.font.size","40");
  65. // 文字随机字体
  66. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  67. // 文字距离
  68. // properties.setProperty("kaptcha.textproducer.char.space","16");
  69. // 自定义验证码背景
  70. properties.setProperty("kaptcha.background.impl", "com.backendsys.KLingConfig.Kaptcha.KaptchaNoBackhround");
  71. Config config = new Config(properties);
  72. // 使用默认的图形验证码实现,当然也可以自定义实现
  73. DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  74. defaultKaptcha.setConfig(config);
  75. return defaultKaptcha;
  76. }
  77. }