Springboot整合kaptcha验证码
01、通过配置类来配置kaptcha
01-01、添加kaptcha的依赖:
<!-- kaptcha验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
02-01、新建KaptchaConfig配置类
package com.kuangstudy.config.code;
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 getDefaultKaptcha(){
DefaultKaptcha captchaProducer = 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", "110");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.font.size", "30");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
captchaProducer.setConfig(config);
return captchaProducer;
}
}
03-01、编写controller用于生成验证码:
package com.kuangstudy.controller.code;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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;
@Controller
public class CodeController {
@Autowired
private Producer captchaProducer ;
@RequestMapping("/kaptcha")
public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
//生成验证码
String capText = captchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
//向客户端写出
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
}
04-01、访问验证地址查看验证码
Springboot整合kaptcha验证码的更多相关文章
- springboot 集成kaptcha验证码Demo
验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人 ...
- springboot集成kaptcha验证码
在pom.xml引入依赖 <!-- 验证码 --> <!-- https://mvnrepository.com/artifact/com.github.penggle/kaptch ...
- SpringBoot整合Shiro实现权限控制,验证码
本文介绍 SpringBoot 整合 shiro,相对于 Spring Security 而言,shiro 更加简单,没有那么复杂. 目前我的需求是一个博客系统,有用户和管理员两种角色.一个用户可能有 ...
- JAVA整合kaptcha生成验证码 (字母验证码和算术验证码)
引入maven <!--图片验证码--> <dependency> <groupId>com.github.penggle</groupId> < ...
- SpringBoot 整合 Shiro 密码登录与邮件验证码登录(多 Realm 认证)
导入依赖(pom.xml) <!--整合Shiro安全框架--> <dependency> <groupId>org.apache.shiro</group ...
- SpringMVC 整合 kaptcha(验证码功能)
一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...
- springboot(十四):springboot整合shiro-登录认证和权限管理(转)
springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...
- SpringBoot整合Redis---Jedis版
目录 介绍 开发环境 pom文件引入 创建redis.properties配置文件 创建RedisConfig配置类 创建RedisUtil工具类 使用 效果 介绍 Redis简介 Redis 是完全 ...
- SpringBoot整合Mail发送邮件&发送模板邮件
整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...
随机推荐
- css上下居中
position: absolute; top: 20%; left: 50%; transform: translateX(-50%); -ms-transform: translateX(-50% ...
- IDEA超级好用的插件推荐
IDEA超级好用的插件推荐 以下都是本人使用idea开发以来,所使用过的插件,强烈推荐,提升代码质量,事半功倍之首选!!! 先介绍下如何安装这些插件:(本人使用idea的版本是2020.2.3) 1. ...
- java 报错: MalformedURLException: unknow protocol: f
问题: java 使用 URL 读取文件解读报错 MalformedURLException 1.使用 URL对象解析文件报错MalformedURLException: unknow protoco ...
- [ARM汇编]常用ARM汇编指令
- 保存网页到zotero研究
打印长页 打印长页很麻烦,打印加载时间过长,打印后无法选取文字 https://www.zhihu.com/question/52639201?sort=created 插件 浏览器自带直接网页打印p ...
- 分享一个学习cesiumjs的中文社区
在cesiumjs中文社区的时间线中我写到: 2018年10月10日 注册用户数51,日uv破100 Mark截图 2018年06月22日 上线测试 2018年06月19日 获得cesiumcn.or ...
- golang中int、float、string数据类型之间的转换
package main import ( "fmt" "strconv" ) func main() { var num1 int = 88 var num2 ...
- Tomcat-如何创建动态的web工程及目录介绍
1,IDEA中如何创建动态web工程 (1)创建一个新模块 (2)选择你要创建什么类型的模块 (3)输入模块名,finish完成创建 创建成功如下图: 一般在WEB-INF下建一个lib目录 存放ja ...
- Error: xz compression not available解决办法
centos6升级php时误安装: rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm rpm -Uvh https://mir ...
- unixbench服务器性能测试
使用 Unixbench5 进行linux系统服务器性能综合测试 1.简介 Unixbench是一个类unix系(Unix,BSD,Linux)统下的性能测试工具,一个开源工具,被广泛用与测试linu ...