zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
        
  • 相关阅读:
    codility上的问题(15) Xi 2012
    HDU 4350 Card
    如何在SourceInsight中选中匹配的大括号中的内容
    Codility上的问题 (16) Omicron 2012
    WPF的MVVM
    html5的自定义data-*属性和jquery的data()方法的使用
    hdu 4635 Strongly connected(强连通+缩点)
    HDU3709:Balanced Number(数位DP+记忆化DFS)
    NGUI: Documentation
    Android到您的计算机使用命令行屏幕捕获和出口
  • 原文地址:https://www.cnblogs.com/forelim/p/15396935.html
Copyright © 2011-2022 走看看