前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一款漂亮的Java行为验证码

一款漂亮的Java行为验证码

原创
作者头像
用户10417561
发布2023-04-24 09:15:42
4210
发布2023-04-24 09:15:42
举报
文章被收录于专栏:norhonenorhone

前言

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

截图展示

项目集成

代码语言:javascript
复制
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/

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 截图展示
  • 项目集成
  • 最后
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档