springboot集成kaptcha验证码
在pom.xml引入依赖
<!-- 验证码 -->
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
配置类KaptchaConfig.java
package com.xiaostudy.shiro_test1.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; /**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 2019/7/17
* Time: 23:46
* Description: No Description
*/
@Configuration
public class KaptchaConfig { @Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
// 图片边框,合法值:yes[默认] , no
properties.setProperty("kaptcha.border", "yes");
// 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.默认black
properties.setProperty("kaptcha.border.color", "105,179,90");
// 边框厚度,合法值:>0,默认1
// properties.setProperty("kaptcha.border.thickness", "1");
// 图片宽,默认200
properties.setProperty("kaptcha.image.width", "110");
// 图片高,默认50
properties.setProperty("kaptcha.image.height", "40");
// 字体大小,默认40px
properties.setProperty("kaptcha.textproducer.font.size", "30");
// 字体,默认Arial, Courier
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
// 字体颜色,合法值: r,g,b 或者 white,black,blue.默认black
properties.setProperty("kaptcha.textproducer.font.color", "blue");
// session key,默认KAPTCHA_SESSION_KEY
properties.setProperty("kaptcha.session.key", "code");
// session date,默认KAPTCHA_SESSION_DATE
// properties.setProperty("kaptcha.session.date", "KAPTCHA_SESSION_DATE");
// 验证码长度,默认5
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 文字间隔,默认2
// properties.setProperty("kaptcha.textproducer.char.space", "2");
// 干扰 颜色,合法值: r,g,b 或者 white,black,blue.默认black
// properties.setProperty("kaptcha.noise.color", "black");
// 更多可参考:https://blog.csdn.net/elephantboy/article/details/52795309 Config config = new Config(properties);
defaultKaptcha.setConfig(config); return defaultKaptcha;
}
}
html使用
<img alt="验证码" onclick = "this.src='/defaultKaptcha?d='+new Date().getTime()" src="/defaultKaptcha" />
验证码请求
/**
* 验证码请求
* @param response
* @param session
* @throws Exception
*/
@RequestMapping("/defaultKaptcha")
public void defaultKaptcha(HttpServletResponse response,HttpSession session) {
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
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
try {
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
登录认证验证码
// 从session中拿到正确的验证码
String captchaId = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
// 用户输入的验证码
String parameter = request.getParameter("vrifyCode");
// 下面就是匹配这两个是否相同
springboot集成kaptcha验证码的更多相关文章
- springboot 集成kaptcha验证码Demo
验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人 ...
- Springboot整合kaptcha验证码
01.通过配置类来配置kaptcha 01-01.添加kaptcha的依赖: <!-- kaptcha验证码 --> <dependency> <groupId>c ...
- Spring Boot快速集成kaptcha生成验证码
Kaptcha是一个非常实用的验证码生成工具,可以通过配置生成多样化的验证码,以图片的形式显示,从而无法进行复制粘贴:下面将详细介绍下Spring Boot快速集成kaptcha生成验证码的过程. 本 ...
- SpringBoot集成Spring Security(4)——自定义表单登录
通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ...
- 使用springboot集成腾讯云短信服务,解决配置文件读取乱码问题
springboot集成腾讯云短信服务: (1)导入依赖 <dependency> <groupId>org.springframework.boot</groupId& ...
- Java实现验证码制作之一Kaptcha验证码
Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar 下载地址:http:/ ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- kaptcha验证码插件的使用
kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...
- kaptcha 验证码组件使用
kaptcha 验证码组件使用简介 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.co ...
随机推荐
- 洛谷 P3367 并查集模板
#include<cstdio> using namespace std; int n,m,p; ]; int find(int x) { if(father[x]!=x) father[ ...
- read()和write(),读和写的优化。
读和写的优化在输入数据后输出数据十分多的情况下是十分有用的,比scanf和printf也要快. 读: int read(){ ; ; char c=getchar(); '){ if(c=='-') ...
- 交互设计算法基础(1) - Binary Search
int binary_search(int[] list, int item) { int low = 0; int high = list.length-1; while (low <= hi ...
- AtCoder Grand Contest 003题解
传送门 \(A\) 咕咕 const int N=1005; char s[N];int val[N],n; int main(){ scanf("%s",s+1),n=strle ...
- WARNING: You are using pip version 19.1.1, however version 19.2.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
pip3 install --upgrade pip
- 模板 - 字符串 - KMP算法
要先理解前缀函数的定义,前缀函数 \(\pi(i)\) 表示字符串 \(s[0,i]\) 的同时是其最长真前缀及最长真后缀的长度,简单来说就是这个 \(s[0,i]\) 首尾最长的重叠长度(不能完全重 ...
- Lock接口的认识和使用
保证线程安全演进: synchronized volatile AtomicInteger Lock接口提供的方法: void lock():加锁 void unlock():解锁 void lock ...
- NoSql数据库MongoDB系列(1)——MongoDB简介
一.NoSQL简介 NoSQL(Not Only SQL ),意即“不仅仅是SQL” ,指的是非关系型的数据库 .是一项全新的数据库革命性运动,早期就有人提出,发展至2009年趋势越发高涨.No ...
- MFC GDI+显示GIF文件《转》
在头文件里面添加: Image* image; GUID Guid ; UINT frameCount; UINT framePos;ULONG_PTR gdiplusToken; afx_msg v ...
- elasticsearch 的入门
参考文档 1.全文搜索引擎 Elasticsearch 入门教程(http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html) 2.elasti ...