zoukankan      html  css  js  c++  java
  • java验证码接口

    1.maven依赖

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

    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 {
        @Bean
        public DefaultKaptcha getDefaultKaptcha() {
            DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
            Properties properties = new Properties();
            // 图片边框
            properties.setProperty("kaptcha.border", "yes");
            // 边框颜色
            properties.setProperty("kaptcha.border.color", "105,179,90");
            // 字体颜色
            properties.setProperty("kaptcha.textproducer.font.color", "red");
            // 图片宽
            properties.setProperty("kaptcha.image.width", "130");
            // 图片高
            properties.setProperty("kaptcha.image.height", "40");
            // 字体大小
            properties.setProperty("kaptcha.textproducer.font.size", "30");
            // session key
            properties.setProperty("kaptcha.session.key", "code");
            // 验证码长度
            properties.setProperty("kaptcha.textproducer.char.length", "4");
            // 文字间距
            properties.put("kaptcha.textproducer.char.space", "7");
            // 字体
            properties.put("kaptcha.textproducer.font.names", "Arial,cmr10");
            // 采用那些文字
            properties.put("kaptcha.textproducer.char.string", "ABCDEFGHJKLMNPQRSTUVWXYZ13456789abcdeghjknsuvwxyz");
            Config config = new Config(properties);
            defaultKaptcha.setConfig(config);
    
            return defaultKaptcha;
        }
    }
    

    3.接口

    @Controller
    @RequestMapping("kaptcha")
    @Slf4j
    @Api(tags = {"验证码生成接口"})
    public class KaptchaController {
    
        /**
         * 1、验证码工具
         */
        @Autowired
        DefaultKaptcha defaultKaptcha;
    
        /**
         * 生成验证码
         * @param request
         * @param response
         * @throws Exception
         */
        @ApiOperation(value = "生成验证码")
        @GetMapping("generateKaptcha")
        public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            byte[] captchaChallengeAsJpeg = null;
            ByteArrayOutputStream jpegOutputStream = null;
            ServletOutputStream responseOutputStream = null;
            try {
                jpegOutputStream = new ByteArrayOutputStream();
                // 生产验证码字符串并保存到session中
                String createText = defaultKaptcha.createText();
                log.info("sessionId={},验证码={}", request.getSession().getId(), createText);
                request.getSession().setAttribute(Constant.VERIFY_CODE, createText);
                // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
                BufferedImage challenge = defaultKaptcha.createImage(createText);
                ImageIO.write(challenge, "jpg", jpegOutputStream);
                // 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
                captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
                response.setHeader("Cache-Control", "no-store");
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setContentType("image/jpeg");
                responseOutputStream = response.getOutputStream();
                responseOutputStream.write(captchaChallengeAsJpeg);
            } catch (IllegalArgumentException e) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            } finally {
                IOUtils.closeQuietly(jpegOutputStream);
                IOUtils.closeQuietly(responseOutputStream);
            }
        }
    }
    

      

  • 相关阅读:
    转载Python中__getattr__ __getattribute__ __get__解释
    python异常介绍
    Javascript创建对象的几种方式
    python类高级话题
    python运算符重载2
    python运算符重载
    python面向对象编程基础
    PHP学习之提示标签
    Web自动化测试构建学习小结(二)
    Web自动化测试构建学习小结(一)
  • 原文地址:https://www.cnblogs.com/lvjijun/p/13992870.html
Copyright © 2011-2022 走看看