1.maven依赖
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
2.配置
import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.Properties; @Component public class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha() { DefaultKaptcha defaultKaptcha = 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", "red"); // 图片宽 properties.setProperty("kaptcha.image.width", "130"); // 图片高 properties.setProperty("kaptcha.image.height", "40"); // 字体大小 properties.setProperty("kaptcha.textproducer.font.size", "30"); // session key properties.setProperty("kaptcha.session.key", "code"); // 验证码长度 properties.setProperty("kaptcha.textproducer.char.length", "4"); // 文字间距 properties.put("kaptcha.textproducer.char.space", "7"); // 字体 properties.put("kaptcha.textproducer.font.names", "Arial,cmr10"); // 采用那些文字 properties.put("kaptcha.textproducer.char.string", "ABCDEFGHJKLMNPQRSTUVWXYZ13456789abcdeghjknsuvwxyz"); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
3.接口
@Controller @RequestMapping("kaptcha") @Slf4j @Api(tags = {"验证码生成接口"}) public class KaptchaController { /** * 1、验证码工具 */ @Autowired DefaultKaptcha defaultKaptcha; /** * 生成验证码 * @param request * @param response * @throws Exception */ @ApiOperation(value = "生成验证码") @GetMapping("generateKaptcha") public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception { byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = null; ServletOutputStream responseOutputStream = null; try { jpegOutputStream = new ByteArrayOutputStream(); // 生产验证码字符串并保存到session中 String createText = defaultKaptcha.createText(); log.info("sessionId={},验证码={}", request.getSession().getId(), createText); request.getSession().setAttribute(Constant.VERIFY_CODE, createText); // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中 BufferedImage challenge = defaultKaptcha.createImage(createText); ImageIO.write(challenge, "jpg", jpegOutputStream); // 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组 captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); responseOutputStream = response.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); } catch (IllegalArgumentException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } finally { IOUtils.closeQuietly(jpegOutputStream); IOUtils.closeQuietly(responseOutputStream); } } }