哈尔滨cms模板建站,网站按关键词显示广告图片,单位网站建设情况,北塘网站制作Kaptcha 文章目录 Kaptcha介绍插件使用介绍原理引入依赖生成验证码 验证码小项目初始化前端代码约定前后端交互接口接口定义 介绍
Kaptcha 是Google的⼀个⾼度可配置的实⽤验证码⽣成⼯具
https://code.google.com/archive/p/kaptcha
⽹上有很多⼈甚⾄公司基于Google的kaptc…Kaptcha 文章目录 Kaptcha介绍插件使用介绍原理引入依赖生成验证码 验证码小项目初始化前端代码约定前后端交互接口接口定义 介绍
Kaptcha 是Google的⼀个⾼度可配置的实⽤验证码⽣成⼯具
https://code.google.com/archive/p/kaptcha
⽹上有很多⼈甚⾄公司基于Google的kaptcha进⾏了⼆次开发. 我们选择⼀个直接适配SpringBoot的开源项⽬
oopsguy/kaptcha-spring-boot: Kaptcha Spring Boot Starter help you use Google Kaptcha with Spring Boot easier. 一个简单封装了 Kaptcha 验证码库的 Spring Boot Starter (github.com)
插件使用介绍
基于GitHub项目来做介绍
原理
验证码可以客⼾端⽣成, 也可以服务器⽣成. 对于普通的字符验证码, 后端通常分两部分.
⼀是⽣成验证码内容, 根据验证码内容和⼲扰项等, ⽣成图⽚, 返回给客⼾端
⼆是把验证码内容存储起来, 校验时取出来进⾏对⽐.
kaptcha插件选择把验证码存储在Session⾥.
引入依赖
dependencygroupIdcom.oopsguy.kaptcha/groupIdartifactIdkaptcha-spring-boot-starter/artifactIdversion1.0.0-beta-2/version
/dependency生成验证码
该插件提供了两种⽅式⽣成验证码 通过代码来⽣成(参考⽂档: https://github.com/oopsguy/kaptcha-spring-boot/blob/master/README_zh-CN.md, 不再介绍) 仅通过配置⽂件来⽣成验证码(推荐)
部分详细配置
配置项配置说明默认值border设置验证码边框的颜色“255,255,255,255”textProducerCharLength设置生成验证码的字符长度4textProducerCharSpace设置生成验证码时字符之间的间距5imageWidth设置验证码图片的宽度200imageHeight设置验证码图片的高度50noiseColor设置噪点的颜色“255,255,255,255”fontColor设置字体颜色“0,0,0,255”fontNames设置字体的名称可以指定多个字体Arial, CouriertextProducerImpl设置生成验证码文本的实现类默认实现类backgroundProducerImpl设置生成验证码背景的实现类默认实现类noiseProducerImpl设置生成验证码噪点的实现类默认实现类wordImpl设置生成验证码字符的实现类默认实现类obscurificatorImpl设置字符模糊处理的实现类默认实现类sessionKey设置存储验证码信息的 Session 键默认值sessionDate设置存储验证码创建时间的 Session 键默认值width设置验证码图片的宽度同 imageWidth200height设置验证码图片的高度同 imageHeight50
验证码小项目
初始化前端代码
// success.html
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0title验证成功页/title
/head
bodyh1验证成功/h1
/body
/html//index.html
!DOCTYPE html
html langenheadmeta charsetutf-8title验证码/titlestyle#inputCaptcha {height: 30px;vertical-align: middle;}#verificationCodeImg{vertical-align: middle;}#checkCaptcha{height: 40px;width: 100px;}/style/headbodyh1输入验证码/h1div idconfirminput typetext nameinputCaptcha idinputCaptchaimg idverificationCodeImg src/admin/captcha stylecursor: pointer; title看不清换一张 /input typebutton value提交 idcheckCaptcha/divscript srchttps://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js/scriptscript$(#verificationCodeImg).click(function(){$(this).hide().attr(src, /admin/captcha?dt new Date().getTime()).fadeIn();});$(#checkCaptcha).click(function () {alert(验证码校验);});/script/body/html约定前后端交互接口
需求分析
后端需要提供两个服务 ⽣成验证码, 并返回验证码 校验验证码是否正确: 校验验证码是否正确
接口定义 生成验证码 请求 GET/admin/captcha响应图片 校验验证码是否正确 请求/admin/check POST /admin/checkcaptchaxn8dcaptcha用户输入的验证码 响应 true根据用户输入的验证码校验验证码是否正确
完善前端代码
!DOCTYPE html
html langenheadmeta charsetutf-8title验证码/titlestyle#inputCaptcha {height: 30px;vertical-align: middle;}#verificationCodeImg{vertical-align: middle;}#checkCaptcha{height: 40px;width: 100px;}/style/headbodyh1输入验证码/h1div idconfirminput typetext nameinputCaptcha idinputCaptchaimg idverificationCodeImg src/admin/captcha stylecursor: pointer; title看不清换一张 /input typebutton value提交 idcheckCaptcha/divscript srchttps://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js/scriptscript$(#verificationCodeImg).click(function(){$(this).hide().attr(src, /admin/captcha?dt new Date().getTime()).fadeIn();});$(#checkCaptcha).click(function () {$.ajax({type:get,url:/admin/check,data:{inputCaptcha:$(#inputCaptcha).val()},success:function (result){if (result){// 页面跳转location.hrefsuccess.html}else {alert(验证码失败请重试);}}});});/script/body/html后端代码
package org.haobin.kaptcha;import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpSession;
import java.util.Date;RestController
RequestMapping(/admin)
public class KaptchaController {public static final String KAPTCHA_SESSION_KEY KAPTCHA_SESSION_KEY;public static final String KAPTCHA_SESSION_DATE KAPTCHA_SESSION_DATE;public static final long TIME_OUT 60 * 1000; // 1min 毫秒/*** 校验验证码是否正确** param inputCaptcha 用户输入的验证码* return*/RequestMapping(/check)public boolean check(String inputCaptcha, HttpSession session) {// 1. 判断输入的验证码是否为空if (!StringUtils.hasLength(inputCaptcha)) {return false;}// 2. 获取生成的验证码// 正确的验证码生成验证码// 3. 比对 生成的验证码和输入的验证码是否一致// 4. 确认验证码是否过期String saveCaptcha (String) session.getAttribute(KAPTCHA_SESSION_KEY);Date saveCaptchaDate (Date) session.getAttribute(KAPTCHA_SESSION_DATE);// 不区分大小写if (inputCaptcha.equalsIgnoreCase(saveCaptcha)) {if (saveCaptchaDate ! null || System.currentTimeMillis() - saveCaptchaDate.getTime() TIME_OUT) {return true;}}return false;}
}
文章转载自: http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.sqdjn.cn.gov.cn.sqdjn.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.dfkby.cn.gov.cn.dfkby.cn http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.snnb.cn.gov.cn.snnb.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn