验证码实现 - 工具类调用版

前言

图片验证码是我们日常经常用到的,本文将介绍如何实现以及其原理,以注册页面为例实现功能。
如果对滑块验证码感兴趣的可以看这篇文章:滑块验证码实现及原理
如果对验证码实现感兴趣的可以看这篇文章:验证码实现 - html页面版
如果对表单校验感兴趣的可以看这篇文章:表单校验
在这里插入图片描述

代码实现

代码的解释在注释中已经写的很清楚了,如有更好的实现方式欢迎留言。
ValidateCode工具类

package cn.qd.utils;

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.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class ValidateCode {

    private int width = 90;//验证码宽度 默认值:90
    private int height = 40;//验证码高度 默认值:40
    private int codeCount = 4;//验证码个数  默认值:4
    private int lineCount = 19;//混淆线个数  默认值:19
    private int  fontSize = 20;//字体大小像素
    // 存储session中的key值 默认值:"validateCode"
    private String sessionKey = "validateCode";
    public ValidateCode(){}

    /**	 *
     * * @param width 验证码宽度
     * * @param height 验证码高度
     * * @param fontSize 字体大小像素
     * */
    public ValidateCode(int width,int height,int fontSize){
        this.width = width;
        this.height = height;
        this.fontSize = fontSize;
    }

    /**	 *
     *  * @param width 验证码宽度
     *  * @param height 验证码高度
     *  * @param fontSize 字体大小像素
     *  * @param sessionKey 存储session中的key值
     *  */
    public ValidateCode(int width,int height,int fontSize,String sessionKey){
        this.width = width;
        this.height = height;
        this.fontSize = fontSize;
        this.sessionKey = sessionKey;
    }
    /**	 *
     * * @param width 验证码宽度
     * * @param height 验证码高度
     * * @param codeCount 验证码个数
     * * @param fontSize 字体大小像素
     * * @param sessionKey 存储session中的key值
     * */
    public ValidateCode(int width,int height,int codeCount,int fontSize,String sessionKey){
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.fontSize = fontSize;
        this.sessionKey = sessionKey;
    }
    /**	 *
     * * @param width 验证码宽度
     * * @param height 验证码高度
     * * @param codeCount 验证码个数
     * * @param lineCount 混淆线个数
     * * @param fontSize 字体大小像素
     * * @param sessionKey 存储session中的key值
     * */
    public ValidateCode(int width,int height,int codeCount,int lineCount,int fontSize,String sessionKey){
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        this.fontSize = fontSize;
        this.sessionKey = sessionKey;
    }
    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' };
    /**
     *  * 具体获取验证码的方法
     *  * @param time  time为时戳,这样的话可以避免浏览器缓存验证码
     *  * @throws IOException
     *  */
    public void getCode(HttpServletRequest request, HttpServletResponse response){
        //定义随机数类
        Random r = new Random();
        //定义存储验证码的类
        StringBuilder builderCode = new StringBuilder();
        //定义画布
        BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //得到画笔
        Graphics g = buffImg.getGraphics();
        //1.设置颜色,画边框
        g.setColor(Color.gray);
        g.drawRect(0,0,width,height);
        //2.设置颜色,填充内部
        g.setColor(Color.white);
        g.fillRect(1,1,width-2,height-2);
        //3.设置干扰线
        // g.setColor(Color.gray);
        for (int i = 0; i < lineCount; i++) {
            int _R = (int)Math.floor(Math.random()*256);
            int _G = (int)Math.floor(Math.random()*256);
            int _B = (int)Math.floor(Math.random()*256);
            g.setColor(new Color(_R, _G, _B, 255));
            g.drawLine(r.nextInt(width),r.nextInt(width),r.nextInt(width),r.nextInt(width));
        }
        //4.设置验证码
        g.setColor(Color.blue);
        //4.1设置验证码字体
        g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,fontSize));
        for (int i = 0; i < codeCount; i++) {
            char c = codeSequence[r.nextInt(codeSequence.length)];
            builderCode.append(c);
            g.drawString(c+"",((width/codeCount)*i+2),height*4/5);
        }
        try {
            //5.输出到屏幕
            ServletOutputStream sos = response.getOutputStream();
            ImageIO.write(buffImg,"png",sos);
            //6.保存到session中
            HttpSession session = request.getSession();
            session.setAttribute(""+sessionKey+"",builderCode.toString());
            //7.禁止图像缓存。
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/png");
            //8.关闭sos
            sos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Controller层调用

/**
 * 验证码实现
  * @param request
  * @param response
  * @throws Exception
  */
@RequestMapping(value = "/captcha", method = RequestMethod.GET)
public void captcha(@RequestParam(value = "time") String time, HttpServletRequest request, HttpServletResponse response) throws Exception {
     ValidateCode code= new ValidateCode(130,35,30,"validateCode");
     code.getCode(request,response);
 }

注册页面

<%@ page import="java.util.Date" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!doctype html>
<html>
<head></head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员注册</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" type="text/css"/>
<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" type="text/css"/>

<style>
    body {
        margin-top: 20px;
        margin: 0 auto;
    }

    .carousel-inner .item img {
        width: 100%;
        height: 300px;
    }

    .container .row div {
        /* position:relative;
        float:left; */
    }

    font {
        color: #3164af;
        font-size: 18px;
        font-weight: normal;
        padding: 0 10px;
    }
</style>
<script type="text/javascript">
    $(function () {
        //time表示时间戳,可以防止缓存,确保每次生成不同的验证码
        $("#captchaImage").click(function(){
            //$("#captchaImage").attr("src", "user/captcha?time=" + (new Date()).valueOf());
            this.src = "${pageContext.request.contextPath}/user/captcha?time="+new Date().getTime();
        });
    });

    //校验验证码
    function checkCode() {
        //1.获取验证码的值
        var code = document.getElementById("checkcode").value;
        //2.获取随机生成的验证码

        //3.提示信息
        var s_code = document.getElementById("s_code");
        //3.判断两次的值是否一致
        if (code != confirmpwd) {
            //提示红色错误信息
        s_code.innerHTML = "验证码不一致";
        }
        return flag;
    }
</script>
</head>
<body>

<%@ include file="/WEB-INF/pages/header.jsp" %>

<div class="container" style="width:100%;background:url('${pageContext.request.contextPath}/img/regist_bg.jpg');">
    <div class="row">

        <div class="col-md-2"></div>


        <div class="col-md-8" style="background:#fff;padding:40px 80px;margin:30px;border:7px solid #ccc;">
            <font>会员注册</font>USER REGISTER
            <form class="form-horizontal" style="margin-top:5px;" id="registerFrom" method="post"
                  action="${pageContext.request.contextPath}/user/registerUser">
                <input type="hidden" name="uid" value="0"/>
                <div class="form-group">
                    <label for="username" class="col-sm-2 control-label">用户名</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="username" id="username" placeholder="请输入用户名">
                    </div>
                    <div style="color: red;"><span id="s_username" class="error"></span></div>
                </div>
                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
                    <div class="col-sm-6">
                        <input type="password" class="form-control" name="password" id="password" placeholder="请输入密码">
                    </div>
                    <div style="color: red;"><span id="s_password" class="error"></span></div>
                </div>
                <div class="form-group">
                    <label for="confirmpwd" class="col-sm-2 control-label">确认密码</label>
                    <div class="col-sm-6">
                        <input type="password" class="form-control" name="pwd" id="confirmpwd" placeholder="请输入确认密码">
                    </div>
                    <div style="color: red;"><span id="s_confirmpwd" class="error"></span></div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-6">
                        <input type="email" class="form-control" name="email" id="email" placeholder="Email">
                    </div>
                    <div style="color: red;"><span id="s_email" class="error"></span></div>
                </div>
                <div class="form-group">
                    <label for="usercaption" class="col-sm-2 control-label">姓名</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="name" id="name" placeholder="请输入姓名">
                    </div>
                    <div style="color: red;"><span id="s_rename" class="error"></span></div>
                </div>
                <div class="form-group">
                    <label for="usertelepone" class="col-sm-2 control-label">手机号</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="telephone" id="telephone" placeholder="请输入手机号">
                    </div>
                    <div style="color: red;"><span id="s_telphone" class="error"></span></div>
                </div>
                <div class="form-group opt">
                    <label for="inlineRadio1" class="col-sm-2 control-label">性别</label>
                    <div class="col-sm-6">
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="sex1" value=""></label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="sex2" value=""></label>
                    </div>
                </div>
                <input type="hidden" name="state" value="1"/>
                <div class="form-group">
                    <label for="date" class="col-sm-2 control-label">出生日期</label>
                    <div class="col-sm-6">
                        <input type="date" class="form-control" name="birthday" id="birthday">
                    </div>
                </div>

                <div class="form-group">
                    <label for="date" class="col-sm-2 control-label">验证码</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" name="code" id="checkcode" placeholder="请输入验证码">
                    </div>
                    <div class="col-sm-2">
                        <img src="${pageContext.request.contextPath}/user/captcha?time=<%=new Date().getTime() %>" id="captchaImage" title="点击图片重新获得验证码"/><%--img/captcha.jhtml--%>
                        <%--<a id="captchaImage2" >换一张</a><span></span></td>--%>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input type="submit" width="100" value="注册" name="submit" border="0"
                               style="background: url('${pageContext.request.contextPath}/img/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
                                       height:35px;width:100px;color:white;">
                    </div>
                </div>
            </form>
        </div>

        <div class="col-md-2"></div>

    </div>
</div>

<%@ include file="/WEB-INF/pages/footer.jsp" %>

</body>
</html>

效果图展示

在这里插入图片描述


如果有收获!!! 希望老铁们来个三连,点赞、收藏、转发
创作不易,别忘点个赞,可以让更多的人看到这篇文章,顺便鼓励我写出更好的博客
  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原文:https://github.com/yangchenjava/com.yangc.utils cache EhCacheUtils - 基于ehcache的工具类 LruCacheUtils - 基于LinkedHashMap实现LRU缓存的工具类 MemcachedUtils - 基于memcached的工具类 RedisUtils - 基于redis的工具类,与redis的集群配置无缝结合 db JdbcUtils - 操作jdbc的工具类 MongodbUtils - 操作mongodb的工具类 email EmailUtils - 邮件工具类,支持发送带附件的邮件 encryption AesUtils - 实现AES加密解密 Base64Utils - 实现Base64加密解密 Md5Utils - 获取字符串或文件的md5 excel ReadExcel2003 - 以model方式读2003Excel(大数据) ReadExcel2007 - 以sax方式读2007Excel(大数据) WriteExcel - 写Excel image CaptchaUtils - 生成验证码 ImageUtils - 图片压缩、截图 QRCodeUtils - 生成二维码、解析二维码 io SerializeUtils - 序列化、反序列化对象 ZipUtils - 压缩、解压文件 json JsonUtils - json格式转换 lang CharsetDetectorUtils - 获取文本文件编码格式 ChineseCalendar - 农历日历 ConvertUtils - 高低字节转换 DateUtils - 日期工具类 HtmlFilterUtils - 过滤html标签 JsoupUtils - 基于jsoup过滤html标签 MoneyUtils - 获取大写金额 NumberUtils - 数字工具类 PinyinUtils - 汉字转拼音 media MediaUtils - 基于ffmpeg,qtfaststart,yamdi的多媒体工具类 net AttachmentUtils - HTTP文件下载防止中文乱码 FastDFSUtils - 操作FastDFS的工具类 FtpUtils - 操作FTP的工具类(基于sun自家的包,jdk7以后不建议使用) FtpUtilsApache - 基于apache操作FTP的工具类 HttpUtils - 发送HTTP请求 IpUtils - 获取IP SFtpUtils - 操作SFTP的工具类 prop PropertiesUtils - 操作properties配置文件
### 回答1: 以下是一个简单的 Java 生成验证码的工具类: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class VerifyCodeUtils { // 验证码字符集 private static final String CODES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 验证码字体 private static final Font FONT = new Font("Arial", Font.BOLD, 20); // 验证码图片的宽度 private static final int WIDTH = 100; // 验证码图片的高度 private static final int HEIGHT = 40; // 验证码个数 private static final int COUNT = 4; /** * 生成验证码图片 * @param request * @param response * @throws Exception */ public static void generate(HttpServletRequest request, HttpServletResponse response) throws Exception { // 创建一个 BufferedImage 对象,宽度为 WIDTH,高度为 HEIGHT,类型为 RGB BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 获取一个 Graphics 对象 Graphics g = image.getGraphics(); // 设置背景颜色为白色 g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); // 生成随机数 Random random = new Random(); // 绘制干扰线 for (int i = 0; i < 5; i++) { g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT)); } // 绘制验证码 StringBuilder sb = new StringBuilder(); for (int i = 0; i < COUNT; i++) { // 设置验证码字符颜色为随机颜色 g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); // 生成随机字符 char c = CODES.charAt(random.nextInt(CODES.length())); // 绘制字符 g.setFont(FONT); g.drawString(String.valueOf(c), i * WIDTH / COUNT + 5, 25); sb.append(c); } // 将验证码存入 HttpSession 中 HttpSession session = request.getSession(); session.setAttribute("verifyCode", sb.toString()); // 设置响应头信息 response.setContentType("image/png"); ServletOutputStream out = response.getOutputStream(); // 将 BufferedImage 对象写入 ServletOutputStream 中 ImageIO.write(image, "png", out); out.flush(); out.close(); } } ``` 使用方法: 在 Servlet 中调用 `VerifyCodeUtils.generate(request, response)` 方法即可生成验证码图片并将验证码存入 HttpSession 中。在表单中输入验证码时,可以从 HttpSession 中获取验证码进行验证。 ### 回答2: Java生成验证码的工具类可以利用Java提供的随机数生成器和Graphics2D绘图类来实现。以下是一个简单的实现示例: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaUtil { private static final int WIDTH = 120; // 图片宽度 private static final int HEIGHT = 40; // 图片高度 private static final int CODE_COUNT = 4; // 验证码字符个数 private static final int CODE_FONT_SIZE = 25; // 验证码字体大小 // 生成验证码 public static BufferedImage generateCaptcha() { // 创建BufferedImage对象作为验证码图片 BufferedImage captchaImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 获取Graphics2D对象 Graphics2D g2d = captchaImage.createGraphics(); // 设置背景色为白色 g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, WIDTH, HEIGHT); // 设置字体样式 Font font = new Font("Arial", Font.BOLD, CODE_FONT_SIZE); g2d.setFont(font); // 生成随机验证码 Random random = new Random(); String captchaCode = ""; for (int i = 0; i < CODE_COUNT; i++) { char codeChar = (char) (random.nextInt(26) + 65); // 生成随机大写字母 captchaCode += codeChar; } // 将验证码绘制到图片上 g2d.setColor(Color.BLACK); g2d.drawString(captchaCode, 10, 30); // 添加干扰线 g2d.setColor(Color.GRAY); for (int i = 0; i < 6; i++) { int x1 = random.nextInt(WIDTH); int y1 = random.nextInt(HEIGHT); int x2 = random.nextInt(WIDTH); int y2 = random.nextInt(HEIGHT); g2d.drawLine(x1, y1, x2, y2); } // 销毁Graphics2D对象 g2d.dispose(); return captchaImage; } } ``` 使用该工具类可以通过`generateCaptcha()`方法生成验证码的图片。您可以将生成的验证码图片输出到前端页面,供用户验证。这样,每次刷新页面或需要进行验证验证时,都可以调用该方法生成新的验证码图片。 ### 回答3: Java生成验证工具类可以通过使用Java的图形绘制和随机数生成功能来实现。 首先,我们需要创建一个验证工具类,可以命名为CaptchaUtils。在这个类中,我们可以定义一个静态方法generateCaptcha,用于生成验证码。 生成验证码的过程可以分为以下几个步骤: 1. 创建一个BufferedImage对象,作为画布,用于绘制验证码。 2. 创建一个Graphics2D对象,用于在画布上进行绘制。 3. 设置画布的背景色和字体样式。 4. 使用随机数生成器(可以使用Java提供的Random类)生成一个指定长度的随机验证码字符串。 5. 使用Graphics2D对象将验证码字符串绘制在画布上,可以设置字符的颜色、大小和位置。 6. 生成干扰线或干扰点,增加验证码的安全性。 7. 返回验证码图片。 以下是一个简单的示例代码,实现了上述步骤: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaUtils { public static BufferedImage generateCaptcha(int width, int height, int length) { BufferedImage captchaImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = captchaImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setBackground(Color.WHITE); g2d.clearRect(0, 0, width, height); g2d.setColor(Color.BLACK); g2d.setFont(new Font("Arial", Font.PLAIN, 24)); Random random = new Random(); String captchaString = ""; for (int i = 0; i < length; i++) { char captchaChar = (char) (random.nextInt(26) + 'A'); captchaString += captchaChar; int x = 10 + (i * width / length); int y = 25 + (random.nextInt(height - 50)); g2d.drawString(String.valueOf(captchaChar), x, y); } for (int i = 0; i < 5; i++) { int x1 = random.nextInt(width); int y1 = random.nextInt(height); int x2 = random.nextInt(width); int y2 = random.nextInt(height); g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g2d.drawLine(x1, y1, x2, y2); } g2d.dispose(); return captchaImage; } } ``` 使用该验证工具类,我们可以在其他地方调用generateCaptcha方法生成验证码图片。调用示例代码如下: ```java public static void main(String[] args) { int width = 200; int height = 50; int length = 4; BufferedImage captchaImage = CaptchaUtils.generateCaptcha(width, height, length); // 将验证码图片保存到指定路径 File captchaFile = new File("captcha.jpg"); try { ImageIO.write(captchaImage, "JPEG", captchaFile); System.out.println("验证码已生成并保存为captcha.jpg"); } catch (IOException e) { e.printStackTrace(); } } ``` 以上就是一个简单的Java生成验证工具类的实现。可以根据需要进行进一步的修改和扩展,例如添加噪点、增加验证码样式等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值