Java行为验证码

前言

Java是一种流行的编程语言,验证码是一种常用的网络安全技术。Java发展至今,网上也出现了各种各样的验证码,本人初学Java,下面是我用Java实现短信验证码的总结。

1

项目集成

后台接收前台的kgCaptchaToken进行验证,验证成功执行成功处理,验证失败返回错误代码及信息。

package com.kyger;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class demo extends HttpServlet {
    private static final long serialVersionUID = 1L; 

    public demo() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");;
        response.setContentType("text/html; charset=utf-8");

        // 后台处理
        if (request.getMethod().equals("POST")){
            String html, appId, appSecret;

            // 设置 AppId 及 AppSecret,在应用管理中获取
            appId = "AppId";
            appSecret = "AppSecret";
            KgCaptchaSDK KgRequest = new KgCaptchaSDK(appId, appSecret);

            // 前端验证成功后颁发的 token,有效期为两分钟
            KgRequest.token = request.getParameter("kgCaptchaToken");
            // System.out.print(KgRequest.token);

            // 填写应用服务域名,在应用管理中获取
            KgRequest.appCdn = "appCdn";

            // 请求超时时间,秒
            KgRequest.connectTimeout = 5;

            // 用户登录或尝试帐号,当安全策略中的防控等级为3时必须填写,一般情况下可以忽略
            // 可以填写用户输入的登录帐号(如:request.getParameter("username"),可拦截同一帐号多次尝试等行为
            KgRequest.userId = "kgCaptchaDemo";

            // request 对象,当安全策略中的防控等级为3时必须填写,一般情况下可以忽略
            KgRequest.request = request;
            // java 环境中无法提供 request 对象,请分别定义:clientIp|clientBrowser|domain 参数,即:
            // KgRequest.clientIp = "127.0.0.1";  // 填写客户端IP
            // KgRequest.clientBrowser = "";  // 客户端浏览器信息
            // KgRequest.domain = "http://localhost";  // 你的授权域名或服务IP        

            // 发送验证请求
            Map requestResult = KgRequest.sendRequest();
            if("0".toString().equals(requestResult.get("code"))) {
                // 验签成功逻辑处理 ***

                // 这里做验证通过后的数据处理
                // 如登录/注册场景,这里通常查询数据库、校验密码、进行登录或注册等动作处理
                // 如短信场景,这里可以开始向用户发送短信等动作处理
                // ...

                html = "<script>alert('验证通过');history.back();</script>";
            } else {
                // 验签失败逻辑处理
                html = "<script>alert(\"" + requestResult.get("msg") + " - " + requestResult.get("code") + "\");history.back();</script>";
            }        

            response.getWriter().append(html);
        } else {
            response.sendRedirect("index.html");
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

最后

SDK开源地址:https://github.com/KgCaptcha,顺便做了一个演示:https://www.kgcaptcha.com/demo/

评论

暂无评论