Java验证码 patchca

快来打我* 2021-11-10 00:34 405阅读 0赞

Java验证码 patchca

  • maven依赖
  • 工具类
  • 访问效果

maven依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.3.RELEASE</version>
  5. <relativePath />
  6. </parent>
  7. <properties>
  8. <maven.compiler.source>1.8</maven.compiler.source>
  9. <maven.compiler.target>1.8</maven.compiler.target>
  10. <springboot.version>2.1.3.RELEASE</springboot.version>
  11. </properties>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-freemarker</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.github.bingoohuang</groupId>
  22. <artifactId>patchca</artifactId>
  23. <version>0.0.1</version>
  24. </dependency>

工具类

  1. /** * 验证码工具类 * * @author zhengzc * @date 2019-08-01 */
  2. public class CaptchaUtil {
  3. public static ConfigurableCaptchaService getConfigurableCaptchaService() {
  4. ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
  5. RandomWordFactory wf = new RandomWordFactory();
  6. wf.setCharacters("23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ");
  7. wf.setMaxLength(4);
  8. wf.setMinLength(4);
  9. cs.setWordFactory(wf);
  10. return cs;
  11. }
  12. /** * 选择验证码生成工厂 * * @param cs * @param index */
  13. public static void setFactory(ConfigurableCaptchaService cs, int index) {
  14. System.err.println("工厂序号:" + index);
  15. if (index == 0) {
  16. // 弧线波纹
  17. cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
  18. } else if (index == 1) {
  19. // 大理石波纹
  20. cs.setFilterFactory(new MarbleRippleFilterFactory());
  21. } else if (index == 2) {
  22. // 大理石波纹
  23. cs.setFilterFactory(new DoubleRippleFilterFactory());
  24. } else if (index == 3) {
  25. // 摇摆波纹
  26. cs.setFilterFactory(new WobbleRippleFilterFactory());
  27. } else if (index == 4) {
  28. // 扩散的波纹
  29. cs.setFilterFactory(new DiffuseRippleFilterFactory());
  30. }
  31. }
  32. /** * 选择验证码生成工厂 * * @param cs * @param index */
  33. public static void setColorFactory(ConfigurableCaptchaService cs, int index) {
  34. System.err.println("颜色序号:" + index);
  35. if (index == 0) {
  36. // 自定义颜色
  37. cs.setColorFactory(getColorFactory());
  38. } else if (index == 1) {
  39. // 梯度变化颜色
  40. cs.setColorFactory(new GradientColorFactory());
  41. } else if (index == 2) {
  42. // 随机颜色
  43. cs.setColorFactory(new RandomColorFactory());
  44. } else if (index == 3) {
  45. // 单一颜色
  46. cs.setColorFactory(new SingleColorFactory());
  47. }
  48. }
  49. /** * 自定义颜色工厂 * * @return */
  50. private static ColorFactory getColorFactory() {
  51. return new ColorFactory() {
  52. @Override
  53. public Color getColor(int x) {
  54. Random random = new Random();
  55. int[] c = { 25, 60, 170};
  56. int i = random.nextInt(c.length);
  57. for (int fi = 0; fi < c.length; fi++) {
  58. if (fi == i) {
  59. c[fi] = random.nextInt(71);
  60. } else {
  61. c[fi] = random.nextInt(256);
  62. }
  63. }
  64. return new Color(c[0], c[1], c[2]);
  65. }
  66. };
  67. }
  68. }
  69. /** * 使用谷歌提供的验证码组件patchca来生成验证码 * * @author zhengzc * @date 2019-08-01 */
  70. @Controller
  71. @RequestMapping("/patchca")
  72. public class CaptchaController {
  73. @RequestMapping(value = "/getpatchcaImage")
  74. public void getpatchcaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
  75. ConfigurableCaptchaService cs = CaptchaUtil.getConfigurableCaptchaService();
  76. CaptchaUtil.setFactory(cs, new Random().nextInt(5));
  77. CaptchaUtil.setColorFactory(cs, new Random().nextInt(4));
  78. HttpSession session = request.getSession(false);
  79. if (session == null) {
  80. session = request.getSession();
  81. }
  82. setResponseHeaders(response);
  83. String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
  84. session.setAttribute("captchaToken", token);
  85. System.out.println("当前的 sessionId:" + session.getId() + ",验证码:" + token);
  86. }
  87. protected void setResponseHeaders(HttpServletResponse response) {
  88. response.setContentType("image/jpeg");
  89. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  90. response.setHeader("Pragma", "no-cache");
  91. long time = System.currentTimeMillis();
  92. response.setDateHeader("Last-Modified", time);
  93. response.setDateHeader("Date", time);
  94. response.setDateHeader("Expires", time);
  95. }
  96. }

访问效果

http://localhost:8080/patchca/getpatchcaImage

  • [0,1]在这里插入图片描述
  • [0,3]在这里插入图片描述
  • [1,2]在这里插入图片描述
  • [1,3]在这里插入图片描述
  • [2,1] 在这里插入图片描述
  • [2,3]在这里插入图片描述
  • [3,0]在这里插入图片描述
  • [3,1]在这里插入图片描述
  • [4,0]在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,405人围观)

还没有评论,来说两句吧...

相关阅读