Spring boot2.2集成kaptcha验证码插件
2020-11-08 15:15:46
一个简单易用的验证码可以有效拦截90%的网络机器人,无聊的密码嗅探者。
采用字符图片验证码方式的话,最受欢迎的方案就是kaptcha了,Kaptcha 是一个可高度配置的字符验证码生成工具,可自由配置的选项如:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小,边框,边框粗细,边框颜色
验证码的干扰线
验证码的样式(鱼眼样式、3D、普通模糊、...)
下图是集成字符验证码的效果,虽然现在字符验证码的方式被破解的程度很高,可以说人眼看不清的程序都识别出来了,但是还是可以拦住那90%的。
Spring boot 版本 2.2.10.RELEASE
查看kaptcha版本
https://mvnrepository.com/artifact/com.github.penggle/kaptcha
一)添加maven依赖
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
二)kaptcha配置类
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; @Configuration public class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha(){ DefaultKaptcha captchaProducer = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", "no"); properties.setProperty("kaptcha.border.color", "105,179,90"); properties.setProperty("kaptcha.textproducer.font.color", "blue"); properties.setProperty("kaptcha.image.width", "110"); properties.setProperty("kaptcha.image.height", "36"); properties.setProperty("kaptcha.textproducer.font.size", "30"); properties.setProperty("kaptcha.session.key", "code"); properties.setProperty("kaptcha.textproducer.char.length", "4"); properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCEFGHIJKLMNOPQRSTUVWXYZ"); properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple"); properties.setProperty("kaptcha.noise.color", "black"); // properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise"); properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise"); properties.setProperty("kaptcha.background.clear.from", "232,240,254"); properties.setProperty("kaptcha.background.clear.to", "232,240,254"); properties.setProperty("kaptcha.textproducer.char.space", "3"); Config config = new Config(properties); captchaProducer.setConfig(config); return captchaProducer; } }
三)生成图片的控制器
import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.Producer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; @Controller @Slf4j public class KaptchaController { @Autowired private Producer captchaProducer = null; @RequestMapping("/kaptcha") public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { 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); try(ServletOutputStream out = response.getOutputStream()){ ImageIO.write(bi, "jpg", out); } } }
四)页面显示验证码图片
<div class="form-group"> <div class="input-group"> <input class="form-control" type="text" autocomplete="new-password" placeholder="验证码" required maxlength="4" v-model="verifyCode"> <span class="input-group-btn"> <img id="captcha_img" alt="验证码" title="点击更换" onclick="refreshKaptcha()" src="/kaptcha" /> </span> </div> </div>
更新验证码
function refreshKaptcha() { document.getElementById('captcha_img').src="/kaptcha?"+ Math.random(); }
五)验证校验码
// 获取session中生成的校验码 String kaptchaCode = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); // 获取页面提交的验证码 String verifyCode = request.getParameter("verifyCode"); //校验验证码 if (!StringUtils.equalsIgnoreCase(verifyCode, kaptchaCode)){ throw new Exception("校验码错误!"); }
这样kaptcha集成好了。
其他文章
- 推荐 1810
- 阅读 97
- 点赞 2
- 分享 0
- 评论 0
- 推荐 1205
- 阅读 35
- 点赞 0
- 分享 0
- 评论 0
- 推荐 3026
- 阅读 184
- 点赞 8
- 分享 0
- 评论 0
- 推荐 1085
- 阅读 45
- 点赞 2
- 分享 0
- 评论 0
- 推荐 1598
- 阅读 102
- 点赞 7
- 分享 0
- 评论 0
- 推荐 507
- 阅读 93
- 点赞 4
- 分享 1
- 评论 0
- 推荐 847
- 阅读 180
- 点赞 1
- 分享 0
- 评论 0