验证码实现(google)

Posted lhq1996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码实现(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;
        }
 // 验证码验证
        if(!CodeUtil.checkVerifyCode(request)) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "输入了错误的验证码");
            return modelMap;
        }

 

return true;
    }

 

以上是关于验证码实现(google)的主要内容,如果未能解决你的问题,请参考以下文章

time-based基于google key生成6位验证码(google authenticator)

Android SMS Verification API 结果码始终为 0

状态码 12501 使用 google 登录进行身份验证

Google Home 授权代码和使用 Google 帐户进行身份验证

php生成各种验证码

JSP 设计教师与学生不同登陆界面(带验证码)