SpringBoot开发九-生成验证码
需求介绍—生成验证码
先生成随机字符串然后利用Kaptcha API生成验证图片
代码实现
先在pom.xml引入
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
因为SpringBoot中没有整合kaptcha,所以我们需要自己对这个做一个配置,所以要写一个配置类KaptchaConfig加载到Spring容器里面对其做一个初始化。
ckage com.nowcoder.community.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 {
// 实例化这个接口
@Bean
public Producer kaptchaProducer() {
Properties properties = new Properties();
properties.setProperty("kaptcha.image.width", "100");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.font.size", "32");
properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");
// 验证码的字符在哪个范围里随机产生
properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
// 验证码的长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
DefaultKaptcha kaptcha = new DefaultKaptcha();
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
然后再登录页面LoginController里面使用,思想是在一个方法是给浏览器返回一个html,这个html里面会包含一个验证码的路径,浏览器会再访问服务器得到这个验证码图片,所以要写一个方法向浏览器返回图片,但是在模板里会引入这个路径。
@Autowired
private Producer kaptchaProducer; @RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
public void getKaptcha(HttpServletResponse httpServletResponse, HttpSession httpSession) {
// 生成验证码
String text = kaptchaProducer.createText();
BufferedImage image = kaptchaProducer.createImage(text); // 将验证码存入session
httpSession.setAttribute("kaptcha", text); // 将图片输出给浏览器
httpServletResponse.setContentType("image/png");
try {
OutputStream os = httpServletResponse.getOutputStream();
ImageIO.write(image, "png", os);
} catch (IOException e) {
logger.error("响应验证码失败" + e.getMessage());
}
}
当然你还需要在login.html文件里面改一下对应的那个验证码的路径就可以实现了。
SpringBoot开发九-生成验证码的更多相关文章
- Springboot 生成验证码
技术:springboot+kaptcha+session 概述 场景介绍 验证码,用于web网站.用户点击验证码图片后,生成验证码.提交后,用户输入验证码和Session验证码,进行校验. 详细 ...
- SpringBoot开发mockserver及生成swagger接口文档
通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后 → jenkin ...
- java web学习总结(九) -------------------通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- JavaWeb---总结(九)通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl. ...
- javaweb学习总结(九)—— 通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- java web 学习九(通过servlet生成验证码图片)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- 【开发技术】Java生成验证码
Java生成验证码 为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下; ...
- javaweb(九)—— 通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- javaweb学习总结(九)—— 通过Servlet生成验证码图片(转)
(每天都会更新至少一篇以上,有兴趣的可以关注)转载自孤傲苍狼 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
随机推荐
- 使用RestTemplate发送post请求,请求头中封装参数
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败.中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- 并不是static final 修饰的变量都是编译期常量
见代码 public class Test { public static void main(String[] args){ // 情况一 基本数据类型 //System.out.println(O ...
- Python+unittest+excel
接口测试设计思想: 框架结构如下: 目录如下: readme: config下的run_case_config.ini 文件说明: run_mode: 0:获取所有sheet页 1: if case_ ...
- jenkins报错: error: insufficient permission for adding an object to repository database .git/objects
前言:这是在用jenkins去gitlab上面去拉下代码来编译,就报了这个错,在这里记录下,避免下次 报错: 17:08:17 error: insufficient permission for ...
- ESP32-任务看门狗笔记
看门狗机制用于监控嵌入式系统运行并在发生不可知的软硬件故障时将系统复位.系统正常运行时,看门狗定时器溢出之前会被重置计数值,也就是"喂狗".定时器溢出意味着无法"喂狗&q ...
- pxe+kickstart部署多个版本的Linux操作系统(上)---原理篇
PXE概述: 1.PXE(Pre-bootExecution Environment),预启动执行环境2.通过网络接口启动计算机3.支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系 ...
- Selenium启动Firefox示例(python版)
目前做selenium自动化使用的主流语言分为java和python,前一篇为java版,本篇介绍python实现selenium启动Firefox. 1 #-*- coding:utf-8 -*- ...
- Requests方法 -- 参数化
import requests#禁用安全请求警告from requests.packages.urllib3.exceptions import InsecureRequestWarningreque ...
- 开源协同办公平台部署教程:O2OA PAAS平台部署
一.镜像制作1.将安装介质o2server-5.0.3-linux.zip上传至镜像制作服务器上.(上传目录为/paas/xxhpaas/moka/o2oa)2.使用unzip命令解压安装包,参考命令 ...
- Spring自动装配(二)
为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...