一、依赖
<!-- 验证码 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
二、配置
import com.google.code.kaptcha.Producer; 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 Producer kaptchaProducer(){ Properties properties = new Properties(); properties.setProperty("kaptcha.image.width","100"); properties.setProperty("kaptcha.image.height","40"); properties.setProperty("kaptcha.textproducer.font.size","32"); properties.setProperty("kaptcha.textproducer.font.color","black"); properties.setProperty("kaptcha.textproducer.char.string","0123456789QWERTYUIOPASSDFGHJKLZXCVBNM");//文本集合 properties.setProperty("kaptcha.textproducer.char.length","4");//验证码长度 properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise"); //选择哪个干扰类 DefaultKaptcha kaptcha = new DefaultKaptcha(); Config config = new Config(properties); kaptcha.setConfig(config); return kaptcha; } }
三、获取验证码
import cn.hutool.core.codec.Base64; import org.springframework.util.FastByteArrayOutputStream; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.util.UUID; import java.util.concurrent.TimeUnit; @GetMapping("/kaptcha") public AjaxResult getKaptcha() throws IOException { String uuid = UUID.randomUUID().toString(); //生成验证码 String text = kaptchaProduce.createText(); //将验证码保存到redis,有效期5分钟 redisService.set(RedisKey.getKaptcha(uuid),text,5, TimeUnit.MINUTES); //将验证码转换为图片 BufferedImage image = kaptchaProduce.createImage(text); // 转换流信息写出 FastByteArrayOutputStream os = new FastByteArrayOutputStream(); ImageIO.write(image, "jpg", os); Map<String,String> res=new HashMap<>(); res.put("kaptchaNum",uuid); res.put("kaptcha",text); res.put("img", "data:image/jpeg;base64,"+Base64.encode(os.toByteArray())); return toAjax(res); }
四、登录比较验证码
public AjaxResult login(LoginVo loginVO) throws IOException { //验证码校验 @NotBlank String kaptchaNum = loginVO.getKaptchaNum(); @NotBlank String kaptcha = loginVO.getKaptcha(); String kaptchaReids = redisService.get(RedisKey.getKaptcha(kaptchaNum)); if (kaptchaReids==null) { return AjaxResult.error("验证码不存"); } if (!kaptcha.equals(kaptchaReids)) { return AjaxResult.error("验证码错误"); } redisService.delete(RedisKey.getKaptcha(kaptchaNum)); @NotBlank @Length(min = 6, max = 20) String username = loginVO.getUsername(); User user = userMapper.login(username); if (user == null) { return AjaxResult.error("用户不存在"); } if (user != null && user.getPassword() != null) { if (PasswordUtil.checkPassword(user.getSalt(), loginVO.getPassword(), user.getPassword())) { JwtInfo jwtToken = JwtTool.createJwtToken(user.getId()); redisService.set(RedisKey.getToken(jwtToken.getJwtId()), jwtToken, tokentime, TimeUnit.DAYS); //查出敏感信息 user.setPassword(null); user.setSalt(null); HashMap<String, Object> hm = new HashMap<>(1); hm.put("token", jwtToken.getToken()); hm.put("user", user); return AjaxResult.success("登录成功", hm); } return AjaxResult.error("密码错误"); } return AjaxResult.error("登录失败"); }