• 验证码实现(google)


    1.引入jar包

    <dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>2.3.2</version>
     </dependency>

    2.在web.xml中定义样式

    <servlet>
            <servlet-name>Kaptcha</servlet-name>
            <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
    
            <!-- 是否有边框 -->
            <init-param>
                <param-name>kaptcha.border</param-name>
                <param-value>no</param-value>
            </init-param>
            <!-- 字体颜色 -->
            <init-param>
                <param-name>kaptcha.textproducer.font.color</param-name>
                <param-value>red</param-value>
            </init-param>
            <!-- 图片宽度 -->
            <init-param>
                <param-name>kaptcha.image.width</param-name>
                <param-value>135</param-value>
            </init-param>
            <!-- 使用哪些字符生成验证码 -->
            <init-param>
                <param-name>kaptcha.textproducer.char.string</param-name>
                <param-value>ACDEFHKPRSTWX345679</param-value>
            </init-param>
            <!-- 图片高度 -->
            <init-param>
                <param-name>kaptcha.image.height</param-name>
                <param-value>50</param-value>
            </init-param>
            <!-- 字体大小 -->
            <init-param>
                <param-name>kaptcha.textproducer.font.size</param-name>
                <param-value>43</param-value>
            </init-param>
            <!-- 干扰线的颜色 -->
            <init-param>
                <param-name>kaptcha.noise.color</param-name>
                <param-value>black</param-value>
            </init-param>
            <!-- 字符个数 -->
            <init-param>
                <param-name>kaptcha.textproducer.char.length</param-name>
                <param-value>4</param-value>
            </init-param>
            <!-- 使用哪些字体 -->
            <init-param>
                <param-name>kaptcha.textproducer.font.names</param-name>
                <param-value>Arial</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>Kaptcha</servlet-name>
            <url-pattern>/Kaptcha</url-pattern>
        </servlet-mapping>

    前端

    <li>
        <div class="item-content">
            <div class="item-media">
                <i class="icon icon-form-email"></i>
            </div>
            <div class="item-inner">
                <label for="j_captcha" class="item-title label">验证码</label> <input
                    id="j_captcha" name="j_captcha" type="text"
                    class="form-control in" placeholder="验证码" />
                <div class="item-input">
                    <img id="captcha_img" alt="点击更换" title="点击更换"
                        onclick="changeVerifyCode(this)" src="../Kaptcha" />
                </div>
            </div>
        </div>
    </li>
    function changeVerifyCode(img) {
        img.src = "../Kaptcha?" + Math.floor(Math.random() * 100);
    }

    js

             var verifyCodeActual = $("#j_captcha").val();
             // 判断是否为空
             if (!verifyCodeActual) {
                 $.toast("请输入验证码!");
                 return;
             }
             formData.append("verifyCodeActual", verifyCodeActual);

     后端util类 验证前端输入的验证码

    public class CodeUtil {
        public static boolean checkVerifyCode(HttpServletRequest request) {
            String verifyCodeExpected = (String) request.getSession().getAttribute(
                    com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
            String verifyCodeActual = HttpServletRequestUtil.getString(request, "verifyCodeActual");
            if (verifyCodeActual == null || !verifyCodeActual.equalsIgnoreCase(verifyCodeExpected)) {
                return false;
            }

    return true;
        }
     // 验证码验证
            if(!CodeUtil.checkVerifyCode(request)) {
                modelMap.put("success", false);
                modelMap.put("errMsg", "输入了错误的验证码");
                return modelMap;
            }
  • 相关阅读:
    css--display属性中inline-block与inline的区别
    css基础--常用css属性01
    css基础--简单介绍css
    html常用标签的使用方法
    html概括
    python基础之socket与socketserver
    linux基础命令之sed
    asp.net文件流下载的代码摘要
    window下golang生成静态库给C语言调用
    golang生成c-shared so供c语言或者golang调用到例子
  • 原文地址:https://www.cnblogs.com/lhq1996/p/11934530.html
Copyright © 2020-2023  润新知