springboot使用谷歌验证码Captcha
依赖
<!-- 谷歌 Captcha验证码依赖 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
配置
package com.zl.config;
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 producer(){
Properties properties = new Properties();
// 是否有边框
properties.put("kaptcha.border", "no");
// 设置验证码文本字符颜色,默认为黑色
properties.put("kaptcha.textproducer.font.color", "black");
// 字体大小,默认为40
properties.put("kaptcha.textproducer.font.size","25");
// 字符间距,默认为2
properties.put("kaptcha.textproducer.char.space", "10");
// 字符长度,默认为5
properties.put("kaptcha.textproducer.char.length","4");
// 验证码图片高度,默认为40
properties.put("kaptcha.image.height","34");
// 验证码图片宽度,默认为200
properties.put("kaptcha.image.width",180);
// 干扰实现
properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
Config config = new Config(properties);
// 验证码生成器
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
配置参数
//图片边框,合法值yes,no,默认值yes
properties.put("kaptcha.border","no");
//边框颜色,合法值rgb(and optional alpha)或者 white,black,blue,默认值black
properties.put("kaptcha.border.color","blue");
//边框厚度,合法值>0,默认值为1
properties.put("kaptcha.border.thickness",2);
//图片宽度,默认值200
properties.put("kaptcha.image.width",200);
//图片高度,默认值50
properties.put("kaptcha.image.height",50);
//图片实现类,默认值priv.kerlomz.kaptcha.impl.DefaultKaptcha
properties.put("kaptcha.producer.impl","priv.kerlomz.kaptcha.impl.DefaultKaptcha")
//文本实现类,默认值priv.kerlomz.kaptcha.impl.DefaultTextCreator
properties.put("kaptcha.textproducer.impl","priv.kerlomz.kaptcha.text.impl.DefaultTextCreator")
//文本集合,验证码值从此集合中获取,默认值abcde2345678gfynmnpwx
properties.put("kaptcha.textproducer.char.string","abcde2345678gfynmnpwx")
//验证码长度,默认值为5
properties.put("kaptcha.textproducer.char.length","5")
//字体,默认值Arial, Courier(如果使用中文验证码,则必须使用中文的字体,否则出现乱码)
properties.put("kaptcha.textproducer.font.names","Arial")
//字体大小,默认值为40px
properties.put("kaptcha.textproducer.font.size","40")
//字体颜色,合法值: r,g,b 或者 white,black,blue,默认值black
properties.put("kaptcha.textproducer.font.color","black")
//文字间隔,默认值为2
properties.put("kaptcha.textproducer.char.space","2")
//干扰实现类,默认值priv.kerlomz.kaptcha.impl.DefaultNoise
properties.put("kaptcha.noise.impl","priv.kerlomz.kaptcha.impl.DefaultNoise")
//干扰 颜色,合法值: r,g,b 或者 white,black,blue,默认值black
properties.put("kaptcha.noise.color","black")
/**图片样式:
水纹 priv.kerlomz.kaptcha.impl.WaterRipple
鱼眼 priv.kerlomz.kaptcha.impl.FishEyeGimpy
阴影 priv.kerlomz.kaptcha.impl.ShadowGimpy, 默认值水纹
**/
properties.put("kaptcha.obscurificator.impl","priv.kerlomz.kaptcha.impl.WaterRipple")
//背景实现类,默认值priv.kerlomz.kaptcha.impl.DefaultBackground
properties.put("kaptcha.background.impl","priv.kerlomz.kaptcha.impl.DefaultBackground")
//背景颜色渐变,开始颜色,默认值lightGray/192,193,193
properties.put("kaptcha.background.clear.from","255,255,255")
//背景颜色渐变, 结束颜色,默认值white
properties.put("kaptcha.background.clear.to","white")
//文字渲染器,默认值priv.kerlomz.kaptcha.text.impl.DefaultWordRenderer
properties.put("kaptcha.word.impl","priv.kerlomz.kaptcha.text.impl.DefaultWordRenderer")
实现
controller
/**
* 获取验证码
*/
@GetMapping("/getVerificationCode")
@ApiOperation("获取验证码")
public void getVerificationCode(HttpServletResponse response){
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
// 生成图片验证码
BufferedImage image = UserService.createVerificationCode();
try {
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(image,"jpg",outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 用户登录
*/
@PostMapping("/login")
@ApiOperation("用户登录")
public Result login(@RequestBody UserVO user) {
// 验证码校验
if(!UserService.validateCode(user.getCode())){
return Result.error("验证码错误");
}
...
}
service
package com.zl.service.impl;
import com.google.code.kaptcha.Producer;
import com.zl.util.RedisUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Resource
private Producer producer;
@Resource
private RedisUtil redisUtil;
public BufferedImage createVerificationCode() {
// 生成文字验证码
String code = producer.createText();
// 存储验证码
redisUtil.set(LoginConstants.CAPTCHA_PREFIX+code,code);
return producer.createImage(code);
}
public boolean validateCode(String code) {
String key=LoginConstants.CAPTCHA_PREFIX+code;
if(redisUtil.exists(key)){
// 获取验证码
String redisCode = redisUtil.get(key);
if(redisCode.equals(code)){
return true;
}
}
return false;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库