自定义验证码

样式展示

 当做用户登录验证时,如何自定义一个数字验证码

首先要关闭浏览器的缓存,防止生成验证码图片时验证码图片五变化

        //关闭缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);

设置文件的类型

        //设置文件的类型
        response.setContentType("image/gif");

然后通过java中自带的对象设置验证码的画板、画笔、字体还可以设置验证码背景

        //设置宽高
        int width=127;
        int hight=35;
        //画板
        BufferedImage image = new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
        //画笔
        Graphics graphics = image.getGraphics();
        //字体
        Font font = new Font("黑体", Font.BOLD, 25);
        graphics.setFont(font);
        //设置验证码背景
        String imagePath = request.getServletContext().getRealPath("/static/picture/codebeijing.jpg");
        Image image1 = ImageIO.read(new File(imagePath));
        graphics.drawImage(image1, 0, 0, 127, 35, null);

然后随机生成六位数的验证码,这里截取字符串,我只输入的数字 ,这里其实什么都可以输入 中文字也可以

        //设置验证码随机数
        String source = "0123456789";
        String infor="";
        for (int i = 0; i < 6; i++) {
            //随机取出一个字符
            int index =  new Random().nextInt(source.length()-1);
            char myCode = source.charAt(index);//
            Random random = new Random();
            //颜色
            graphics.setColor(new Color(255, 121, 15));

            graphics.drawString(myCode+"", 5+i*20, 25);
            graphics.drawLine(random.nextInt(150), random.nextInt(40), random.nextInt(150), random.nextInt(40));
            infor += myCode;//存在后台的验证数据
        }

最后将验证码保存到域中用于验证,再关闭流

        //将字符串放入session
        request.getSession().setAttribute("infor", infor);
        graphics.dispose();
        OutputStream out  = response.getOutputStream();
        ImageIO.write(image,"gif", out);
        out.flush();
        out.close();

完整代码展示

    /**
     * 验证码生成方法
     */
    public static void checkcode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //登录验证码生成
        //关闭缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);
        //设置文件的类型
        response.setContentType("image/gif");
        //设置宽高
        int width=127;
        int hight=35;
        //画板
        BufferedImage image = new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
        //画笔
        Graphics graphics = image.getGraphics();
        //字体
        Font font = new Font("黑体", Font.BOLD, 25);
        graphics.setFont(font);
        //设置验证码背景
        String imagePath = request.getServletContext().getRealPath("/static/picture/codebeijing.jpg");
        Image image1 = ImageIO.read(new File(imagePath));
        graphics.drawImage(image1, 0, 0, 127, 35, null);
        //设置验证码随机数
        String source = "0123456789";
        String infor="";
        for (int i = 0; i < 6; i++) {
            //随机取出一个字符
            int index =  new Random().nextInt(source.length()-1);
            char myCode = source.charAt(index);//
            Random random = new Random();
            //颜色
            graphics.setColor(new Color(255, 121, 15));

            graphics.drawString(myCode+"", 5+i*20, 25);
            graphics.drawLine(random.nextInt(150), random.nextInt(40), random.nextInt(150), random.nextInt(40));
            infor += myCode;//存在后台的验证数据
        }
        //将字符串放入session
        request.getSession().setAttribute("infor", infor);
        graphics.dispose();
        OutputStream out  = response.getOutputStream();
        ImageIO.write(image,"gif", out);
        out.flush();
        out.close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值