JAVA:Spring Boot整合Kaptcha验证码实现登录验证

请关注微信公众号:拾荒的小海螺
博客地址:http://lsk-ww.cn/

1、简述

在Web应用程序中,验证码是一种常见的安全措施,用于验证用户的身份以防止恶意活动,如自动化攻击或机器人。Spring Boot提供了许多库和工具,使得集成验证码变得相对容易。在本文中,我们将介绍如何使用Kaptcha库在Spring Boot应用程序中实现验证码功能。
在这里插入图片描述

2、集成

Kaptcha是一个用Java编写的简单验证码生成库。它可以生成基于文本的验证码图片,用于Web应用程序的身份验证。Kaptcha提供了许多配置选项,使得生成的验证码图片可以适应各种应用场景。

2.1 添加依赖

首先,我们需要在Spring Boot项目中添加Kaptcha的依赖。在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>com.github.axet</groupId>
    <artifactId>kaptcha</artifactId>
    <version>0.0.9</version>
</dependency>
2.2 配置

接下来,我们需要配置Kaptcha以生成验证码图片。在Spring Boot中,我们可以通过创建一个配置类来完成这项任务。创建一个名为KaptchaConfig的类,并将以下内容添加到其中:

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaConfig {

    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.textproducer.char.space", "5");
        properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
        // 配置验证码图片的宽度
        properties.setProperty("kaptcha.image.width", "150");
        // 配置验证码图片的高度
        properties.setProperty("kaptcha.image.height", "50");
        // 配置验证码字符长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

在上面的配置中,我们设置了验证码图片的宽度、高度和字符长度。你可以根据自己的需求进行调整。

3、创建验证码接口

现在,我们需要创建一个接口来生成和提供验证码图片。我们将创建一个RESTful接口,让客户端可以通过HTTP请求获取验证码图片:

@GetMapping("captcha.jpg")
public void captcha(HttpServletResponse response, String uuid)throws IOException {
	response.setHeader("Cache-Control", "no-store, no-cache");
	response.setContentType("image/jpeg");
	
	//获取图片验证码
	BufferedImage image = sysCaptchaService.getCaptcha(uuid);
	
	ServletOutputStream out = response.getOutputStream();
	ImageIO.write(image, "jpg", out);
	IOUtils.closeQuietly(out);
}

以一个随机的UUID来设计当前随机验证码存库表,通过当前库表的过期时间来校验:

@Data
@TableName("lk_captcha")
public class CaptchaEntity  {
    @TableId(type = IdType.INPUT)
    private String uuid;
    /**
     * 验证码
     */
    private String code;
    /**
     * 过期时间
     */
    private Date expireTime;

}

创建service时间接口来获取当前随机的图片流和校验接口:

@Autowired
  private Producer producer;

  @Override
  public BufferedImage getCaptcha(String uuid) {
      if(StringUtils.isBlank(uuid)){
          throw new RRException("uuid不能为空");
      }
      //生成文字验证码
      String code = producer.createText();

      CaptchaEntity  captchaEntity = new CaptchaEntity ();
      captchaEntity.setUuid(uuid);
      captchaEntity.setCode(code);
      //5分钟后过期
      captchaEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), 5));
      this.save(captchaEntity);

      return producer.createImage(code);
  }

  @Override
  public boolean validate(String uuid, String code) {
      CaptchaEntity captchaEntity = this.getOne(new QueryWrapper<CaptchaEntity >().eq("uuid", uuid));
      if(captchaEntity == null){
          return false;
      }

      //删除验证码
      this.removeById(uuid);

      if(captchaEntity.getCode().equalsIgnoreCase(code) && captchaEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
          return true;
      }

      return false;
  }

在上面的代码中,我们创建了一个名为getCaptcha的处理方法,它将生成验证码图片,并将图片以BufferedImage的形式返回给客户端。

4、结论

通过使用Kaptcha库,我们可以很容易地在Spring Boot项目中实现验证码功能,增强Web应用程序的安全性。在本文中,我们介绍了如何添加Kaptcha依赖、配置Kaptcha、创建验证码接口,并集成验证码功能到Spring Boot应用程序中。

希望本文能够帮助你顺利地在你的项目中实现验证码功能!

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾荒的小海螺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值