Skip to content

Commit

Permalink
两次验证中增加userid
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyuhui1993 committed Mar 1, 2016
1 parent 80139b3 commit 8878a95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
48 changes: 43 additions & 5 deletions src/com/geetest/sdk/java/GeetestLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public class GeetestLib {

protected final String verName = "3.1.0";// SDK版本编号
protected final String verName = "3.2.0";// SDK版本编号
protected final String sdkLang = "java";// SD的语言类型

protected final String apiUrl = "http://api.geetest.com"; //极验验证API URL
Expand Down Expand Up @@ -59,7 +59,10 @@ public class GeetestLib {
/**
* 私钥
*/

private String privateKey = "";

private String userId = "";

private String responseStr = "";

Expand Down Expand Up @@ -143,6 +146,20 @@ public int preProcess() {
return 1;

}

/**
* 验证初始化预处理
*
* @param userid
* @return 1表示初始化成功,0表示初始化失败
*/
public int preProcess(String userid){

this.userId = userid;
return this.preProcess();
}



/**
* 用captchaID进行注册,更新challenge
Expand All @@ -152,6 +169,10 @@ public int preProcess() {
private int registerChallenge() {
try {
String GET_URL = apiUrl + registerUrl+"?gt=" + this.captchaId;
if (this.userId != ""){
GET_URL = GET_URL + "&user_id=" + this.userId;
this.userId = "";
}
gtlog("GET_URL:" + GET_URL);
String result_str = readContentFromGet(GET_URL);
gtlog("register_result:" + result_str);
Expand All @@ -165,7 +186,7 @@ private int registerChallenge() {
return 0;
}
} catch (Exception e) {
gtlog("exception:register api:");
gtlog("exception:register api");
}
return 0;
}
Expand Down Expand Up @@ -249,7 +270,6 @@ private boolean resquestIsLegal(String challenge, String validate, String seccod
}



/**
* 服务正常的情况下使用的验证方式,向gt-server进行二次验证,获取验证结果
*
Expand All @@ -271,7 +291,11 @@ public int enhencedValidateRequest(String challenge, String validate, String sec
String query = String.format("seccode=%s&sdk=%s", seccode,
(this.sdkLang + "_" + this.verName));
String response = "";


if (this.userId != ""){
query = query + "&user_id=" + this.userId;
this.userId = "";
}
gtlog(query);
try {
if (validate.length() <= 0) {
Expand All @@ -296,7 +320,21 @@ public int enhencedValidateRequest(String challenge, String validate, String sec
} else {
return 0;
}

}

/**
* 服务正常的情况下使用的验证方式,向gt-server进行二次验证,获取验证结果
*
* @param challenge
* @param validate
* @param seccode
* @param userid
* @return 验证结果,1表示验证成功0表示验证失败
*/
public int enhencedValidateRequest(String challenge, String validate, String seccode, String userid) {

this.userId = userid;
return this.enhencedValidateRequest(challenge, validate, seccode);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/com/geetest/sdk/java/web/demo/StartCaptchaServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ protected void doGet(HttpServletRequest request,
GeetestLib gtSdk = new GeetestLib(GeetestConfig.getCaptcha_id(), GeetestConfig.getPrivate_key());

String resStr = "{}";

//自定义userid
String userid = "test";

//进行验证预处理
int gtServerStatus = gtSdk.preProcess();
int gtServerStatus = gtSdk.preProcess(userid);

//将服务器状态设置到session中
request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
//将userid设置到session中
request.getSession().setAttribute("userid", userid);

resStr = gtSdk.getResponseStr();

Expand Down
5 changes: 4 additions & 1 deletion src/com/geetest/sdk/java/web/demo/VerifyLoginServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ protected void doPost(HttpServletRequest request,
//从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);
gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, userid);
System.out.println(gtResult);
} else {
// gt-server非正常情况下,进行failback模式验证
Expand Down

0 comments on commit 8878a95

Please sign in to comment.