11. SpringBoot整合谷歌验证码

SpringBoot整合谷歌验证码

  1. 依赖

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

    1. 配置文件 kaptcha.properties

      ##### Kaptcha Information
      kaptcha.width=150
      kaptcha.height=42
      kaptcha.border=no
      kaptcha.textproducer.font.size=40
      kaptcha.textproducer.char.space=10
      kaptcha.textproducer.font.names=u4EFFu5B8B,u5FAEu8F6Fu96C5u9ED1
      kaptcha.textproducer.char.string=1234567890
      kaptcha.textproducer.char.length=4
      kaptcha.background.clear.from=92,189,170
      kaptcha.background.clear.to=255,255,255
      
    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 {
          private static Properties props = new Properties();
      
          @Bean
          public DefaultKaptcha defaultKaptcha() throws Exception {
              // 创建DefaultKaptcha对象
              DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
      
              // 读取配置文件
              try {
                  props.load(KaptchaConfig.class.getClassLoader()
                          .getResourceAsStream("kaptcha.properties"));
              }catch (Exception e) {
                  e.printStackTrace();
              }
      
              // 将Properties文件设到DefaultKaptcha对象中
              defaultKaptcha.setConfig(new Config(props));
              return defaultKaptcha;
          }
          
      }
      
    3. API

      import com.google.code.kaptcha.Constants;
      import com.google.code.kaptcha.Producer;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      import javax.imageio.ImageIO;
      import javax.servlet.ServletOutputStream;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      import java.awt.image.BufferedImage;
      
      @Controller
      public class CodeController {
          @Autowired
          private Producer captchaProducer = null;
          @RequestMapping("/kaptcha")
          public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
              HttpSession session = request.getSession();
              response.setDateHeader("Expires", 0);
              response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
              response.addHeader("Cache-Control", "post-check=0, pre-check=0");
              response.setHeader("Pragma", "no-cache");
              response.setContentType("image/jpeg");
              //生成验证码
              String capText = captchaProducer.createText();
              session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
              //向客户端写出
              BufferedImage bi = captchaProducer.createImage(capText);
              ServletOutputStream out = response.getOutputStream();
              ImageIO.write(bi, "jpg", out);
              try {
                  out.flush();
              } finally {
                  out.close();
              }
          }
      }
      
原文地址:https://www.cnblogs.com/forelim/p/15396935.html