100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java 验证码_Java - 验证码 - 由Kaptcha组件实现

java 验证码_Java - 验证码 - 由Kaptcha组件实现

时间:2023-06-15 21:35:08

相关推荐

java 验证码_Java - 验证码 - 由Kaptcha组件实现

本文是基于SpringBoot整合Kaptcha验证码实现

Kaptcha 是一个可高度配置的实用验证码生成工具,在项目开发中能够非常方便实现验证码

先来看一个由 Kaptcha 制作的验证码效果图

快速进入如何进行配置与实现的

第1步:配置 Kaptcha 的依赖库

com.github.penggle kaptcha 2.3.2

第2步:编写配置类

package com.dingsheng.configuration;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;/** * 验证码 - Kaptcha - 插件 */@Configurationpublic class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha() { DefaultKaptcha captchaProducer = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", "yes"); properties.setProperty("kaptcha.border.color", "105,179,90"); properties.setProperty("kaptcha.textproducer.font.color", "blue"); properties.setProperty("kaptcha.image.width", "100"); properties.setProperty("kaptcha.image.height", "38"); 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", "宋体,楷体,微软雅黑"); Config config = new Config(properties); captchaProducer.setConfig(config); return captchaProducer; }}

第3步:编写KaptchaController

package com.dingsheng.controller;import com.google.code.kaptcha.Constants;import com.google.code.kaptcha.Producer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;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;import java.io.IOException;@Controllerpublic class KatchaController { @Autowired private Producer producer = null; @GetMapping("/captcha") public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // 浏览器和任何中继的Web代理,都不会存储这次相应的数据 response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); // 生成验证码 String capText = producer.createText(); session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); // 输出验证码 BufferedImage bi = producer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); out.flush(); out.close(); }}

第4步:页面引入验证码的请求地址

通过上面的4个步骤,即可以实现由 Kaptcha 制作的验证码

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。