Springboot使用kaptcha生成验证码
<dependency> <groupId>com.youkol.support.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
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 defaultKaptcha=new DefaultKaptcha();
Properties properties=new Properties();
properties.setProperty("kaptcha.border", "no");
properties.setProperty("kaptcha.border.color", "34,114,200");
properties.setProperty("kaptcha.image.width", "200");
properties.setProperty("kaptcha.image.height", "50");
//properties.setProperty("kaptcha.textproducer.char.string", "0123456789");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "Arial,Arial Narrow,Serif,Helvetica,Tahoma,Times New Roman,Verdana");
properties.setProperty("kaptcha.textproducer.font.size", "38");
properties.setProperty("kaptcha.background.clear.from", "white");
properties.setProperty("kaptcha.background.clear.to", "white");
Config config=new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
import com.google.code.kaptcha.impl.DefaultKaptcha; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.TimeUnit; @Slf4j @Service public class CaptchaServiceImpl implements CaptchaService { @Resource RedisTemplate<String, String> redis0Template; @Resource DefaultKaptcha defaultKaptcha; private static final String captchaPrefix = "captcha:"; @Override public Map<String, String> captcha() { String kaptchaText = defaultKaptcha.createText(); BufferedImage image = defaultKaptcha.createImage(kaptchaText); String base64Code = ""; try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ImageIO.write(image, "jpg", outputStream); base64Code = Base64.encodeBase64String(outputStream.toByteArray()); } catch (Exception e) { log.error("error when generate captcha", e); } //uuid; 唯一标识code //code; 验证码图片的Base64串 Map<String, String> kaptchaVoMap = new HashMap<>(); String uuid = UUID.randomUUID().toString(); kaptchaVoMap.put("uuid", uuid); kaptchaVoMap.put("code", "data:image/png;base64," + base64Code); redis0Template.opsForValue().set(captchaPrefix + uuid, kaptchaText, 5, TimeUnit.MINUTES); return kaptchaVoMap; } @Override public boolean valid(String uuid, String code) { String kaptchaText = redis0Template.opsForValue().get(captchaPrefix + uuid); if (kaptchaText != null && kaptchaText.equalsIgnoreCase(code)) { redis0Template.delete(captchaPrefix + uuid); return true; } return false; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库