概述

该SDK是为使用java作为服务端语言的服务器使用的,其中3.0验证安全系统的java SDK目前提供基于java servlet 的DEMO。

环境需求

条目
兼容性 JAVA SE 1.6及以上
sdk依赖 java-json.jar
服务器 tomcat 7及以上
demo依赖 servlet-api.jar

资源下载

条目
产品结构流程 通讯流程
SDK下载链接 gt3-java-sdk

安装

下载SDK

使用命令从Github获取

git clone https://github.com/GeeTeam/gt3-java-sdk.git

手动下载获取

使用从github下载.zip文件获取最新的sdk

Github: gt3-java-sdk

引入

将项目目录下src/sdk/GeetestLib.java拷入你的项目中

在您的项目中使用以下代码导入极验3.0验证安全系统 java SDK

import sdk.GeetestLib

配置接口

GeetestLib共有四个接口,分别是:

  1. 预处理接口preProcess
  2. 获取验证字符串接口getResponseStr
  3. 二次验证接口enhencedValidateRequest
  4. 本地二次验证接口failbackValidateRequest

在调用GeetestLib前请正确配置从极验3.0后台注册获取的公钥(id)和私钥(key)

private static final String geetest_id = "你的公钥";
private static final String geetest_key = "你的私钥";

代码示例

初始化及获取字符串(API1)

public class StartCaptchaServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(), GeetestConfig.isnewfailback());
String resStr = "{}";
//自定义userid
String userid = "test";
//进行验证预处理
int gtServerStatus = gtSdk.preProcess(userid);
//将服务器状态设置到session中
request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
//将userid设置到session中
request.getSession().setAttribute("userid", userid);
resStr = gtSdk.getResponseStr();
PrintWriter out = response.getWriter();
out.println(resStr);
}
}

二次验证及宕机(API2)

public class VerifyLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(), GeetestConfig.isnewfailback());
String challenge = request.getParameter(GeetestLib.fn_geetest_challenge);
String validate = request.getParameter(GeetestLib.fn_geetest_validate);
String seccode = request.getParameter(GeetestLib.fn_geetest_seccode);
//从session中获取gt-server状态
int gt_server_status_code = (Integer) request.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);
//从session中获取userid
String userid = (String)request.getSession().getAttribute("userid");
int gtResult = 0;
if (gt_server_status_code == 1) {
//gt-server正常,向gt-server进行二次验证
gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, userid);
System.out.println(gtResult);
} else {
// gt-server非正常情况下,进行failback模式验证
System.out.println("failback:use your own server captcha validate");
gtResult = gtSdk.failbackValidateRequest(challenge, validate, seccode);
System.out.println(gtResult);
}
if (gtResult == 1) {
// 验证成功
PrintWriter out = response.getWriter();
JSONObject data = new JSONObject();
try {
data.put("status", "success");
data.put("version", gtSdk.getVersionInfo());
} catch (JSONException e) {
e.printStackTrace();
}
out.println(data.toString());
}
else {
// 验证失败
JSONObject data = new JSONObject();
try {
data.put("status", "fail");
data.put("version", gtSdk.getVersionInfo());
} catch (JSONException e) {
e.printStackTrace();
}
PrintWriter out = response.getWriter();
out.println(data.toString());
}
}
}

运行demo

将demo导出成war包,放到tomcat 7目录下的webapps文件中,启动tomcat

在浏览器中访问http://localhost:8080/gt-java-sdk/login.jsp即可看到Demo界面。