1、在pom文件中先导入生成图像的依赖包

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>kaptcha-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>

2、在config写配置类

@Configuration
public class CaptchaConfig {
/**
* 验证码配置
* Kaptcha配置类名
*
* @return
*/
@Bean
@Qualifier("captchaProducer")
public DefaultKaptcha kaptcha() {
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//验证码个数
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
//字体间隔
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"8");
//干扰线颜色
//干扰实现类
properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
//图片样式
properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple");
//文字来源
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789"); Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}

3、配置好redis相关的配置

4、存入redis

  1. 根据请求获取客户ip作为key,方法如下:
  2. **
    * 获取ip
    * @param request
    * @return
    */
    public static String getIpAddr(HttpServletRequest request) {
    String ipAddress = null;
    try {
    ipAddress = request.getHeader("x-forwarded-for");
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    ipAddress = request.getHeader("Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    ipAddress = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    ipAddress = request.getRemoteAddr();
    if (ipAddress.equals("127.0.0.1")) {
    // 根据网卡取本机配置的IP
    InetAddress inet = null;
    try {
    inet = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    ipAddress = inet.getHostAddress();
    }
    }
    // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    if (ipAddress != null && ipAddress.length() > 15) {
    // "***.***.***.***".length()
    // = 15
    if (ipAddress.indexOf(",") > 0) {
    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
    }
    }
    } catch (Exception e) {
    ipAddress="";
    }
    return ipAddress;
    }

5、controller类编写

@RestController
@RequestMapping("/api/v1/captchar")
public class CaptCharController { @Autowired
private RedisTemplate redisTemplate; @Autowired
private Producer captchaProducer; @GetMapping("get_captcha")
public void getCaptChar(HttpServletRequest request, HttpServletResponse response){ String producerText = captchaProducer.createText();
String key = getKey(request);
//设置过期时间为10分钟
redisTemplate.opsForValue().set(key,producerText,10, TimeUnit.MINUTES); BufferedImage image = captchaProducer.createImage(producerText); ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
ImageIO.write(image,"jpg",outputStream);
outputStream.flush();
outputStream.close();
}catch (Exception e){
e.fillInStackTrace();
} } @GetMapping("check")
public String check(HttpServletRequest request, HttpServletResponse response, @RequestParam String value){
String key = getKey(request);
String v = (String) redisTemplate.opsForValue().get(key);
if (v.equals(value)){
return "验证成功";
}else {
return "校验失败";
}
} private String getKey(HttpServletRequest request){
String ipAddr = CommonUtil.getIpAddr(request);
String header = request.getHeader("User-Agent");
String key = "user-service:captcha:"+CommonUtil.MD5(ipAddr+header);
return key;
} }

redis获取图形验证码的更多相关文章

  1. jmeter 获取图形验证码接口测试

    今天开发提测了一个图形验证码的接口,以前没有测过这个,上来有点懵..... 记录一下. 使用jmeter配置好接口和参数,运行后查看结果树,显示都是乱码 解决方法: 添加一个后置处理器--beansh ...

  2. 务必收藏备用:.net core中通过Json或直接获取图形验证码(数字验证码、字母验证码、混合验证码),有源代码全实战demo(开源代码.net core3.0)

    很多人写的博客大家看了会一知半解,不知道怎么用,应该引用什么类库或者代码不全,这样很多小白很是头疼,尤其是尝新技术更是如此.我们这边不止告诉你步骤,而且还提供开源demo.随着时间的推移,我们的dem ...

  3. vue 项目,获取手机验证码和图形验证码(iviewUI框架)

    1.编辑获取验证码模块 <Form ref="phoneFormItem" :model="phoneFormItem" :label-width=&qu ...

  4. python爬虫中图形验证码的处理

    使用python爬虫自动登录时,遇到需要输入图形验证码的情况,一个比较简单的处理方法是使用打码平台识别验证码. 使用过两个打码平台,打码兔和若快,若快的价格更便宜,识别率相当.若快需要注册两个帐号:开 ...

  5. springmvc的文件上传和JWT图形验证码

    相关pom依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commo ...

  6. Java Web项目使用图形验证码 — Kaptcha

    一.验证码介绍 生成的主要方式: 1.使用Java原生的方式,其中包含了Servlet.AWT.ImageIO的使用: 2.使用开源库,例如Jcaptcha.Kaptcha...: (各图形验证码开源 ...

  7. 04 . Go+Vue开发一个线上外卖应用(用户名密码和图形验证码)

    图形化验证码生成和验证 功能介绍 在使用用户名和密码登录功能时,需要填写验证码,验证码是以图形化的方式进行获取和展示的. 验证码使用原理 验证码的使用流程和原理为:在服务器端负责生成图形化验证码,并以 ...

  8. 【无私分享:ASP.NET CORE 项目实战(第十四章)】图形验证码的实现

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 很长时间没有来更新博客了,一是,最近有些忙,二是,Core也是一直在摸索中,其实已经完成了一个框架了,并且正在准备在生产环境中 ...

  9. PHP5 GD库生成图形验证码(汉字)

    PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...

  10. PHP5生成图形验证码(有汉字)

    利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor   新建一个真彩色图像      imagecolora ...

随机推荐

  1. WPF项目需要不断更新前台图片时,碰到“System.IO.IOException: 文件“xxx”正由另一进程使用“问题的解决

    问题描述 项目中要求能不断拍照并更新显示图片,使用FileStream在本地创建了图片文件: 当下次重新拍照前删除之前拍过的图片时,提示"System.IO.IOException: 文件& ...

  2. 异常:java.sql.SQLException: HOUR_OF_DAY: 0 -> 1解决

    问题:Error attempting to get column 'xxTime' from result set. Cause: java.sql.SQLException: HOUR_OF_DA ...

  3. 9.增加Marker 标记

    1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="U ...

  4. Ubuntu磁盘查看分区和挂载

    1.查看硬盘 sudo lshw -c disk 查看有多少个硬盘,一般会显示 disk:0(设备名为 /dev/vda) disk:1 (设备名为 /dev/vdb) 2.查看分区 sudo fdi ...

  5. 题解[CF575E]Spectator_Riots

    题意 一个球场,可以看作 \(10^5\times10^5\) 的矩形,每个位置都是一个整点.一个位置 \((x,y)\) 位于球场内当且仅当 \(x\in[0,10^5]\and y\in[0,10 ...

  6. centos 换源

    原文: https://blog.csdn.net/MateSnake/article/details/124088310 备份原来的源:sudo mv /etc/yum.repos.d/CentOS ...

  7. for/in 语句用于循环对象属性

    for/in 语句用于循环对象属性. 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作. JavaScript 支持不同类型的循环: for - 循环代码块一定的次数 for/in  ...

  8. 微信小程序的this在success函数中使用

    在绝大多数情况下,函数的调用方式决定了this的值.this不能在执行期间被赋值,并且在每次函数被调用时this的值也可能会不同. 在微信小程序中 我就遇到的一些问题 requestName: fun ...

  9. jdk 1.8 处理 map和list

    1.map 的 key 和 value 反转 2. list<Object> 取两列转成  map<String,String> map 3. list<Object&g ...

  10. Micro-OA系统

    Micro-OA简单描述 MicroOA是一款不需要专业的开发知识或开发经验,通过页面交互式即可实现动态搭建表单的微型办公自动化系统.在日常工作当中,我们面临着各种各样的表单,在开发系统时,若我们采用 ...