zoukankan      html  css  js  c++  java
  • patchca整合Spring MVC生成超炫的验证码

    @Controller
    public class Login2Controller {
        private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
        private static Random random = new Random();
        static {
    //        cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
            cs.setColorFactory(new ColorFactory() {
                @Override
                public Color getColor(int x) {
                    int[] c = new int[3];
                    int i = random.nextInt(c.length);
                    for (int fi = 0; fi < c.length; fi++) {
                        if (fi == i) {
                            c[fi] = random.nextInt(71);
                        else {
                            c[fi] = random.nextInt(256);
                        }
                    }
                    return new Color(c[0], c[1], c[2]);
                }
            });
            RandomWordFactory wf = new RandomWordFactory();
            wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
            wf.setMaxLength(4);
            wf.setMinLength(4);
            cs.setWordFactory(wf);
        }
        @RequestMapping("/pcrimg")
        public void crimg(HttpServletRequest request, HttpServletResponse response) throws IOException {
            switch (random.nextInt(5)) {
                case 0:
                    cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
                    break;
                case 1:
                    cs.setFilterFactory(new MarbleRippleFilterFactory());
                    break;
                case 2:
                    cs.setFilterFactory(new DoubleRippleFilterFactory());
                    break;
                case 3:
                    cs.setFilterFactory(new WobbleRippleFilterFactory());
                    break;
                case 4:
                    cs.setFilterFactory(new DiffuseRippleFilterFactory());
                    break;
            }
            HttpSession session = request.getSession(false);
            if (session == null) {
                session = request.getSession();
            }
            setResponseHeaders(response);
            String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
            session.setAttribute("captchaToken", token);
            System.out.println("当前的SessionID=" + session.getId() + ",验证码=" + token);
        }
        protected 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);
        }
    }
  • 相关阅读:
    linux系统基本目录的介绍
    vue 组件之间的通信-父组件给子组件传递数据
    postgresql数据库查询特定日期的数据
    使用HttpRequest调用第三方接口
    postgresql数据库中的 rownum
    mybatis框架,执行插入语句的时候,如果没有字段传过来就赋值为空 进行判断
    postgresql数据库left join将主表中的数据查询出多条的解决办法
    前后端交互 -精度丢失问题解决
    vue找页面
    mysql语法 join on 表示什么
  • 原文地址:https://www.cnblogs.com/zxf330301/p/6210964.html
Copyright © 2011-2022 走看看