使用kaptcha生成图片验证码

生成图片验证码

先导入依赖
build.gradle

implementation 'com.github.axet:kaptcha:0.0.9'

配置类
KaptchaConfiguration.java

package cn.com.infosky.config;

import com.google.code.kaptcha.Constants;
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;

/**
 * <p>配置类</p>
 *
 * @author xuxm
 * @date: 2021/6/22 11:19
 * @description: 配置类
 */
@Configuration
public class KaptchaConfiguration {
    @Bean
    public DefaultKaptcha defaultKaptcha() {
        Properties properties = new Properties();
        //边框
        properties.put(Constants.KAPTCHA_BORDER, "no");
        //字体颜色
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
        //5个字符
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "5");
        //字体
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

LoginController.java

package cn.com.infosky.controller;

import cn.com.infosky.service.IService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.UUID;

/**
 * <p></p>
 *
 * @author xuxm
 * @date: 2021/6/22 11:23
 * @description:
 */
@Controller
@CrossOrigin
public class LoginController {
    @Autowired
    private IService iService;

    /**
     * 获取图片验证码
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getCaptcha", method = RequestMethod.GET)
    public void get(HttpServletResponse response) throws IOException {
        response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store,no-cache");
        response.setContentType("image/jpeg");
        BufferedImage image = iService.getCaptcha(UUID.randomUUID().toString());
        ServletOutputStream outputStream = response.getOutputStream();
        //通过流的方式返回结果数据
        ImageIO.write(image, "jpg", outputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

IService.java

package cn.com.infosky.service;

import cn.com.infosky.exceptions.ParamException;
import com.google.code.kaptcha.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.image.BufferedImage;

/**
 * <p></p>
 *
 * @author xuxm
 * @date: 2021/6/22 11:24
 * @description:
 */
@Service
public class IService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private Producer producer;

    /**
     * <p>获取图片验证码</p>
     *
     * @param uuid
     * @return
     */
    public BufferedImage getCaptcha(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            throw new ParamException("uuid不能为空{}!");
        }
        logger.info("开始生成验证码");
        //获取验证码
        String code = producer.createText();
        return producer.createImage(code);
    }
}

到这里就结束了,是不是很简单。

上一篇:flv文件解析工具


下一篇:【Spring Boot】验证码:整合 kaptcha 生成、校对验证码