springboot整合kaptcha生成验证码功能
作者:Mitsuha三葉 发布时间:2023-07-14 21:48:51
标签:springboot,kaptcha,验证码
介绍:kaptcha 是谷歌开源的非常实用的验证码生成工具
一、导入jar包
<!-- kaptcha验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
二、编写kaptcha配置类
package com.zym.config;
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 defaultKaptcha(){
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//边框
properties.setProperty("kaptcha.border", "no");
//字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "green");
//图片宽度
properties.setProperty("kaptcha.image.width", "120");
//图片高度
properties.setProperty("kaptcha.image.height", "30");
//字体大小
properties.setProperty("kaptcha.textproducer.font.size", "20");
//session key
properties.setProperty("kaptcha.session.key", "kaptcha");
//验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
//字体
properties.setProperty("kaptcha.textproducer.font.names", "宋体");
//文字间隔
properties.setProperty("kaptcha.textproducer.char.space", "10");
//噪点实现类
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
//图片样式-阴影
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
三、编写接口
package com.zym.controller;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
@RestController
public class KaptchaController {
@Autowired
private DefaultKaptcha defaultKaptcha;
@GetMapping("/kaptcha")
public void getKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
//设置响应头
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String text = defaultKaptcha.createText();
HttpSession session = request.getSession();
//将验证码存入session
session.setAttribute("code", text);
//创建验证码图片
BufferedImage image = defaultKaptcha.createImage(text);
ServletOutputStream os = response.getOutputStream();
ImageIO.write(image, "jpg", os);
IOUtils.closeQuietly(os);
}
}
四、测试接口
使用PostMan:
输入url:localhost:8080/kaptcha
成功得到验证码图片,大功告成!
来源:https://blog.csdn.net/qq_42804736/article/details/109200622


猜你喜欢
- 本文实现的功能有:1、 初始化游戏窗口2、初始化游戏的界面3、初始化游戏的说明面板4、随机生成下落方块5、方块下落速度变化6、判断方块是否可
- 一、前往 jetbrains 官网下载 IDEA Ultimate版本https://www.jetbrains.com/idea/down
- RunnableRunnable接口非常简单,就定义了一个方法run(), 实现Runnable接口的run方法就可以实现多线程// 函数式
- 两种android图片裁剪方式,供大家参考,具体内容如下一、相机拍完照之后利用系统自带裁剪工具进行截取public static void
- 1.1 接口组成更新概述接口的组成常量:public static final抽象方法:public abstract默认方法(Java 8
- 笔者在使用Entity Framework中的Scaffolding机制自动创建拓展名为mdf的数据库及表单时,遇到如下的错误:A file
- 1.利用 “+”(加号)运算符:string str = “Hello”+ “World”; console.WriteLine(str);
- 引言在Google I/O 2014上,Google公布了Android L Preview版本,此版本的UI有了非常大的改变,很炫很给力!
- 1、概述首先和大家一起回顾一下Java 消息服务,在我之前的博客《Java消息队列-JMS概述》中,我为大家分析了:1.消息服务:一个中间件
- 可能我们用惯了 Newtonsoft.Json.dll 等第三方的类库来实现序列化等类似的操作,但是有时只是简单的用一下,感觉没必要那么费事
- 1. 场景描述本节结合springboot2、springmvc、mybatis、swagger2等,搭建一个完整的增删改查项目,希望通过这
- 这篇文章主要介绍了基于Java检查IPv6地址的合法性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 本文实例为大家分享了Java实现简易计算器的具体代码,供大家参考,具体内容如下程序的运行环境为Windows10 ,编译环境为IDEA。计算
- 在Java中我们知道静态变量会在类加载时机的“初始化”阶段得到赋值(编译器会收集类中的静态变量及静态
- 如何在冗长的监控录像中找到关键点?我们知道,监控录像中大部分信息都是没用的,那些信息就等同于一幅静态图像。我们要等待监控的范围内出现异常情况
- 本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下1、TextView 主要用于界面上显示一段文本
- 用Canvas画贝塞尔曲线,要画贝塞尔曲线首先了解贝塞尔曲线:由于用计算机画图大部分时间是操作鼠标来掌握线条的路径,与手绘的感觉和效果有很大
- 在定义API的时候,对于一些返回集合对象的方法,很多人喜欢将返回类型定义成IEnumerable<T>,这本没有什么问题。这里要
- 一、前言微信接口调用验证最终需要用到的三个参数noncestr、timestamp、signature:接下来将会给出获取这三个参数的详细代
- 在c++当中,标准类型string来代表可变长的字符串序列,使用string需要加载string 头文件,而方法定义在命名空间std当中,所