介绍:

kaptcha是Google提供的一个图形验证码插件,有了它,你可以通过简单的配置生成各种样式的验证码。

1:SpringBoot引入kaptcha的依赖

        <dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version></dependency>

2:编写kaptcha配置类

package simulationvirtual.VirtualExperiment.config;import com.google.code.kaptcha.Producer;
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 {/*** Kaptcha图形验证码工具配置类* @author: Xiongch* @param:* @return: com.google.code.kaptcha.Producer* @date: 2022/9/9 15:47*/@Beanpublic Producer kaptchaProducer() {// 实例一个DefaultKaptchaDefaultKaptcha defaultKaptcha = new DefaultKaptcha();// 创建配置对象Properties properties = new Properties();// 设置边框properties.setProperty("kaptcha.border", "yes");// 设置颜色properties.setProperty("kaptcha.border.color", "105,179,90");// 设置字体颜色properties.setProperty("kaptcha.textproducer.font.color", "blue");// 设置宽度properties.setProperty("kaptcha.image.width", "125");// 高度properties.setProperty("kaptcha.image.height", "50");// 设置session.keyproperties.setProperty("kaptcha.session.key", "code");// 设置文本长度properties.setProperty("kaptcha.textproducer.char.length", "4");// 设置字体properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");// 将以上属性设置为实例一个DefaultKaptcha的属性Config config = new Config(properties);defaultKaptcha.setConfig(config);// 将defaultKaptcha返回return defaultKaptcha;}
}

2.1:Kaptcha详细配置

Kaptcha详细配置表
序号 属性名 描述 示例
1 kaptcha.width 验证码宽度 200
2 kaptcha.height 验证码高度 50
3 kaptcha.border.enabled 是否显示边框 false
4 kaptcha.border.color 边框颜色 black
5 kaptcha.border.thickness 边框厚度 2
6 kaptcha.content.length 验证码文本长度 5
7 kaptcha.content.source 文本源 abcde2345678gfynmnpwx
8 kaptcha.content.space 文本间隔 2
9 kaptcha.font.name 字体名称 Arial
10 kaptcha.font.size 字体大小 40
11 kaptcha.font.color 字体颜色 black
12 kaptcha.background-color.from 背景颜色(开始渐变色) lightGray
13 kaptcha.background-color.to 背景颜色(结束渐变色 white

3:编写Controller,实现验证码请求接口

package simulationvirtual.VirtualExperiment.controller.tools;import com.google.code.kaptcha.Producer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import simulationvirtual.VirtualExperiment.util.AjaxResult;
import simulationvirtual.VirtualExperiment.util.Base64;
import simulationvirtual.VirtualExperiment.util.UUIDutil;import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;import java.util.concurrent.TimeUnit;@RestController
public class KaptchaController {@Autowiredprivate RedisTemplate<String,Object> redisTemplate;@Autowiredprivate Producer kaptchaProduer;/*** 生成图形验证码* @author: Xiongch* @param:null* @return:Ajax_Result(统一返回工具)* @date: 2022/9/9 15:47*/@GetMapping("/kaptcha")public AjaxResult getKaptcha(HttpServletResponse response, HttpSession session){AjaxResult Ajax_Result = AjaxResult.success();String imagecode = kaptchaProduer.createText();// 生成图片BufferedImage image = kaptchaProduer.createImage(imagecode);// 将验证码存入Sessionsession.setAttribute("kaptcha",imagecode);//将图片输出给浏览器String uuid = UUIDutil.getUUID();//uuid-->验证码唯一标识FastByteArrayOutputStream os = new FastByteArrayOutputStream();try {response.setContentType("image/png");ImageIO.write(image,"png",os);//验证码实现redis缓存,过期时间2分钟session.setAttribute("uuid",imagecode);redisTemplate.opsForValue().set(uuid,imagecode,2, TimeUnit.MINUTES);} catch (IOException e) {return AjaxResult.error(e.getMessage());}Ajax_Result.put("uuid",uuid);Ajax_Result.put("img", Base64.encode(os.toByteArray()));return Ajax_Result;}}

4:vue前端展示代码

        <div class="login-code"><img :src="codeUrl" @click="getCode" class="login-code-img"/></div>

4.1:点击刷新方法

 methods: {getCode() {this.$axios({method:'GET',url:"/kaptcha",headers: {"Content-Type": "application/json"}}).then(res=>{this.codeUrl = "data:image/gif;base64," + res.data.img;})},

5:效果如下

SpringBoot整合kaptcha(谷歌验证码工具)实现验证码功能相关推荐

  1. 谷歌了java集成开发_Spring整合Kaptcha谷歌验证码工具的开发步骤

    开发步骤: 1.加入依赖 com.google.code.kaptcha kaptcha 2.3 国内镜像无法下载该依赖,需要手动通过jar包在本地仓库安装一个依赖. 安装命令: mvn instal ...

  2. Springboot整合kaptcha实现验证码

    验证码的作用 防止恶意破解密码.刷票.论坛灌水.刷页. 有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,实际上使用验证码是现在很多网站通行的方式(比如招商银行的网上个人银 ...

  3. Springboot整合kaptcha验证码

    Springboot整合kaptcha验证码 01.通过配置类来配置kaptcha 01-01.添加kaptcha的依赖: <!-- kaptcha验证码 --> <dependen ...

  4. Springboot 整合SpringSecurity实现账号密码+手机验证码登陆

    Springboot 整合SpringSecurity实现账号密码+手机验证码登陆 示例说明 版本 示例安装 Spring-security 介绍 为什么不用 shiro Spring-Securit ...

  5. Springboot整合SpringSecurity 04-启用登出logout功能

    Springboot整合SpringSecurity 04-启用登出logout功能 前面Springboot整合SpringSecurity 02-使用自定义登陆页面我们讲过了SpringSecur ...

  6. SpringBoot整合Shiro实现权限控制,验证码

    本文介绍 SpringBoot 整合 shiro,相对于 Spring Security 而言,shiro 更加简单,没有那么复杂. 目前我的需求是一个博客系统,有用户和管理员两种角色.一个用户可能有 ...

  7. springboot整合redis实现发送短信验证码

    我用的短信平台是阿里云的,需要付费购买服务,购买地址:https://common-buy.aliyun.com/?spm=5176.8195934.907839.sms6.312c4183mzE9Y ...

  8. Springboot整合Redis(RedisConfig等工具类编写)

    我们使用的是上一期创建的Spring boot项目,没看过那篇文章的可以去看看Springboot整合数据库 +JpaRepository实现简单数据查询 目录 Redis介绍 1.添加依赖 2.在` ...

  9. SpringBoot整合MyBatis并实现简单的查询功能

    学了SpringBoot整合MyBatis才知道什么叫做省事,想当初用SSM的时候,那配置是真的多,SpringBoot真的是太友好了,到底有多好,接下来演示一个对数据库的查询功能,然初学者的我们大开 ...

最新文章

  1. MAC OS 10.10.5虚拟机免费下载(可安装Xcode7)
  2. 项目中常用的 19 条 MySQL 优化总结
  3. HashMap的负载因子为什么默认是0.75
  4. 一款超炫酷后台权限管理系统
  5. Spark SQL 加载数据
  6. MFC VS2012对话框背景填图
  7. java 存储数据到文件中_本机速度文件支持的“纯” Java大数据存储
  8. php制作简单的用户登陆,如何用php代码实现简单的用户登陆以及登陆验证功能
  9. MAC编译OpenJDK8:clang: error: include path for libstdc++ headers not found(独家解决办法)
  10. python小甲鱼笔记_小甲鱼python笔记第4讲笔记(个人笔记)
  11. 计算机软件实习项目四 —— 校园一卡通管理系统 (实验准备) 12-27
  12. Abaqus学习笔记(基础)
  13. ab变频器22b用户手册_AB变频器 PowerFlex400用户手册.pdf
  14. 通过负载均衡器+域名实现容灾切换-(11)深信服负载均衡器
  15. 开源代码学习之persepolis【一】
  16. 怎样把普洱熟茶泡得更好喝?
  17. 应广单片机定时器中断配置
  18. Oracle获取一周前,一个月前,一年前的日期
  19. 【深度学习不是犯罪】欧盟祭出最严数据保护法:专家解读 GDPR
  20. img图片不失真,进行比例缩放

热门文章

  1. PostgreSQL编写记录删除表格信息的Extension扩展
  2. hibernate状态_如何选择Mac何时Hibernate(或“进入待机状态”)
  3. 当秀美小城遇上“懂行人”:怀凌云壮志,谱时代新篇!
  4. 【团队协作工具】Teambition 石墨 CSDN
  5. TensorFlow 中的identity()函数
  6. Java 序列化和反序列化详解完整版
  7. 爬虫ip被封的6个解决方法
  8. 基于环信sdk在uni-app框架中快速开发一款多平台社交Demo
  9. mysql增删改查 简称_mysql增删改查基本语句
  10. 海澜java题_00后的海澜之家初体验,都怪周杰伦!!