zoukankan      html  css  js  c++  java
  • java 生成验证码

    1.引入maven依赖

         <dependency>
                <groupId>com.github.axet</groupId>
                <artifactId>kaptcha</artifactId>
                <version>0.0.9</version>
            </dependency>

    2.配置属性

    @Configuration
    public class VerifyCodeConfig {
        @Bean
        public DefaultKaptcha producer() {
            Properties properties = new Properties();
            properties.put("kaptcha.border", "yes");//是否有边框
            properties.put("kaptcha.textproducer.font.color", "blue");//背景颜色
            properties.put("kaptcha.textproducer.char.space", "5");
            properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");//字体
            Config config = new Config(properties);
            DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
            defaultKaptcha.setConfig(config);
            return defaultKaptcha;
        }
    }
    

    3.创建一个contoller:

    @Controller
    public class ThymeleafController {
        @Autowired
        private Producer producer;
      
        @GetMapping("/get/verification/code")
        public void VerificationCode(HttpServletResponse response){
            response.setHeader("Cache-Control", "no-store, no-cache");
            response.setContentType("image/jpeg");
            String text = producer.createText(); //验证码内容可以保存数据库,并设置过期时间
            BufferedImage image = producer.createImage(text);
            ServletOutputStream out=null;
            try {
                out= response.getOutputStream();
                ImageIO.write(image, "jpg", out);
            }catch (Exception ex){
                if(null!=out){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }

    4.浏览器访问:

      

  • 相关阅读:
    用select模拟一个socket server
    用select (多路复用)模拟一个 socket server
    IO模式
    IO多路复用
    进程、线程和协程的理解
    进程、线程和协程--自己的理解
    二维数组的初始化,遍历
    数组的练习
    练习1
    数组的内存结构
  • 原文地址:https://www.cnblogs.com/yangxiaohui227/p/14117135.html
Copyright © 2011-2022 走看看