图片验证码获取和验证

工具类 

@Component
public class CaptchaGenerator {

    // 默认图片宽度
    private final int defaultWidth = 90;

    // 默认图片高度
    private final int defaultHeight = 30;

    // 默认验证码长度
    private final int defaultCodeCount = 4;

    // 默认字体高度
    private final int defaultFontHeight = 18;

    // 默认xx值
    private final int defaultXx = 15;

    // 默认codeY值
    private final int defaultCodeY = 16;

    private Random random = new Random();

    private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    private char[] numberSequence = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    /**
     * 全部使用默认值
     */
    public Captcha getCaptcha(boolean number) {
        return getCaptcha(number,defaultWidth, defaultHeight, defaultCodeCount, defaultFontHeight, defaultXx, defaultCodeY);
    }

    /**
     * 自定义高度和宽度
     */
    public Captcha getCaptcha(int width, int height) {
        return getCaptcha(false,width, height, defaultCodeCount, defaultFontHeight, defaultXx, defaultCodeY);
    }

    /**
     * 自定义高宽和验证码长度
     */
    public Captcha getCaptcha(int width, int height, int codeCount) {
        return getCaptcha(false,width, height, codeCount, defaultFontHeight, defaultXx, defaultCodeY);
    }

    /**
     * @return key=验证码字符串 val=验证码图片
     */
    public Captcha getCaptcha(boolean number,int width, int height, int codeCount, int fontHeight, int xx, int codeY) {
        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // Graphics2D gd = buffImg.createGraphics();
        // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
        Graphics gd = buffImg.getGraphics();
        // 将图像填充为白色
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
        // 创建字体,字体的大小应该根据图片的高度来定
        Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
        // 设置字体
        gd.setFont(font);
        // 画边框
        gd.setColor(Color.BLACK);
        gd.drawRect(0, 0, width - 1, height - 1);
        // randomCode用于保存随机产生的验证码,以便用户登录后进行验证
        String randomCode = "";
        int red = 0, green = 0, blue = 0;
        // 随机产生codeCount数字的验证码。
        for (int i = 0; i < codeCount; i++) {
            // 得到随机产生的验证码数字
            String code = null;
            if (number){
                code = String.valueOf(numberSequence[random.nextInt(10)]);
            } else {
                code = String.valueOf(codeSequence[random.nextInt(36)]);
            }
            // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            // 用随机产生的颜色将验证码绘制到图像中
            gd.setColor(new Color(red, green, blue));
            gd.drawString(code, (i + 1) * xx, codeY);
            // 添加到已生成的验证码尾部
            randomCode += code;
        }
        // 随机产生干扰线,使图象中的验证码不易被其它程序探测到
        // 干扰线复杂度由循环数量及线条长短控制
        gd.setColor(Color.BLACK);
        for (int i = 0; i < 15; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(60);
            int yl = random.nextInt(60);
            gd.drawLine(x, y, x + xl, y + yl);
        }
        // 组装返回值
        return new Captcha(randomCode, buffImg);
    }

    /**
     * Inner class represents a captcha image and its code
     */
    public class Captcha {

        private String code;

        private BufferedImage image;

        Captcha(String code, BufferedImage image) {
            this.code = code;
            this.image = image;
        }

        public String getCode() {
            return code;
        }

        public BufferedImage getImage() {
            return image;
        }
    }
}

图片验证码接口 

    @Autowired
    private CaptchaGenerator captchaGenerator;

    /**
     * 获取图片验证码
     * @param number
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping(value = "/getCaptcha", method = RequestMethod.GET)
    public void getCode(boolean number,HttpServletRequest req, HttpServletResponse resp) throws IOException {
        log.debug("Generating captcha... number={}",number);
        // sign==true?数字字母混合:纯数字
        CaptchaGenerator.Captcha captcha = captchaGenerator.getCaptcha(number);
        log.debug("Writing captcha to client... code={}", captcha.getCode());
        // 将四位数字的验证码保存到Session中
        HttpSession session = req.getSession();
        session.setAttribute("code", captcha.getCode());
        // 禁止图像缓存
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        resp.setContentType("image/jpeg");
        // 将图像输出到Servlet输出流中
        ServletOutputStream sos = resp.getOutputStream();
        ImageIO.write(captcha.getImage(), "jpeg", sos);
        sos.close();
    }

    /**
     * 验证码校验
     * @param req
     * @param code
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/checkCaptcha", params = "code", method = RequestMethod.GET)
    @ResponseBody
    public AjaxCallResults checkCaptcha(HttpServletRequest req, @RequestParam("code") String code) throws IOException {
        AjaxCallResults map = AjaxCallResults.newInstanceOfSuccess();
        boolean checkResult = false;
        HttpSession session = req.getSession();
        String checkCode = (String) session.getAttribute("code");
        if (code != null) {
            code = code.toUpperCase();
        }
        if (checkCode != null && checkCode.equals(code)) {
            checkResult = true;
        }
        map.put("result", checkResult);
        return map;
    }

前端

点击变更

你可能感兴趣的:(Java)