目录一、引入依赖二、编写配置类三、编写接口四、测试一、引入依赖!--kaptcha依赖包-- dependency groupIdcom.baomidou/groupId artifactIdkaptcha-spring-boot-starter/artifactId version1.1.0/version /dependency二、编写配置类package net.xdclass.config; import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; Configuration public class CaptchaConfig { /** * 验证码配置 * Kaptcha配置类名 * * return */ Bean Qualifier(captchaProducer) public DefaultKaptcha kaptcha() { DefaultKaptcha kaptcha new DefaultKaptcha(); Properties properties new Properties(); // properties.setProperty(Constants.KAPTCHA_BORDER, yes); // properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, 220,220,220); // //properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, 38,29,12); // properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, 147); // properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, 34); // properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, 25); // //properties.setProperty(Constants.KAPTCHA_SESSION_KEY, code); //验证码个数 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, 4); // properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, Courier); //字体间隔 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,8); //干扰线颜色 // properties.setProperty(Constants.KAPTCHA_NOISE_COLOR, white); //干扰实现类 properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, com.google.code.kaptcha.impl.NoNoise); //图片样式 properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, com.google.code.kaptcha.impl.WaterRipple); //文字来源 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, 0123456789); Config config new Config(properties); kaptcha.setConfig(config); return kaptcha; } }三、编写接口package net.xdclass.controller; import com.google.code.kaptcha.Producer; import lombok.extern.slf4j.Slf4j; import net.xdclass.service.NotifyService; import net.xdclass.util.JsonData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; RestController RequestMapping(/api/account/v1) Slf4j public class NotifyController { Autowired private NotifyService notifyService; Autowired private Producer captchaProducer; RequestMapping(send_code) public JsonData sendCode() { notifyService.testSend(); return JsonData.buildSuccess(); } /** * 生成验证码 * param request * param response */ GetMapping(captcha) public void getCaptcha(HttpServletRequest request, HttpServletResponse response){ String captchaText captchaProducer.createText(); log.info(验证码内容:{},captchaText); //存储redis,配置过期时间 jedis/lettuce //redisTemplate.opsForValue().set(getCaptchaKey(request),captchaText,CAPTCHA_CODE_EXPIRED,TimeUnit.MILLISECONDS); BufferedImage bufferedImage captchaProducer.createImage(captchaText); try (ServletOutputStream outputStream response.getOutputStream()){ ImageIO.write(bufferedImage,jpg,outputStream); outputStream.flush(); } catch (IOException e) { log.error(获取流出错:{},e.getMessage()); } } }四、测试