本文主要是介绍java实现 腾讯人机验证 + 前端,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
二话不说先上效果图:
前端
新建一个html页面内容为:
<!DOCTYPE html>
<html>
<head><title>腾讯人机校验</title>
</head>
<body><button id="TencentCaptcha" data-appid="1253632833" data-cbfn="callback" class="layui-btn layui-block" lay-filter="login" lay-submit="">点击校验</button>
</body><script src="https://ssl.captcha.qq.com/TCaptcha.js"></script><script>window.callback = function(res){console.log(res); if(res.ret == 0){//将ticket、randstr传至后台校验} }
</script>
</html>
注意点:
1、引入包:
<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
2、登录按钮:
<button id="TencentCaptcha" data-appid="1253632833" data-cbfn="callback" class="layui-btn layui-block" lay-filter="login" lay-submit="">点击校验</button>
后端
1、参数配置:
qq.captcha.open= false
qq.captcha.url= https://ssl.captcha.qq.com/ticket/verify
qq.captcha.aid= **********
qq.captcha.AppSecretKey= ************
2、工具校验类:
public Boolean check(String ticket,String randstr) throws Exception {Map<String, String> paramsMap= new HashMap<>();paramsMap.put("aid", ******);//配置文件配置的qq.captcha.aidparamsMap.put("AppSecretKey", *****);//配置文件配置的qq.captcha.AppSecretKeyparamsMap.put("Ticket", ticket);paramsMap.put("Randstr", randstr);paramsMap.put("UserIP", IPUtils.getIpAddr());String msg = HttpUtils.get(captcha.getUrl(), paramsMap);/*** response: 1:验证成功,0:验证失败,100:AppSecretKey参数校验错误[required]* evil_level:[0,100],恶意等级[optional]* err_msg:验证错误信息[optional]*/System.out.println(msg);JSONObject json = JSONObject.parseObject(msg);String response = (String) json.get("response");if("1".equals(response)){return true;}else{return false;}}
注意:
aid和AppSecretKey在腾讯云上可查看:https://console.cloud.tencent.com/cam/capi
这篇关于java实现 腾讯人机验证 + 前端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!