Java笔记-腾讯验证码平台使用实例
发布日期:2021-06-30 10:41:30
浏览次数:2
分类:技术文章
本文共 4063 字,大约阅读时间需要 13 分钟。
主要就是官方的这个图:
前端调用接口,得到腾讯发过来的几个数据,前端把这几个数据给后端,后端拿到这些数据后传给腾讯,让其判断是否正常,以及其他属性。
程序运行截图如下:
点击登录后,拖动正确进行跳转,拖动错误就重新输入
看看后台的打印:
这个是腾讯反馈的数据,response为1说明是正常,风险等级为0
程序结构如下:
源码如下:
LoginServlet.java
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.net.URLEncoder;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;@WebServlet(value = "/login")public class LoginServlet extends HttpServlet{ private static final String APP_ID = "xxxxxxxxxx"; private static final String APP_SECRET = "xxxxxxxxxx**"; private static final String VERIFY_URI = "https://ssl.captcha.qq.com/ticket/verify?aid=%s&AppSecretKey=%s&Ticket=%s&Randstr=%s&UserIP=%s"; public static int verifyTicket(String ticket, String rand, String userIp) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet; CloseableHttpResponse response = null; try { httpGet = new HttpGet(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); HttpEntity entity = response.getEntity(); if (entity != null) { String res = EntityUtils.toString(entity); System.out.println(res); // 临时输出 JSONObject result = JSON.parseObject(res); // 返回码 int code = result.getInteger("response"); // 恶意等级 int evilLevel = result.getInteger("evil_level"); // 验证成功 if (code == 1) return evilLevel; } } catch (java.io.IOException e) { // 忽略 } finally { try { response.close(); } catch (Exception ignore) { } } return -1; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证 String ticket = request.getParameter("Ticket"); String randstr = request.getParameter("Randstr"); String userIP = request.getRemoteAddr(); verifyTicket(ticket, randstr, userIP); response.sendRedirect("success.jsp"); }}
web.xml
Archetype Created Web Application
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
success.jsp
<%@ page contentType="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
转载地址:https://it1995.blog.csdn.net/article/details/104138143 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月12日 08时50分17秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
听李天飞《大话西游》有感
2019-04-30
走进开源代码(三)
2019-04-30
Linux下开发Qt界面程序时命令行传参数的一个坑
2019-04-30
SourceInsight使用技巧(转)
2019-04-30
QT之旅——post 文件
2019-04-30
树莓派为连接不同Wifi分配固定IP的方法
2019-04-30
[转]Linux 下编译、安装、配置 QT
2019-04-30
新手教学看eMule 0.50a Xtreme 8.0设置
2019-04-30
如何在Linux使用Eclipse + CDT开发C/C++程序?
2019-04-30
Eclipse官网下载页面的Packages 和Developer Builds区别
2019-04-30
在CentOS 6.4安装Qt5.0.1
2019-04-30
深入浅出TCP之send和recv
2019-04-30
yum和apt-get的区别
2019-04-30
vim中文帮助的安装
2019-04-30
linux下获取所有文件夹和文件,支持nfs和xfs
2019-04-30
用分区魔术师把linux所占的分区删除后重写mbr
2019-04-30
软件架构师书籍
2019-04-30
Java程序员到架构师的推荐阅读书籍
2019-04-30
LFS、BLFS、ALFS、HLFS的区别
2019-04-30
国外知名网站评出对程序员最具影响力的图书(附下载)
2019-04-30