关于验证码:一款漂亮的Java行为验证码

4次阅读

共计 2102 个字符,预计需要花费 6 分钟才能阅读完成。

前言

Java 图形验证码,反对自定义图片、中文、算术等类型,可用于 Java Web、JavaSE 等我的项目。真香!

截图展现

我的项目集成

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 = "xxx";
            appSecret = "xxx";
            KgCaptchaSDK KgRequest = new KgCaptchaSDK(appId, appSecret);
                
            // 前端验证胜利后颁发的 token,有效期为两分钟
            KgRequest.token = request.getParameter("kgCaptchaToken");
            // System.out.print(KgRequest.token);
    
            // 填写应用服务域名,在利用治理中获取
            KgRequest.appCdn = "xxx";
    
            // 申请超时工夫,秒
            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<String, String> 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/

正文完
 0