springboot整合kaptcha实现验证码图片

252 阅读1分钟

验证码的参数可以在config中配置

config配置类:

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(){
            com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
            Properties properties = new Properties();
            /**是否使用边框 */
            properties.setProperty("kaptcha.border", "no");
            /**边框颜色 */
            properties.setProperty("kaptcha.border.color", "105,179,90");
            /**验证码字体颜色 */
            properties.setProperty("kaptcha.textproducer.font.color", "red");
            /**验证码字体颜色 */
            properties.setProperty("kaptcha.image.width", "110");
            /**验证码图片的宽度 */
            properties.setProperty("kaptcha.image.height", "40");
            /**验证码字体的大小 */
            properties.setProperty("kaptcha.textproducer.font.size", "40");
            /**验证码保存在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;
        }
    }

控制层:

@Controller
public class TestController {
    @Autowired
    DefaultKaptcha defaultKaptcha;

    //获取验证码
    @RequestMapping("/getCode")
    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{
        byte[] captchaChallengeAsJpeg = null;
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        try {
            //生产验证码字符串并保存到session中
            String createText = defaultKaptcha.createText();
            httpServletRequest.getSession().setAttribute("vrifyCode", createText);
            //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
            BufferedImage challenge = defaultKaptcha.createImage(createText);
            ImageIO.write(challenge, "jpg", jpegOutputStream);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        //定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream =
                httpServletResponse.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    }
}