Java 腾讯验证码平台使用实例

网友投稿 283 2022-12-15


Java 腾讯验证码平台使用实例

主要就是官方的这个图:

前端调用接口,得到腾讯发过来的几个数据,前端把这几个数据给后端,后端拿到这些数据后传给腾讯,让其判断是否正常,以及其他属性。

程序运行截图如下:

点击登录后,拖动正确进行跳转,拖动错误就重新输入

看看后台的打印:

这个是腾讯反馈的数据,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**";

memmsRIGt 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

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

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" %>

porn.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅谈JAVA 内存流的实现
下一篇:Java获取时间打印到控制台代码实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~