美文网首页
JCaptcha生成验证码并读取

JCaptcha生成验证码并读取

作者: 天马行空_9f6e | 来源:发表于2021-05-13 21:34 被阅读0次

    Maven配置

    <dependency>

                <groupId>com.octo.captcha</groupId>

                <artifactId>jcaptcha</artifactId>

                <version>1.0</version>

            </dependency>

    编写JCaptchaUtil工具类

    package com.bruce.common.util;

    import com.octo.captcha.CaptchaFactory;

    import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;

    import com.octo.captcha.component.image.color.RandomRangeColorGenerator;

    import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;

    import com.octo.captcha.component.image.textpaster.RandomTextPaster;

    import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;

    import com.octo.captcha.component.word.FileDictionary;

    import com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGenerator;

    import com.octo.captcha.engine.GenericCaptchaEngine;

    import com.octo.captcha.image.gimpy.GimpyFactory;

    import com.octo.captcha.service.image.ImageCaptchaService;

    import com.octo.captcha.service.multitype.GenericManageableCaptchaService;

    public class JCaptchaUtil {

    private static final ImageCaptchaService service =imageCaptchaService();

    public static ImageCaptchaService getService() {

    return service;

    }

    public static ImageCaptchaService imageCaptchaService() {

    //设置背景

            UniColorBackgroundGenerator backgroundGenerator =new UniColorBackgroundGenerator(100,50);

    //设置颜色

            RandomRangeColorGenerator textColor =new RandomRangeColorGenerator(new int[]{0,250},new int[]{0,250},new int[]{0,250});

    RandomTextPaster randomTextPaster =new RandomTextPaster(4,5,textColor);

    //设置字体

            RandomFontGenerator randomFontGenerator =new RandomFontGenerator(20,30);

    //组装成图像

            ComposedWordToImage composedWordToImage =new ComposedWordToImage(randomFontGenerator,backgroundGenerator,randomTextPaster);

    ComposeDictionaryWordGenerator cdwg =new ComposeDictionaryWordGenerator(new FileDictionary("toddlist"));

    GimpyFactory gf =new GimpyFactory(cdwg,composedWordToImage);

    GenericCaptchaEngine genericCaptchaEngine =new GenericCaptchaEngine(new CaptchaFactory[]{gf});

    return new GenericManageableCaptchaService(genericCaptchaEngine,20,2000,2000);

    }

    }

    编写JCaptchaController用于请求生成验证码

    package com.bruce.portal.web.controller;

    import com.bruce.common.util.JCaptchaUtil;

    import org.springframework.web.bind.annotation.GetMapping;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RestController;

    import javax.imageio.ImageIO;

    import javax.servlet.ServletOutputStream;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import java.awt.image.BufferedImage;

    import java.io.ByteArrayOutputStream;

    import java.io.IOException;

    @RestController

    @RequestMapping("/jcaptcha")

    public class JCaptchaController {

    String attrName ="verifyCode";

    @GetMapping("/generatorCode")

    public void generatorCode(HttpServletRequest request,HttpServletResponse response) {

    System.out.println("generatorCode");

    try {

    BufferedImage bufferedImage =JCaptchaUtil.getService().getImageChallengeForID(request.getSession().getId());

    ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();

    //ImageIO jpegEncoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream);

                String formatName ="jpg";

    //FileOutputStream out = new FileOutputStream(dstName);

    //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

    //encoder.encode(dstImage);

                ImageIO.write(bufferedImage,formatName,byteArrayOutputStream);

    response.setHeader("Cache-Control","no-store");

    response.setContentType("image/jpeg");

    byte[]bytes =byteArrayOutputStream.toByteArray();

    ServletOutputStream servletOutputStream = response.getOutputStream();

    servletOutputStream.write(bytes);

    servletOutputStream.flush();

    servletOutputStream.close();

    }catch (IOException ioe) {

    ioe.printStackTrace();

    }

    }

    @GetMapping("/verify")

    public String verify(String verifyCode,HttpServletRequest request) {

    boolean flag =JCaptchaUtil.getService().validateResponseForID(request.getSession().getId(), verifyCode);

    if (flag) {

    return "验证码校验验通过";

    }

    return "验证码校验不通过";

    }

    }

    相关文章

      网友评论

          本文标题:JCaptcha生成验证码并读取

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