All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.CaptchaUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.utils;

import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.Optional;
import java.util.UUID;

/**
 * Created by Elon.su on 2018/2/17.
 */
@Slf4j
public final class CaptchaUtils {

    private CaptchaUtils() {

    }

    public static void producerCaptcha(Producer captchaProducer, HttpServletRequest request, HttpServletResponse response, String captchaKey) throws Exception {
        if (null == captchaProducer || null == request || null == response) {
            throw new NullPointerException("系统参数没有实例化");
        }
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = captchaProducer.createText();
        String sessionKey = captchaKey(captchaKey);
        log.debug("[captcha] key:{}, value", sessionKey, capText);
        session.setAttribute(sessionKey, capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

    public static boolean validateCaptcha(HttpServletRequest request, String type, String captchaValue) throws Exception {
        String captcha = Optional.ofNullable(request.getSession())
                .map(x -> (String) x.getAttribute(captchaKey(type)))
                .orElse("");

        if (StringUtils.isNotBlank(captchaValue) && captchaValue.equalsIgnoreCase(captcha)) {
            return true;
        }
        return false;
    }

    private static String captchaKey(String type) {
        return "KAPTCHA_" + type + UUID.randomUUID().toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy