springboot+kaptcha demo

第一步:pom文件中添加

        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

第二步:添加配置类KaptchaConfig

@Configuration
public class KaptchaConfig {

    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
        Properties properties = new Properties();
        // 图片边框
        properties.setProperty("kaptcha.border", "yes");
        // 边框颜色
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "blue");
        // 图片宽
        properties.setProperty("kaptcha.image.width", "110");
        // 图片高
        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.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

}

第三步:CaptchaController

@RestController
@RequestMapping(path = "/controller/captcha", produces = { "application/json;charset=UTF-8" })
public class CaptchaController {

	private static final Logger logger = LoggerFactory.getLogger(CaptchaController.class);

	@Autowired
	DefaultKaptcha defaultKaptcha;

	@GetMapping("/kaptcha")
	public String generateKaptcha(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("coming into generateKaptcha method");
		String jpg_base64 = null;
		try {
			byte[] captchaChallengeAsJpeg = null;
			ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
			String createText = defaultKaptcha.createText();
			request.getSession().setAttribute("vrifyCode", createText);
			BufferedImage challenge = defaultKaptcha.createImage(createText);
			ImageIO.write(challenge, "jpg", jpegOutputStream);
			captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
			BASE64Encoder encoder = new BASE64Encoder();
			jpg_base64 = encoder.encodeBuffer(captchaChallengeAsJpeg).trim();
			jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");
		} catch (IOException e) {
			logger.error("generate captcha failed", e);
			return CommonUtil.errorJsonStr(ErrorEnum.E_514);
		}
		return CommonUtil.successJsonStr(jpg_base64);
	}

	@GetMapping("/vrifyKaptcha")
	public @ResponseBody String vrifyKaptcha(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
		logger.debug("coming into generateKaptcha method");
		String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode");
		String parameter = httpServletRequest.getParameter("vrifyCode");
		if (!captchaId.equals(parameter)) {
			logger.error("vrity captcha failed");
			return CommonUtil.errorJsonStr(ErrorEnum.E_511);
		} else {
			logger.debug("vrity captcha success");
			return CommonUtil.successJsonStr();
		}
	}

}

springboot+kaptcha返回base64编码的jpg格式的图片,验证验证码是否正确的接口。

前端接收方式:img标签

    <img id = "base64" src="https://img-blog.csdnimg.cn/2022010703424228277.gif" />

 

转载于:https://my.oschina.net/u/2376912/blog/2209433

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值