Springboot 实现Kaptcha 验证码

一、jar包导入

  <!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
           <!--谷歌验证码-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

二、创建

KaptchaConfig 类,实例化到Spring容器内,配置验证码的基本属性
package com.example.springboot.shiro.core.shiro;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

/*
   @component (把普通pojo实例化到spring容器中,相当于配置文件中的
    <bean id="" class=""/>)
 */


@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", "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");
        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层 

 /**
     * Kaptcha 验证码
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws Exception
     */
    @RequestMapping("/defaultKaptcha")
    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        byte[] captchaChallengeAsJpeg = null;
        ByteArrayOutputStream gifgOutputStream = new ByteArrayOutputStream();
        try {
            //生产验证码字符串并保存到session中
            String createText = defaultKaptcha.createText();
            System.out.println("Kaptcha ====>>>>" + createText);
           httpServletRequest.getSession().setAttribute("Kaptcha",createText);

            System.out.println("存入Vcode到Session====>" +httpServletRequest.getSession().getAttribute("Kaptcha"));
            //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
            BufferedImage challenge = defaultKaptcha.createImage(createText);
            ImageIO.write(challenge, "gif", gifgOutputStream);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            System.out.println("获取验证码异常=======>>>>>>>" + e.getMessage());
            return;
        }

        //定义response输出类型为image/jpeg/gif类型,使用response输出流输出图片的byte数组
        captchaChallengeAsJpeg = gifgOutputStream.toByteArray();
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/gif");
        ServletOutputStream responseOutputStream =
                httpServletResponse.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    }

四、jsp

<input type="text" name="vcode" id="VerificationCode"  placeholder="Verification code" style="width: 110px; margin-left: -8px; margin-right: 10px;">
            <img
                 src='/defaultKaptcha?d='+new Date()*1 onclick="this.src='/defaultKaptcha?d='+new Date()*1"

            >

猜你喜欢

转载自blog.csdn.net/joe_wang1/article/details/81271130