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的更多相关文章

  1. springboot中使用kaptcha验证码

    maven依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptc ...

  2. Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能

    Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能 背景 注册-登录-修改密码⼀般需要发送验证码,但是容易被 攻击恶意调⽤ 什么是短信-邮箱轰炸机 手机短信轰炸机是批.循环给 ...

  3. Spring MVC 中使用 Google kaptcha 验证码

    验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...

  4. 验证码 kaptcha 参数详解

    Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and optio ...

  5. SpringBoot之自定义验证码

    代码地址如下:http://www.demodashi.com/demo/14280.html 项目介绍 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控 ...

  6. Spring Boot2(十五):Shiro记住我rememberMe、验证码Kaptcha

    接着上次学习的<Spring Boot2(十二):手摸手教你搭建Shiro安全框架>,实现了Shiro的认证和授权.今天继续在这个基础上学习Shiro实现功能记住我rememberMe,以 ...

  7. Java Web项目使用图形验证码 — Kaptcha

    一.验证码介绍 生成的主要方式: 1.使用Java原生的方式,其中包含了Servlet.AWT.ImageIO的使用: 2.使用开源库,例如Jcaptcha.Kaptcha...: (各图形验证码开源 ...

  8. 如何让接口文档自动生成,SpringBoot中Swagger的使用

    目录 一.在SpringBoot项目中配置Swagger2 1.pom.xml中对Swagger2的依赖 2.编写配置类启用Swagger 3.配置实体类的文档 4.配置接口的文档 5.访问文档 二. ...

  9. springboot中RedisTemplate的使用

    springboot中RedisTemplate的使用 参考 了解 Redis 并在 Spring Boot 项目中使用 Redis--以IBM为学习模板 springboot之使用redistemp ...

随机推荐

  1. What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?

     What are the benefits to using anonymous functions instead of named functions for callbacks and par ...

  2. JavaScript Functional Programming:声明式与命令式

    函数式编程属于声明式编程(declarative programming)的范畴,经常跟声明式编程一块儿讨论的是命令式编程(imperative programming),因为它们是两种不太一样的风格 ...

  3. Java8 新特性之Stream

    1.Student package com.elk.log.myTest; public class Student implements Comparable<Student> { /* ...

  4. osg HUD 前景色

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...

  5. 123457123456#0#-----com.threeapp.MakerHanBao01----儿童汉堡制作游戏

    ----com.threeapp.MakerHanBao01----儿童汉堡制作游戏

  6. Java中四个作用域的可见范围

    作用域 当前类 同一包 子孙类 其他包 public √ √ √ √ protected √ √ √ × friendly或default(即没有修饰符时的权限) √ √ × × private √ ...

  7. iOS-GCD处理后台线程和UI线程的交互

    一个例子: 在iPhone上做一个下载网页的功能,就是:在iPhone上放一个按钮,单击按钮时,显示一个转动的圆圈,表示正在进行下载,下载完成后,将内容加载到界面上的一个文本控件上. 使用GCD前: ...

  8. VS2017.常量中有换行符

    1.VS中加入 “/utf-8” Qt中 也是加入“/utf-8”,加的地方注意下:在 pro文件中 ,这个位置加入: win32-msvc*:QMAKE_CXXFLAGS += /wd"4 ...

  9. 高级UI-NavigationView侧滑

    NavigationView是遵循MD设计规范的侧滑模式,推荐使用 要使用NavigationView,就需要引入support-design依赖 implementation 'com.androi ...

  10. 洛谷 题解 UVA10048 【噪音恐惧症 Audiophobia】

    [题意] 输入一个\(C\)个点\(S\)条边 \((C<=100)\) \((S<=1000)\)的无向带权图,边权表示该路径上的噪声值.当噪声太大时,耳膜可能会收到损伤,所以当你从某点 ...