springboot中使用验证码kaptcha
1.pom.xml引入kaptcha所需要的jar包
<!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.添加KaptchaConfig类
package com.demo.common; import java.util.Properties; import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config; @Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "yes");
// 边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "red");
// 图片宽
properties.setProperty("kaptcha.image.width", "110");
// 图片高
properties.setProperty("kaptcha.image.height", "40");
// 字体大小
properties.setProperty("kaptcha.textproducer.font.size", "30");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字体
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config); return defaultKaptcha;
}
}
3.添加获取验证码的COntroller
package com.demo.controller; import java.awt.image.BufferedImage;
import java.io.IOException; import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer; @Controller
@RequestMapping(value = "/captcha")
public class CaptchaController { private Producer captchaProducer = null; @Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
} /**
*
* 获取验证码图片
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping("getCaptchaCode")
public ModelAndView getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
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();
}
return null;
}
}
4.前台配置(form表单中添加验证码,我使用的是thymeleaf模板引擎,语法可参考官网https://www.thymeleaf.org/)
<form class="registerform" id="form1">
<div class="fm-item">
<label for="logonId" class="form-label">登陆账号:</label>
<input type="text" maxlength="100" id="username" name="username" class="i-text"/>
<div class="ui-form-explain"></div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label">登陆密码:</label>
<input type="password" maxlength="100" id="password" name="password" class="i-text"/>
<div class="ui-form-explain"></div>
</div>
<div class="fm-item pos-r">
<label for="logonId" class="form-label">验证码</label>
<input type="text" maxlength="100" id="yzm" name="yzm" class="i-text yzm"/>
<div class="ui-form-explain">
<img id="captchaImg" alt="" th:src="@{/captcha/getCaptchaCode}" class="yzm-img" style="width: 100px;height: 40px;line-height:40px;"/>
</div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label"></label>
<input type="button" value="" tabindex="4" id="submit" class="btn-login"/>
<div class="ui-form-explain"></div>
</div>
</form>
5.js部分 点击验证码获取新的验证码
//获取新验证码
$('#captchaImg').click(function() {
$(this).attr('src', '/captcha/getCaptchaCode.jpg');
});
springboot中使用验证码kaptcha的更多相关文章
- springboot中使用kaptcha验证码
		maven依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptc ... 
- Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能
		Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能 背景 注册-登录-修改密码⼀般需要发送验证码,但是容易被 攻击恶意调⽤ 什么是短信-邮箱轰炸机 手机短信轰炸机是批.循环给 ... 
- Spring MVC 中使用 Google kaptcha 验证码
		验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ... 
- 验证码 kaptcha 参数详解
		Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and optio ... 
- SpringBoot之自定义验证码
		代码地址如下:http://www.demodashi.com/demo/14280.html 项目介绍 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控 ... 
- Spring Boot2(十五):Shiro记住我rememberMe、验证码Kaptcha
		接着上次学习的<Spring Boot2(十二):手摸手教你搭建Shiro安全框架>,实现了Shiro的认证和授权.今天继续在这个基础上学习Shiro实现功能记住我rememberMe,以 ... 
- Java Web项目使用图形验证码 — Kaptcha
		一.验证码介绍 生成的主要方式: 1.使用Java原生的方式,其中包含了Servlet.AWT.ImageIO的使用: 2.使用开源库,例如Jcaptcha.Kaptcha...: (各图形验证码开源 ... 
- 如何让接口文档自动生成,SpringBoot中Swagger的使用
		目录 一.在SpringBoot项目中配置Swagger2 1.pom.xml中对Swagger2的依赖 2.编写配置类启用Swagger 3.配置实体类的文档 4.配置接口的文档 5.访问文档 二. ... 
- springboot中RedisTemplate的使用
		springboot中RedisTemplate的使用 参考 了解 Redis 并在 Spring Boot 项目中使用 Redis--以IBM为学习模板 springboot之使用redistemp ... 
随机推荐
- oracle中的trigger
			https://blog.csdn.net/indexman/article/details/8023740/ https://www.cnblogs.com/sharpest/p/7764660.h ... 
- redis  json   降低性能 使用 hash
			使用hashtable和hash-max-zipmap-entries内存优化和效率http://www.flyfifi.cn/blog/detail/71/ 
- <HTML/CSS>BFC原理剖析
			本文讲了BFC的概念是什么: BFC的约束规则:咋样才能触发生成新的BFC:BFC在布局中的应用:防止margin重叠(塌陷,以最大的为准): 清除内部浮动:自适应两(多)栏布局. 1. BFC是什么 ... 
- MeasureSpec常用方法
			package com.loaderman.customviewdemo; import android.content.Context; import android.util.AttributeS ... 
- ISO/IEC 9899:2011 条款6.4.7——头文件名
			6.4.7 头文件名 语法 1.header-name: < h-char-sequence > " q-char-sequence " h-c ... 
- 011-多线程-基础-基于AbstractQueuedSynchronizer自定义同步组件
			一.概述 队列同步器AbstractQueuedSynchronizer,是用来构建锁或者其他同步组件的基础框架. 1.1.自定义独占锁同步组件 设计一个同步工具:该工具在同一时刻,只允许一个线程访问 ... 
- OSG 遍历fbx节点
			count:560 construction_worker 4294967295 osg::MatrixTransform1 Bip001 L Finger02 4294967295 osg::Mat ... 
- javascript——语法 && 结构
			原文链接:Understanding Syntax and Code Structure 
- building confluentinc kafka-connect-hdfs
			When I try to compile I get an error about a missing SNAPSHOT dependency. The error looks something ... 
- 点击链接,取得href的值,但是不转向
			点击链接,取得href的值,但是不转向 $('.list a').click(function (e) { e.preventDefault();//取消事件的默认动作. $.ajax({ ... 
