Java 騰訊驗證碼平臺使用實例
主要就是官方的這個圖:

前端調(diào)用接口,得到騰訊發(fā)過來的幾個數(shù)據(jù),前端把這幾個數(shù)據(jù)給后端,后端拿到這些數(shù)據(jù)后傳給騰訊,讓其判斷是否正常,以及其他屬性。
程序運行截圖如下:

點擊登錄后,拖動正確進行跳轉(zhuǎn),拖動錯誤就重新輸入

看看后臺的打?。?/p>

這個是騰訊反饋的數(shù)據(jù),response為1說明是正常,風(fēng)險等級為0
程序結(jié)構(gòu)如下:

源碼如下:
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
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>qq</title>
<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
<script type="text/javascript">
function vail(){
var vailCode = new TencentCaptcha('2047017221', function(res){
if(res.ret == 0){
var form = document.getElementById("form1");
var ticketInput = document.getElementById("Ticket");
var randstrInput = 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();
}
</script>
</head>
<body>
<form id="form1" method="post" action="login">
<div>
<input id="Ticket" name="Ticket" type="hidden" value="">
<input id="Randstr" name="Randstr" type="hidden" value="">
<input type="button" value="登錄" id="btnOK" οnclick="vail()" />
</div>
</form>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>SUCCESS</h1> </body> </html>
porn.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>wxDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.11</version>
</dependency>
</dependencies>
</project>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring事務(wù)Propagation及其實現(xiàn)原理詳解
這篇文章主要介紹了spring事務(wù)Propagation及其實現(xiàn)原理詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
詳解springboot采用多數(shù)據(jù)源對JdbcTemplate配置的方法
在本篇文章中我們給大家詳細分享了springboot采用多數(shù)據(jù)源對JdbcTemplate配置的方法,有需要的朋友們可以學(xué)習(xí)參考下。2018-10-10
將本地jar包安裝進入maven倉庫(實現(xiàn)方法)
下面小編就為大家?guī)硪黄獙⒈镜豭ar包安裝進入maven倉庫(實現(xiàn)方法)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

