SpringBoot使用Kaptcha实现验证码的生成与验证功能

作者:菜鸟是大神 时间:2022-11-30 15:57:44 

当我们在项目中登录使用验证码的时候,不妨试试Kaptcha生成验证码,非常简单

1、首先,我们在pom.xml文件中引入kaptcha的maven依赖

<!-- kaptcha验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>

2、然后,我们编写kaptcha的配置类:KaptchaConfig.java

package com.lzzy.meet.common.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Properties;

/**
* @ClassName KaptchaConfig
* kaptcha配置类
* @Author
* @Date 2019-09-05 13:50:50
* @Version 1.0
**/
@Slf4j
@Component
public class KaptchaConfig {

@Bean
   public DefaultKaptcha getKaptcheCode() {
       DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
       Properties properties = new Properties();
       properties.setProperty("kaptcha.border", "no");
       properties.setProperty("kaptcha.textproducer.font.color", "black");
       properties.setProperty("kaptcha.image.width", "100");
       properties.setProperty("kaptcha.image.height", "36");
       properties.setProperty("kaptcha.textproducer.font.size", "30");
       properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
       properties.setProperty("kaptcha.session.key", "code");
       properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
       properties.setProperty("kaptcha.background.clear.from", "232,240,254");
       properties.setProperty("kaptcha.background.clear.to", "232,240,254");
       properties.setProperty("kaptcha.textproducer.char.length", "4");
       properties.setProperty("kaptcha.textproducer.font.names", "彩云,宋体,楷体,微软雅黑");
       Config config = new Config(properties);
       defaultKaptcha.setConfig(config);
       return defaultKaptcha;
   }
}

3、接下来,我们编写kaptcha的控制层:KaptchaController.java

package com.lzzy.meet.common.kaptcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
* @ClassName KaptchaController
* kaptcha调用
* @Author
* @Date 2019-09-05 13:59:59
* @Version 1.0
**/
@Slf4j
@Controller
@RequestMapping("kaptcha")
public class KaptchaController {

@Autowired
   private Producer producer;

@GetMapping("kaptcha-image")
   public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
       response.setDateHeader("Expires", 0);
       response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
       response.addHeader("Cache-Control", "post-check=0, pre-check=0");
       response.setHeader("Pragma", "no-cache");
       response.setContentType("image/jpeg");
       String capText = producer.createText();
       log.info("******************当前验证码为:{}******************", capText);
       // 将验证码存于session中
       request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
       BufferedImage bi = producer.createImage(capText);
       ServletOutputStream out = response.getOutputStream();
       // 向页面输出验证码
       ImageIO.write(bi, "jpg", out);
       try {
       // 清空缓存区
           out.flush();
       } finally {
       // 关闭输出流
           out.close();
       }
   }
}

4、然后,我们就可以在前端调用katpcha的接口生成验证码了:

<img th:src="@{/kaptcha/kaptcha-image}" class="ver_btn" onclick="this.src=this.src+'?c='+Math.random();"/>

 由于我这里使用的是 thymeleaf 模板引擎,所以路径名称会有点奇怪,生成的验证码样式如图所示:

SpringBoot使用Kaptcha实现验证码的生成与验证功能

 5、最后,我们将用户在客户端登陆时输入的验证码传送到服务端进行验证:

/**
* 验证验证码
* @param
* @return 正确:true/错误:false
*/
public static boolean validate(String registerCode) {
// 获取Session中验证码
Object captcha = ServletUtils.getAttribute(Constants.KAPTCHA_SESSION_KEY);
// 判断验证码是否为空
if (StringUtils.isEmpty(registerCode)) {
return false;
}
// 校验验证码的正确与否
boolean result = registerCode.equalsIgnoreCase(captcha.toString());
if (result) {
// 正确了后,将验证码从session中删掉
ServletUtils.getRequest().getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
}
// 返回验证结果
return result;
}

这样我们就成功的使用kaptcha完成了验证码的生成与验证功能

来源:https://blog.csdn.net/wufaqidong1/article/details/129437101

标签:SpringBoot,Kaptcha,验证码
0
投稿

猜你喜欢

  • C#判断当前程序是否通过管理员运行的方法

    2023-09-27 15:48:24
  • 微信开发准备第二步 springmvc mybatis项目结构搭建

    2021-07-16 22:18:37
  • Base64编码解码原理及C#编程实例

    2022-05-07 03:58:53
  • 浅谈SpringBoot在使用测试的时候是否需要@RunWith

    2022-12-19 04:14:14
  • C#使用iTextSharp将PDF转成文本的方法

    2022-05-03 16:59:48
  • Android Handler消息派发机制源码分析

    2023-12-18 17:32:55
  • java高并发锁的3种实现示例代码

    2022-05-23 16:45:47
  • Java Excel透视表相关操作实现代码

    2022-02-27 00:11:01
  • 深入理解Java并发编程之ThreadLocal

    2023-11-21 02:43:42
  • Spring注解@Configuration和@Component区别详解

    2022-11-05 02:04:18
  • 玩转Android之Drawable的使用

    2023-07-01 22:42:46
  • Android Volley框架全面解析

    2022-05-11 15:33:15
  • UnityShader使用图像叠加实现运动模糊

    2021-09-24 14:45:16
  • Kotlin协程Channel源码示例浅析

    2023-06-14 22:54:08
  • java如何判断一个对象是否为空对象

    2023-12-11 06:53:59
  • c# wpf如何附加依赖项属性

    2023-01-09 05:05:56
  • Java利用endorsed如何覆盖jdk提供的类详解

    2021-05-30 08:26:33
  • JUC循环屏障CyclicBarrier与CountDownLatch区别详解

    2021-11-17 10:40:08
  • Android WebView或手机浏览器打开连接问题解决办法总结

    2022-09-15 18:46:22
  • RxJava之网络请求最常见的三种场景

    2023-04-29 19:49:48
  • asp之家 软件编程 m.aspxhome.com