springboot验证码

springboot验证码_第1张图片

主要代码

package springboot.com.yzm;

import com.github.bingoohuang.patchca.color.RandomColorFactory;
import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
import com.github.bingoohuang.patchca.word.RandomWordFactory;

public class CaptchaFactory {

    private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();

    static {
        cs.setColorFactory(new RandomColorFactory());
        RandomWordFactory wf = new RandomWordFactory();
        wf.setCharacters("1234567890");
        wf.setMaxLength(4);
        wf.setMinLength(4);
        cs.setWordFactory(wf);
    }

    public static ConfigurableCaptchaService getInstance() {
        return cs;
    }
}

package springboot.com.yzm;


import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
import com.github.bingoohuang.patchca.filter.FilterFactory;
import com.github.bingoohuang.patchca.filter.predefined.*;
import com.github.bingoohuang.patchca.utils.encoder.EncoderHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/security")
public class ValidateController {

    /**
     * 日志对象
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private static final long serialVersionUID = 1L;

    private static ConfigurableCaptchaService cs = CaptchaFactory.getInstance();

    private static List factories;

    static {
        factories = new ArrayList();
        factories.add(new CurvesRippleFilterFactory(cs.getColorFactory()));
        factories.add(new MarbleRippleFilterFactory());
        factories.add(new DoubleRippleFilterFactory());
        factories.add(new WobbleRippleFilterFactory());
        factories.add(new DiffuseRippleFilterFactory());
    }

    @RequestMapping("/getImage")
    public void getImage(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            cs.setFilterFactory(factories.get(new Random().nextInt(5)));
            setResponseHeaders(response);

            String token = EncoderHelper.getChallangeAndWriteImage(cs, "png",
                    response.getOutputStream());
            session.setAttribute("TEST_YZM", token);
            logger.info("当前的SessionID = " + session.getId() + ",  验证码 = " + token);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setResponseHeaders(HttpServletResponse response) {
        response.setContentType("image/png");
        response.setHeader("Cache-Control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        long time = System.currentTimeMillis();
        response.setDateHeader("Last-Modified", time);
        response.setDateHeader("Date", time);
        response.setDateHeader("Expires", time);
    }


}

package springboot.com.yzm;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/yzm")
public class YzmTestController {

    @RequestMapping(value = "/cap")
    public String cap(HttpServletRequest request, HttpServletResponse response) {
        return "/yzm/cap";
    }

    @ResponseBody
    @RequestMapping(value = "/validate")
    public String validate(HttpServletRequest request, HttpServletResponse response, String yzmCode) {

        System.out.println("用户输入的验证码为yzmCode=" + yzmCode);
        String yzm = (String) request.getSession().getAttribute("TEST_YZM");
        String prin = "";
        if (!StringUtils.equals(yzm, yzmCode)) {
            prin = "##########################################验证码输入错误############################################";
        } else {
            prin = "******************************************验证码输入成功********************************************";
        }
        System.out.println(prin);
        return prin;
    }
}



    yzm
    
    



最下边附上我的码云地址
https://gitee.com/anlijie/springboot-verification
如果好的话,给个star吧
springboot验证码_第2张图片

你可能感兴趣的:(Java,springboot,验证码)