主要就是官方的这个图:
前端调用接口,得到腾讯发过来的几个数据,前端把这几个数据给后端,后端拿到这些数据后传给腾讯,让其判断是否正常,以及其他属性。
程序运行截图如下:
点击登录后,拖动正确进行跳转,拖动错误就重新输入
看看后台的打印:
这个是腾讯反馈的数据,响应为1说明是正常,风险等级为0
程序结构如下:
源码如下:
LoginServlet.java
进口javax.servlet.ServletException; 进口javax.servlet.annotation.WebServlet; 进口javax.servlet.http.HttpServlet; 进口javax.servlet.http.HttpServletRequest; 进口javax.servlet.http.HttpServletResponse; 进口java.io.IOException; 进口java.net.URLEncoder; 进口com.alibaba.fastjson.JSON; 进口com.alibaba.fastjson.JSONObject; 进口org.apache.http.HttpEntity; 进口org.apache.http.client.methods.CloseableHttpResponse; 进口org.apache.http.client.methods.HttpGet; 进口org.apache.http.impl.client.CloseableHttpClient; 进口org.apache.http.impl.client.HttpClients; 进口org.apache.http.util.EntityUtils; @WebServlet (value=" https://www.yisu.com/login ") 公开课LoginServlet扩展HttpServlet { 私有静态最终字符串APP_ID=皒xxxxxxxxx”; 私有静态最终字符串APP_SECRET=皒xxxxxxxxx * *”; 私有静态最终字符串VERIFY_URI=" https://ssl.captcha.qq.com/ticket/verify& # 63;援助=% s& AppSecretKey=% s&票=% s& Randstr=% s& UserIP=% s”; 公共静态int verifyTicket (String票,兰德的字符串,字符串userIp) { CloseableHttpClient httpclient=HttpClients.createDefault (); HttpGet HttpGet; CloseableHttpResponse响应=零; 尝试{ httpGet=new httpGet (String.format (VERIFY_URI APP_ID, APP_SECRET, URLEncoder。编码(票,“utf - 8”), URLEncoder。编码(兰德,“utf - 8”), URLEncoder。编码(userIp“utf - 8”) )); 响应=httpclient.execute (httpGet); HttpEntity实体=response.getEntity (); 如果(实体!=null) { 字符串res=EntityUtils.toString(实体); System.out.println (res);//临时输出 JSONObject结果=JSON.parseObject (res);//返回码 int代码=result.getInteger(“响应”);//恶意等级 int evilLevel=result.getInteger (“evil_level”);//验证成功 如果(代码==1)返回evilLevel; } }(io。IOException e) {//忽略 最后}{ 尝试{ response.close (); }捕捉(异常忽略){ } } 返回1; } 公共空间doGet (HttpServletRequest请求,HttpServletResponse响应) 抛出ServletException IOException { 这一点。doPost(请求、响应); } 保护无效doPost (HttpServletRequest请求,HttpServletResponse响应) 抛出ServletException IOException {//验证 字符串票=request.getParameter(“票”); 字符串randstr=request.getParameter (“randstr”); 字符串userIP=request.getRemoteAddr (); verifyTicket(机票、randstr userIP); response.sendRedirect (“success.jsp”); } }
web . xml
& lt; !DOCTYPE web公共 ”——//Sun Microsystems公司//DTD Web应用程序2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd”在 & lt; web-app> & lt; display-name>原型创建Web Application & lt;/web-app>
index . jsp
% @ & lt;页面contentType=" text/html; charset=utf - 8”语言=癹ava”%比; & lt; !DOCTYPE HTML公共”——//W3C//DTD HTML 4.01过渡//EN”比; & lt; html> & lt; head> & lt; title> qq & lt;脚本src=" https://ssl.captcha.qq.com/TCaptcha.js "祝辞& lt;/script> & lt;脚本type=" text/javascript祝辞 函数维尔(){ var vailCode=new TencentCaptcha(' 2047017221 ',函数(res) { 如果(res)。ret==0) { var形式=. getelementbyid (“form1”); var ticketInput=. getelementbyid(“票”); var randstrInput=. getelementbyid (“Randstr”); ticketInput。值=https://www.yisu.com/zixun/res.ticket; randstrInput。值=res.randstr;//console.log (“res。票:“+ res.ticket);//console.log (“res。randstr:“+ res.randstr); form.submit (); } 其他{ alert("验证出错!”); } }); vailCode.show (); } & lt;/script> & lt;/head> & lt; body> & lt;形式id=癴orm1”方法=皃ost”行动=暗锹肌北? & lt; div> & lt;输入id=捌薄泵?捌薄崩嘈?耙亍钡募壑?薄氨? & lt;输入id=" Randstr " name=癛andstr”类型=耙亍钡募壑?薄氨? & lt;输入类型="按钮" value=" https://www.yisu.com/zixun/登录" id=" btnOK "οnclick=拔?)”/比; & lt;/div> & lt;/form> & lt;/body> & lt;/html>Java腾讯验证码平台使用实例