美文网首页技术
Captcha生成与使用,解决Docker font

Captcha生成与使用,解决Docker font

作者: Qihang | 来源:发表于2017-12-03 17:06 被阅读705次
    • pom依赖
    <!-- Kaptcha -->
    <dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>${kaptcha.version}</version>
    </dependency>
    
    • KaptchaConfig
    @Bean(name="captchaProducer")
    public DefaultKaptcha getKaptchaBean(){
        DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
        Properties properties=new Properties();
        properties.setProperty("kaptcha.border", "yes");
        properties.setProperty("kaptcha.border.color", "204,204,204");
        properties.setProperty("kaptcha.image.width", "125");
        properties.setProperty("kaptcha.image.height", "60");
        properties.setProperty("kaptcha.background.clear.from", "245,245,245");
        properties.setProperty("kaptcha.background.clear.to", "245,245,245");
        properties.setProperty("kaptcha.noise.color", "195,35,97");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.textproducer.font.size", "40");
        properties.setProperty("kaptcha.textproducer.font.color", "195,35,97");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config=new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
    
    • KaptchaService
    @Resource
    Producer captchaProducer;
    
    @Override
    public BufferedImage getImgCode() {
        String codeText = captchaProducer.createText();
        // 利用生成的字符串构建图片
        BufferedImage bi = captchaProducer.createImage(codeText);
    
        jedisClient.setAndExpire("kaptcha:imgCode:" + codeText, codeText,300);
        return bi;
    }
    
    • 方式一 生成图片
    @RequestMapping(value = "/getImgCode", method = RequestMethod.GET)
    public void getCode(HttpServletResponse response) {
        ResultDto resultDto = new ResultDto();
    
        BufferedImage codeImage = kaptchaService.getImgCode();
        response.setDateHeader("Expires", 0L);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        response.setContentType("image/jpeg");
        ServletOutputStream out = null;
    
        try {
            out = response.getOutputStream();
            ImageIO.write(codeImage, "jpg", out);
            out.flush();
        } catch (IOException var15) {
            logger.error(var15.getMessage(), var15);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException var14) {
                    logger.error(var14.getMessage(), var14);
                }
            }
    
        }
    }
    

    显示

    <img src="***/getImgCode?math="+Math.random() onClick="this.src='***/getImgCode?math='+Math.random()"/>
    
    • 方式二 生成base64
    @RequestMapping(value = "/getImgCode", method = RequestMethod.GET)
    public ResultDto getCode(HttpServletResponse response) {
        ResultDto resultDto = new ResultDto();
    
        BufferedImage codeImage = kaptchaService.getImgCode();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(codeImage, "jpg", baos);
            baos.flush();
        } catch (IOException var15) {
            logger.error(var15.getMessage(), var15);
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException var14) {
                    logger.error(var14.getMessage(), var14);
                }
            }
    
        }
        byte[] bytes = baos.toByteArray();
        String result = encoder.encodeBuffer(bytes).trim();
        resultDto.setData(result);
        return resultDto;
    }
    

    显示

    <img src="data:image/png;base64,***" onClick="this.src="data:image/png;base64,***"/>
    

    解决Docker部署无字体空指针

    • 构建docker镜像
    FROM  docker.private/openjdk:fonts
    
    MAINTAINER Qihang
    
    VOLUME /tmp
    
    COPY merchant-rest/target/merchant.jar merchant.jar
    
    ENV JAVA_OPTS="-Duser.timezone=Asia/Shanghai"
    
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /merchant.jar" ]
    
    • 部署获取图片验证码,报找不到字体空指针

    • 解决方案Dockerfile

    FROM openjdk:8-jdk-alpine
    
    MAINTAINER Qihang <leiyang@ranbb.com>
    
    RUN set -xe \
    && apk --no-cache add ttf-dejavu fontconfig
    
    • build
    docker build -t docker.private/openjdk:fonts .
    
    • 简单描述
      • 报字体空指针,说明openjdk:8-jdk-alpine没有字体。
      • 解决方法就是加入字体Dockerfile
      • 找不到字体解决了,同时出现了构建镜像慢的问题
      • 解决方案,把安装字体的命令单独提取一层为基础镜像
      • 把构建服务的Docker引用那个基础镜像

    相关文章

      网友评论

        本文标题:Captcha生成与使用,解决Docker font

        本文链接:https://www.haomeiwen.com/subject/ghxzbxtx.html