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 ...
随机推荐
- SQL-W3School-函数:SQL MID() 函数
ylbtech-SQL-W3School-函数:SQL MID() 函数 1.返回顶部 1. MID() 函数 MID 函数用于从文本字段中提取字符. SQL MID() 语法 SELECT MID( ...
- Ionic4.x 中的 UI 组件(UI Components)表单相关组件
1.ion-input 单行文本框 2.ion-toggle 开关 3.ion-radio-group.ion-radio 单选按钮组 4.ion-checkbox 多选按钮组 5.ion-selec ...
- SurfaceView动态背景效果实现
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.*; imp ...
- 【转载】 迁移学习简介(tranfer learning)
原文地址: https://blog.csdn.net/qq_33414271/article/details/78756366 土豆洋芋山药蛋 --------------------------- ...
- windows通过企业内部授权服务器激活方法
OFFICE KMS 激活方法: # 进入office安装目录 cscript ospp.vbs /sethst:kms.domain.com #设置KMS服务器 cscript ospp.vbs / ...
- Flutter 中使用Future消除Callback Hell
/先分别定义各个异步任务 Future<String> login(String userName, String pwd){ ... //用户登录 }; Future<String ...
- 采购信息记录批导BAPI
转自:https://www.cnblogs.com/freeandeasy/p/11810272.html作者的话: 可以批导创建及修改信息记录的主数据.而且可以对条件中的时间段及其数量等级中的 ...
- C# Newtonsoft.Json JObject 操作
C# Newtonsoft.Json JObject 操作举例 JArray j = new JArray(); JObject obj = new JObject( ") ); JObje ...
- python迭代器、生成器、装饰器之装饰器
装饰器...... 定义:本质是函数,为其他函数添加附加功能 原则: 1.不能修改被装饰的函数的源代码 2.不能修改被装饰函数的调用方式 仔细观察下面代码,看看有什么发现. 内嵌函数+高阶函数+闭包= ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...