Java 腾讯验证码平台使用实例
主要就是官方的这个图:
前端调用接口,得到腾讯发过来的几个数据,前端把这几个数据给后端,后端拿到这些数据后传给腾讯,让其判断是否正常,以及其他属性。
程序运行截图如下:
点击登录后,拖动正确进行跳转,拖动错误就重新输入
看看后台的打印:
这个是腾讯反馈的数据,response为1说明是正常,风险等级为0
程序结构如下:
源码如下:
LoginServlet.java
importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjava.io.IOException; importjava.net.URLEncoder; importcom.alibaba.fastjson.JSON; importcom.alibaba.fastjson.JSONObject; importorg.apache.http.HttpEntity; importorg.apache.http.client.methods.CloseableHttpResponse; importorg.apache.http.client.methods.HttpGet; importorg.apache.http.impl.client.CloseableHttpClient; importorg.apache.http.impl.client.HttpClients; importorg.apache.http.util.EntityUtils; @WebServlet(value="/login") publicclassLoginServletextendsHttpServlet{ privatestaticfinalStringAPP_ID="xxxxxxxxxx"; privatestaticfinalStringAPP_SECRET="xxxxxxxxxx**"; privatestaticfinalStringVERIFY_URI="https://ssl.captcha.qq.com/ticket/verify?aid=%s&AppSecretKey=%s&Ticket=%s&Randstr=%s&UserIP=%s"; publicstaticintverifyTicket(Stringticket,Stringrand,StringuserIp){ CloseableHttpClienthttpclient=HttpClients.createDefault(); HttpGethttpGet; CloseableHttpResponseresponse=null; try{ httpGet=newHttpGet(String.format(VERIFY_URI, APP_ID, APP_SECRET, URLEncoder.encode(ticket,"UTF-8"), URLEncoder.encode(rand,"UTF-8"), URLEncoder.encode(userIp,"UTF-8") )); response=httpclient.execute(httpGet); HttpEntityentity=response.getEntity(); if(entity!=null){ Stringres=EntityUtils.toString(entity); System.out.println(res);//临时输出 JSONObjectresult=JSON.parseObject(res); //返回码 intcode=result.getInteger("response"); //恶意等级 intevilLevel=result.getInteger("evil_level"); //验证成功 if(code==1)returnevilLevel; } }catch(java.io.IOExceptione){ //忽略 }finally{ try{ response.close(); }catch(Exceptionignore){ } } return-1; } publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ this.doPost(request,response); } protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //验证 Stringticket=request.getParameter("Ticket"); Stringrandstr=request.getParameter("Randstr"); StringuserIP=request.getRemoteAddr(); verifyTicket(ticket,randstr,userIP); response.sendRedirect("success.jsp"); } }
web.xml
ArchetypeCreatedWebApplication
index.jsp
<%@pagecontentType="text/html;charset=UTF-8"language="java"%>functionvail(){ varvailCode=newTencentCaptcha('2047017221',function(res){ if(res.ret==0){ varform=document.getElementById("form1"); varticketInput=document.getElementById("Ticket"); varrandstrInput=document.getElementById("Randstr"); ticketInput.value=res.ticket; randstrInput.value=res.randstr; //console.log("res.ticket:"+res.ticket); //console.log("res.randstr:"+res.randstr); form.submit(); } else{ alert("验证出错!"); } }); vailCode.show(); }
success.jsp
<%@pagecontentType="text/html;charset=UTF-8"language="java"%>Title SUCCESS
porn.xml
4.0.0 org.example wxDemo 1.0-SNAPSHOT war com.alibaba fastjson 1.2.62 org.apache.httpcomponents httpcore 4.4.5 org.apache.httpcomponents httpclient 4.5.11
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。