极限验证

来源:互联网 发布:天津数据恢复 编辑:程序博客网 时间:2024/04/27 22:36
首先是官方的demo
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!doctype html><html><head><base href="<%=basePath%>"><title>极意网络</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style>body {background-color: #FEFEFE;}.wrap {width: 960px;margin: 100px auto;font-size: 125%;}.row {margin: 30px 0;}</style><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script></head><body><div class="wrap"><h1>JavaEE站点安装Demo页面</h1><form method="post" action="VerifyLoginServlet"><div class="row"><label for="name">邮箱</label> <input type="text" id="email"name="email" value="geetest@126.com" /></div><div class="row"><label for="passwd">密码</label> <input type="password" id="passwd"name="passwd" value="gggggggg" /></div><%--Start  Code--%><div class="row"><div id="div_geetest_lib"></div><div id="div_id_embed"></div><%--End  Code--%><div class="row"><input type="submit" value="登录" id="submit-button" /></div><script type="text/javascript">function geetest_ajax_results() {//TODO, not necessory a geetest ajax demo,$.ajax({url : "/todo/VerifyLoginServlet",//todo:set the servelet of your owntype : "post",data : gt_captcha_obj.getValidate(),success : function(sdk_result) {console.log(sdk_result)}});}var gtFailbackFrontInitial = function(result) {var s = document.createElement('script');s.id = 'gt_lib';s.src = 'http://static.geetest.com/static/js/geetest.0.0.0.js';s.charset = 'UTF-8';s.type = 'text/javascript';document.getElementsByTagName('head')[0].appendChild(s);var loaded = false;s.onload = s.onreadystatechange = function() {if (!loaded&& (!this.readyState|| this.readyState === 'loaded' || this.readyState === 'complete')) {loadGeetest(result);loaded = true;}};}//get  geetest server status, use the failback solutionvar loadGeetest = function(config) {//1. use geetest captchawindow.gt_captcha_obj = new window.Geetest({gt : config.gt,challenge : config.challenge,product : 'embed',offline : !config.success});gt_captcha_obj.appendTo("#div_id_embed");//Ajax request demo,if you use submit form ,then ignore it gt_captcha_obj.onSuccess(function() {geetest_ajax_results()});}s = document.createElement('script');s.src = 'http://api.geetest.com/get.php?callback=gtcallback';$("#div_geetest_lib").append(s);var gtcallback =( function() {var status = 0, result, apiFail;return function(r) {status += 1;if (r) {result = r;setTimeout(function() {if (!window.Geetest) {apiFail = true;gtFailbackFrontInitial(result)}}, 1000)}else if(apiFail) {return}if (status == 2) {loadGeetest(result);}}})()$.ajax({url : "StartCaptchaServlet",type : "get",dataType : 'JSON',success : function(result) {gtcallback(result)}})</script></div></form></div></body></html>
后端代码
package com.geetest.sdk.java;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.InetAddress;import java.net.Socket;import java.net.URL;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.HashMap;import javax.servlet.http.HttpServletRequest;/** * Java SDK *  * @author Zheng * @time 2014年7月10日 下午3:29:09 */public class GeetestLib {/** * SDK版本编号 */// private final int verCode = 8;/** * SDK版本名称 */protected final String verName = "2.15.10.9.1";protected final String sdkLang = "java";// SD的语言类型protected final static String gt_session_key = "geetest";// geetest对象存储的session的key值(单实例)protected final static String gt_server_status_session_key = "gt_server_status";// 极验服务器状态key值(单实例)protected final String baseUrl = "api.geetest.com";protected final String api_url = "http://" + baseUrl;protected final String https_api_url = "https://" + baseUrl;// 一些页面是httpsprotected final int com_port = 80;// 通讯端口号protected final int defaultIsMobile = 0;// private final int defaultMobileWidth = 260;// the default width of the// mobile id// 一些常量public static final String success_res = "success";public static final String fail_res = "fail";public static final String forbidden_res = "forbidden";// 前端验证的表单值--属于接口,不允许修改protected final String fn_geetest_challenge = "geetest_challenge";protected final String fn_geetest_validate = "geetest_validate";protected final String fn_geetest_seccode = "geetest_seccode";protected Boolean debugCode = true;// 调试开关,是否输出调试日志protected String validateLogPath = "";// 服务器端保存日志的目录//var/log/,请确保有可读写权限/** * 公钥 */private String captchaId = "";/** * 私钥 */private String privateKey = "";/** * the challenge */private String challengeId = "";/** * set the own private pictures,default is "" */private String picId = "";/** * he captcha product type,default is 'embed' */private String productType = "embed";/** * is secure */private Boolean isHttps = false;public Boolean getIsHttps() {return isHttps;}public void setIsHttps(Boolean isHttps) {this.isHttps = isHttps;}/** * when the productType is popup,it needs to set the submitbutton */private String submitBtnId = "submit-button";public String getSubmitBtnId() {return submitBtnId;}public void setSubmitBtnId(String submitBtnId) {this.submitBtnId = submitBtnId;}/** * 是否是移动端的 */private int isMobile = defaultIsMobile;// 1--true,0-falsepublic String getChallengeId() {return challengeId;}public void setChallengeId(String challengeId) {this.challengeId = challengeId;}public final Boolean getDebugCode() {return debugCode;}public final void setDebugCode(Boolean debugCode) {this.debugCode = debugCode;}/** * 获取版本编号 *  * @author Zheng * @email dreamzsm@gmail.com * @time 2014年7月11日 上午11:07:11 * @return */public String getVersionInfo() {return verName;}public String getValidateLogPath() {return validateLogPath;}public void setValidateLogPath(String validateLogPath) {this.validateLogPath = validateLogPath;}// public void setCaptcha_id(String captcha_id) {// this.captcha_id = captcha_id;// }/** * 一个无参构造函数 */public GeetestLib() {}// public static GeetestLib createGtInstance() {// GeetestLib geetestSdk = new GeetestLib();// geetestSdk.setCaptchaId(GeetestConfig.getCaptcha_id());// geetestSdk.setPrivateKey(GeetestConfig.getPrivate_key());//// return geetestSdk;// }/** * 将当前实例设置到session中 *  * @param request */public void setGtSession(HttpServletRequest request) {request.getSession().setAttribute(gt_session_key, this);// set sessionthis.gtlog("set session succeed");}/** * 同一会话多实例时,设置session *  * @param request * @param gt_instance_session_key *            不同验证实例设置的key */public void setGtSession(HttpServletRequest request,String gt_instance_session_key) {request.getSession().setAttribute(gt_instance_session_key, this);// set// sessionthis.gtlog("set session succeed");}/** * 极验服务器的gt-server状态值 *  * @param request */public void setGtServerStatusSession(HttpServletRequest request,int statusCode) {request.getSession().setAttribute(gt_server_status_session_key,statusCode);// set session}/** * 极验服务器的gt-server状态值(多实例) *  * @param request * @param statusCode * @param gt_instance_server_status_session_key */public void setGtServerStatusSession(HttpServletRequest request,int statusCode, String gt_instance_server_status_session_key) {request.getSession().setAttribute(gt_instance_server_status_session_key, statusCode);// set// session}/** * 获取session *  * @param request * @return */public static GeetestLib getGtSession(HttpServletRequest request) {return (GeetestLib) request.getSession().getAttribute(gt_session_key);}/** * 获取session(用于同一会话多实例模式下,做的区分) *  * @param request * @param gt_instance_session_key * @return */public static GeetestLib getGtSession(HttpServletRequest request,String gt_instance_session_key) {return (GeetestLib) request.getSession().getAttribute(gt_instance_session_key);}/** * 0表示不正常,1表示正常 *  * @param request * @return */public static int getGtServerStatusSession(HttpServletRequest request) {return (Integer) request.getSession().getAttribute(gt_server_status_session_key);}/** * 获取session(用于同一会话多实例模式下,做的区分) *  * @param request * @param gt_instance_server_status_session_key * @return */public static int getGtServerStatusSession(HttpServletRequest request,String gt_instance_server_status_session_key) {return (Integer) request.getSession().getAttribute(gt_instance_server_status_session_key);}/** * 预处理失败后的返回格式串 *  * @return */public String getFailPreProcessRes() {// return String.format("{\"success\":%s}", 0);Long rnd1 = Math.round(Math.random() * 100);Long rnd2 = Math.round(Math.random() * 100);String md5Str1 = md5Encode(rnd1 + "");String md5Str2 = md5Encode(rnd2 + "");String challenge = md5Str1 + md5Str2.substring(0, 2);this.setChallengeId(challenge);return String.format("{\"success\":%s,\"gt\":\"%s\",\"challenge\":\"%s\"}", 0,this.getCaptchaId(), this.getChallengeId());}/** * 预处理成功后的标准串 *  * @return */public String getSuccessPreProcessRes() {return String.format("{\"success\":%s,\"gt\":\"%s\",\"challenge\":\"%s\"}", 1,this.getCaptchaId(), this.getChallengeId());}/** * 保存验证的日志,方便后续和极验做一些联调工作,用于可能有前端验证通过,但是后面验证失败的情况 *  * @param challenge * @param validate * @param seccode * @param gtUser *            用户页面的cookie标识 * @param sdkResult */public void saveValidateLog(String challenge, String validate,String seccode, String sdkResult) {SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss");String date = sDateFormat.format(new java.util.Date());String logFormat = String.format("date:%s,challenge:%s,validate:%s,seccode:%s,sdkResult:%s",date, challenge, validate, seccode, sdkResult);gtlog(logFormat);}public String getPicId() {return picId;}public void setPicId(String picId) {this.picId = picId;}public String getProductType() {return productType;}public void setProductType(String productType) {this.productType = productType;}public int getIsMobile() {return isMobile;}public void setIsMobile(int isMobile) {this.isMobile = isMobile;}public String getPrivateKey() {return privateKey;}public void setPrivateKey(String privateKey) {this.privateKey = privateKey;}public GeetestLib(String privateKey) {this.privateKey = privateKey;}// public GeetestLib(String privateKey, String captcha_id) {// this.privateKey = privateKey;// this.captcha_id = captcha_id;// }// public int getVerCode() {// return verCode;// }public String getVerName() {return verName;}public String getCaptchaId() {return captchaId;}public void setCaptchaId(String captchaId) {this.captchaId = captchaId;}/** * processing before the captcha display on the web front *  * @return */public int preProcess() {// first check the server status , to handle failback// if (getGtServerStatus() != 1) {// return 0;// }// just check the server side registerif (registerChallenge() != 1) {return 0;}return 1;}/** * generate the dynamic front source *  * @param different *            product display mode :float,embed,popup * @return */public String getGtFrontSource() {String base_path = "";if (this.isHttps) {base_path = this.https_api_url;} else {base_path = this.api_url;}String frontSource = String.format("<script type=\"text/javascript\" src=\"%s/get.php?"+ "gt=%s&challenge=%s", base_path, this.captchaId,this.challengeId);if (this.productType.equals("popup")) {frontSource += String.format("&product=%s&popupbtnid=%s",this.productType, this.submitBtnId);} else {frontSource += String.format("&product=%s", this.productType);}frontSource += "\"></script>";return frontSource;}/** * 获取极验的服务器状态 *  * @author Zheng * @email dreamzsm@gmail.com * @time 2014年7月10日 下午7:12:38 * @return */public int getGtServerStatus() {try {final String GET_URL = api_url + "/check_status.php";if (readContentFromGet(GET_URL).equals("ok")) {return 1;} else {System.out.println("gServer is Down");return 0;}} catch (Exception e) {e.printStackTrace();}return 0;}/** * generate a random num *  * @return */public int getRandomNum() {int rand_num = (int) (Math.random() * 100);// System.out.print(rand_num);return rand_num;}/** * Register the challenge *  * @return */public int registerChallenge() {try {String GET_URL = api_url + "/register.php?gt=" + this.captchaId;// if (this.productType.equals("popup")) {// GET_URL += String.format("&product=%s&popupbtnid=%s",// this.productType, this.submitBtnId);// } else {// GET_URL += String.format("&product=%s", this.productType);// }// System.out.print(GET_URL);String result_str = readContentFromGet(GET_URL);// System.out.println(result_str);if (32 == result_str.length()) {this.challengeId = result_str;return 1;} else {System.out.println("gServer register challenge failed");return 0;}} catch (Exception e) {gtlog("exception:register api:");// e.printStackTrace();}return 0;}/** * 读取服务器 *  * @author Zheng dreamzsm@gmail.com * @time 2014年7月10日 下午7:11:11 * @param getURL * @return * @throws IOException */private String readContentFromGet(String getURL) throws IOException {URL getUrl = new URL(getURL);HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();connection.setConnectTimeout(2000);// 设置连接主机超时(单位:毫秒)connection.setReadTimeout(2000);// 设置从主机读取数据超时(单位:毫秒)// 建立与服务器的连接,并未发送数据connection.connect();// 发送数据到服务器并使用Reader读取返回的数据StringBuffer sBuffer = new StringBuffer();InputStream inStream = null;byte[] buf = new byte[1024];inStream = connection.getInputStream();for (int n; (n = inStream.read(buf)) != -1;) {sBuffer.append(new String(buf, 0, n, "UTF-8"));}inStream.close();connection.disconnect();// 断开连接return sBuffer.toString();}/** * 判断一个表单对象值是否为空 *  * @time 2014年7月10日 下午5:54:25 * @param gtObj * @return */protected boolean objIsEmpty(Object gtObj) {if (gtObj == null) {return true;}if (gtObj.toString().trim().length() == 0) {return true;}// && gtObj.toString().trim().length() > 0return false;}/** * 检查客户端的请求是否为空--三个只要有一个为空,则判断不合法 *  * @time 2014年7月10日 下午5:46:34 * @param request * @return */public boolean resquestIsLegal(HttpServletRequest request) {if (objIsEmpty(request.getParameter(this.fn_geetest_challenge))) {return false;}if (objIsEmpty(request.getParameter(this.fn_geetest_validate))) {return false;}if (objIsEmpty(request.getParameter(this.fn_geetest_seccode))) {return false;}return true;}/** * 检验验证请求 传入的参数为request--vCode 8之后不再更新,不推荐使用 *  * @time 2014年7月10日 下午6:34:55 * @param request * @return */public boolean validateRequest(HttpServletRequest request) {boolean gtResult = this.validate(request.getParameter(this.fn_geetest_challenge),request.getParameter(this.fn_geetest_validate),request.getParameter(this.fn_geetest_seccode));return gtResult;}/** * failback使用的验证方式 *  * @param request * @return */public String failbackValidateRequest(HttpServletRequest request) {gtlog("in failback validate");if (!resquestIsLegal(request)) {return GeetestLib.fail_res;}String challenge = request.getParameter(this.fn_geetest_challenge);String validate = request.getParameter(this.fn_geetest_validate);// String seccode = request.getParameter(this.fn_geetest_seccode);if (!challenge.equals(this.getChallengeId())) {return GeetestLib.fail_res;}String[] validateStr = validate.split("_");String encodeAns = validateStr[0];String encodeFullBgImgIndex = validateStr[1];String encodeImgGrpIndex = validateStr[2];gtlog(String.format("encode----challenge:%s--ans:%s,bg_idx:%s,grp_idx:%s",challenge, encodeAns, encodeFullBgImgIndex, encodeImgGrpIndex));int decodeAns = decodeResponse(this.getChallengeId(), encodeAns);int decodeFullBgImgIndex = decodeResponse(this.getChallengeId(),encodeFullBgImgIndex);int decodeImgGrpIndex = decodeResponse(this.getChallengeId(),encodeImgGrpIndex);gtlog(String.format("decode----ans:%s,bg_idx:%s,grp_idx:%s", decodeAns,decodeFullBgImgIndex, decodeImgGrpIndex));String validateResult = validateFailImage(decodeAns,decodeFullBgImgIndex, decodeImgGrpIndex);if (!validateResult.equals(GeetestLib.fail_res)) {// 使用一随机标识来丢弃掉此次验证,防止重放Long rnd1 = Math.round(Math.random() * 100);String md5Str1 = md5Encode(rnd1 + "");this.setChallengeId(md5Str1);}return validateResult;}/** * * @param ans * @param full_bg_index * @param img_grp_index * @return */private String validateFailImage(int ans, int full_bg_index,int img_grp_index) {final int thread = 3;// 容差值String full_bg_name = md5Encode(full_bg_index + "").substring(0, 9);String bg_name = md5Encode(img_grp_index + "").substring(10, 19);String answer_decode = "";// 通过两个字符串奇数和偶数位拼接产生答案位for (int i = 0; i < 9; i++) {if (i % 2 == 0) {answer_decode += full_bg_name.charAt(i);} else if (i % 2 == 1) {answer_decode += bg_name.charAt(i);} else {gtlog("exception");}}String x_decode = answer_decode.substring(4, answer_decode.length());int x_int = Integer.valueOf(x_decode, 16);// 16 to 10int result = x_int % 200;if (result < 40) {result = 40;}if (Math.abs(ans - result) <= thread) {return GeetestLib.success_res;} else {return GeetestLib.fail_res;}}/** * 输入的两位的随机数字,解码出偏移量 *  * @param randStr * @return */public int decodeRandBase(String challenge) {String base = challenge.substring(32, 34);ArrayList<Integer> tempArray = new ArrayList<Integer>();for (int i = 0; i < base.length(); i++) {char tempChar = base.charAt(i);Integer tempAscii = (int) (tempChar);Integer result = (tempAscii > 57) ? (tempAscii - 87): (tempAscii - 48);tempArray.add(result);}int decodeRes = tempArray.get(0) * 36 + tempArray.get(1);return decodeRes;}/** * 解码随机参数 *  * @param encodeStr * @param challenge * @return */public int decodeResponse(String challenge, String string) {if (string.length() > 100) {return 0;}int[] shuzi = new int[] { 1, 2, 5, 10, 50 };String chongfu = "";HashMap<String, Integer> key = new HashMap<String, Integer>();int count = 0;for (int i = 0; i < challenge.length(); i++) {String item = challenge.charAt(i) + "";if (chongfu.contains(item) == true) {continue;} else {int value = shuzi[count % 5];chongfu += item;count++;key.put(item, value);}}int res = 0;for (int j = 0; j < string.length(); j++) {res += key.get(string.charAt(j) + "");}res = res - decodeRandBase(challenge);return res;}/** * 增强版的验证信息,提供了更多的验证返回结果信息,以让客户服务器端有不同的数据处理。 *  * @param challenge * @param validate * @param seccode * @return */public String enhencedValidateRequest(HttpServletRequest request) {if (!resquestIsLegal(request)) {return GeetestLib.fail_res;}String challenge = request.getParameter(this.fn_geetest_challenge);String validate = request.getParameter(this.fn_geetest_validate);String seccode = request.getParameter(this.fn_geetest_seccode);// String gtuser = "";// Cookie[] cookies = request.getCookies();//// if (cookies != null) {// for (int i = 0; i < cookies.length; i++) {// Cookie cookie = cookies[i];// if ("GeeTestUser".equals(cookie.getName())) {// gtuser = cookie.getValue();// gtlog(String.format("GeeTestUser:%s", gtuser));// }// }// }String host = baseUrl;String path = "/validate.php";int port = 80;// String query = "seccode=" + seccode + "&sdk=" + this.sdkLang + "_"// + this.verName;String query = String.format("seccode=%s&sdk=%s", seccode,(this.sdkLang + "_" + this.verName));String response = "";gtlog(query);try {if (validate.length() <= 0) {return GeetestLib.fail_res;}if (!checkResultByPrivate(challenge, validate)) {return GeetestLib.fail_res;}response = postValidate(host, path, query, port);gtlog("response: " + response);} catch (Exception e) {e.printStackTrace();}gtlog("md5: " + md5Encode(seccode));if (response.equals(md5Encode(seccode))) {return GeetestLib.success_res;} else {return response;}}/** * the old api use before version code 8(not include) *  * @param challenge * @param validate * @param seccode * @return * @time 2014122_171529 by zheng */private boolean validate(String challenge, String validate, String seccode) {String host = baseUrl;String path = "/validate.php";int port = 80;if (validate.length() > 0 && checkResultByPrivate(challenge, validate)) {String query = "seccode=" + seccode;String response = "";try {response = postValidate(host, path, query, port);gtlog(response);} catch (Exception e) {e.printStackTrace();}gtlog("md5: " + md5Encode(seccode));if (response.equals(md5Encode(seccode))) {return true;}}return false;}/** * Print out log message Use to Debug *  * @time 2014122_151829 by zheng *  * @param message */public void gtlog(String message) {if (debugCode) {System.out.println("gtlog: " + message);}}protected boolean checkResultByPrivate(String challenge, String validate) {String encodeStr = md5Encode(privateKey + "geetest" + challenge);return validate.equals(encodeStr);}/** * fuck,貌似不是Post方式,后面重构时修改名字 *  * @param host * @param path * @param data * @param port * @return * @throws Exception */protected String postValidate(String host, String path, String data,int port) throws Exception {String response = "error";// data=fixEncoding(data);InetAddress addr = InetAddress.getByName(host);Socket socket = new Socket(addr, port);BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));wr.write("POST " + path + " HTTP/1.0\r\n");wr.write("Host: " + host + "\r\n");wr.write("Content-Type: application/x-www-form-urlencoded\r\n");wr.write("Content-Length: " + data.length() + "\r\n");wr.write("\r\n"); // 以空行作为分割// 发送数据wr.write(data);wr.flush();// 读取返回信息BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));String line;while ((line = rd.readLine()) != null) {System.out.println(line);response = line;}wr.close();rd.close();socket.close();return response;}// /**// * 转为UTF8编码// *// * @time 2014年7月10日 下午3:29:45// * @param str// * @return// * @throws UnsupportedEncodingException// */// private String fixEncoding(String str) throws// UnsupportedEncodingException {// String tempStr = new String(str.getBytes("UTF-8"));// return URLEncoder.encode(tempStr, "UTF-8");// }/** * md5 加密 *  * @time 2014年7月10日 下午3:30:01 * @param plainText * @return */public String md5Encode(String plainText) {String re_md5 = new String();try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(plainText.getBytes());byte b[] = md.digest();int i;StringBuffer buf = new StringBuffer("");for (int offset = 0; offset < b.length; offset++) {i = b[offset];if (i < 0)i += 256;if (i < 16)buf.append("0");buf.append(Integer.toHexString(i));}re_md5 = buf.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return re_md5;}}
package com.geetest.sdk.java.web.demo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.geetest.sdk.java.GeetestLib;public class VerifyLoginServlet extends HttpServlet {/** *  */private static final long serialVersionUID = 244554953219893949L;protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// get session to share the objectGeetestLib geetest = GeetestLib.getGtSession(request);int gt_server_status_code = GeetestLib.getGtServerStatusSession(request);String gtResult = "fail";if (gt_server_status_code == 1) {gtResult = geetest.enhencedValidateRequest(request);System.out.println(gtResult);} else {// TODO use you own system when geetest-server is down:failbackSystem.out.println("failback:use your own server captcha validate");gtResult = "fail";gtResult=geetest.failbackValidateRequest(request);}if (gtResult.equals(GeetestLib.success_res)) {// TODO handle the Success resultPrintWriter out = response.getWriter();out.println(GeetestLib.success_res + ":" + geetest.getVersionInfo());} else if (gtResult.equals(GeetestLib.forbidden_res)) {// TODO handle the Forbidden resultPrintWriter out = response.getWriter();out.println(GeetestLib.forbidden_res + ":"+ geetest.getVersionInfo());} else {// TODO handle the Fail resultPrintWriter out = response.getWriter();out.println(GeetestLib.fail_res + ":" + geetest.getVersionInfo());}}}
package com.geetest.sdk.java.web.demo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.geetest.sdk.java.GeetestLib;public class VerifyLoginServlet extends HttpServlet {/** *  */private static final long serialVersionUID = 244554953219893949L;protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// get session to share the objectGeetestLib geetest = GeetestLib.getGtSession(request);int gt_server_status_code = GeetestLib.getGtServerStatusSession(request);String gtResult = "fail";if (gt_server_status_code == 1) {gtResult = geetest.enhencedValidateRequest(request);System.out.println(gtResult);} else {// TODO use you own system when geetest-server is down:failbackSystem.out.println("failback:use your own server captcha validate");gtResult = "fail";gtResult=geetest.failbackValidateRequest(request);}if (gtResult.equals(GeetestLib.success_res)) {// TODO handle the Success resultPrintWriter out = response.getWriter();out.println(GeetestLib.success_res + ":" + geetest.getVersionInfo());} else if (gtResult.equals(GeetestLib.forbidden_res)) {// TODO handle the Forbidden resultPrintWriter out = response.getWriter();out.println(GeetestLib.forbidden_res + ":"+ geetest.getVersionInfo());} else {// TODO handle the Fail resultPrintWriter out = response.getWriter();out.println(GeetestLib.fail_res + ":" + geetest.getVersionInfo());}}}
改造的spring版本
@RequestMapping(value = "/loginVerifyPrepare", method = RequestMethod.GET)@ResponseBodypublic Object loginVerifyPrepare(HttpServletRequest request) {GeetestLib gtSdk = new GeetestLib();gtSdk.setCaptchaId(GeetestConfig.getCaptcha_id());gtSdk.setPrivateKey(GeetestConfig.getPrivate_key());gtSdk.setGtSession(request);String resStr = "{}";if (gtSdk.preProcess() == 1) {// gt server is in useresStr = gtSdk.getSuccessPreProcessRes();gtSdk.setGtServerStatusSession(request, 1);} else {// gt server is downresStr = gtSdk.getFailPreProcessRes();gtSdk.setGtServerStatusSession(request, 0);}return resStr;}@RequestMapping(value = "/loginVerifyResult", method = RequestMethod.POST)@ResponseBodypublic Object loginVerifyResult(HttpServletRequest request) {// get session to share the objectGeetestLib geetest = GeetestLib.getGtSession(request);int gt_server_status_code = GeetestLib.getGtServerStatusSession(request);String gtResult = "fail";if (gt_server_status_code == 1) {gtResult = geetest.enhencedValidateRequest(request);logger.info("gtResult : " + gtResult);} else {// use you own system when geetest-server is down:failbacklogger.info("failback:use your own server captcha validate");gtResult = "fail";}if (gtResult.equals(GeetestLib.success_res)) {// handle the Success resultreturn GeetestLib.success_res + ":" + geetest.getVersionInfo();} else if (gtResult.equals(GeetestLib.forbidden_res)) {// handle the Forbidden resultreturn GeetestLib.forbidden_res + ":" + geetest.getVersionInfo();} else {// handle the Fail resultreturn GeetestLib.fail_res + ":" + geetest.getVersionInfo();}}
html页面
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>#include("/resources/common/header.html")<link href="/resources/css/login.css" rel="stylesheet" /><script src="/resources/plugin/jquery/js/jquery.cookie.js"></script><script src="/resources/plugin/notify/js/notify.min.js"></script><script src="http://api.geetest.com/get.php"></script></head><body><form id="loginForm" method="post"><div class="login-container animated fadeInDown"><div class="loginbox bg-white"><div class="loginbox-title"><strong>旭航商店运营平台</strong></div><div class="loginbox-textbox"><i class="loginbox-icon glyphicon glyphicon-user"></i><input type="text" id="loginname" name="loginname" class="form-control" placeholder="用户名" /></div><div class="loginbox-textbox"><i class="loginbox-icon glyphicon glyphicon-lock"></i><input type="password" id="password" name="password" class="form-control" placeholder="密码" /></div><div id="div_verify" class="verifybox"></div><div id="div_captcha" class="loginbox-textbox"><input type="text" id="captchaInput" class="form-control captchaInput" maxlength="4" /><img id="captchaImage" class="captchaImage" title="等待刷新" /></div><div class="loginbox-textbox"><div class="checkbox"><label><input id="checkRemember" type="checkbox">记住密码</label><label class="label-right"><input id="checkPasswordVisible" type="checkbox">显示密码</label></div></div><div class="loginbox-submit"><input type="button" id="btnLogin" class="btn btn-primary btn-block" disabled="disabled" value="登     录"></div></div></div><p class="copyright">Copyright©2014 Hangzhou XuHang Technology Co. Ltd. All Rights Reserved.</p></form></body><script type="text/javascript">var verifyResult = false;var verifyDefault = true;var tagKeepRemember = "keepRemember";var tagLoginname = "loginname";var tagPassword = "password";var loginnameCookie, passwordCookie;var notifyOptions4Show = {autoHide : false,position : "right",className : "error",style : "bootstrap",showDuration : 400,hideDuration : 200,};var notifyOptions4Hide = {autoHide : true,showDuration : 0,hideDuration : 0,};$(function() {// 重新跳转登录// if (top.window.location.href != window.location.href) {if (window != top) {top.window.location.href = window.location.href;}var inited = initCookie();var loginnameRedirect = "${loginnameRedirect}";if (loginnameRedirect) {$("#loginname").val(loginnameRedirect);$("#password").val("");$("#password").focus();} else {if (!inited) {if ($("#loginname").val()) {$("#password").focus();} else {$("#loginname").focus();}}}// get geetest server status, use the failback solution$.ajax({url : "${urlLoginVerifyPrepare}",type : "get",dataType : 'JSON',success : function(result) {$("#btnLogin").removeAttr("disabled");if (result.success) {verifyDefault = true;$("#div_captcha").hide();//1. use geetest capthcawindow.gt_captcha_obj = new window.Geetest({gt : result.gt,challenge : result.challenge,product : 'float'});gt_captcha_obj.appendTo("#div_verify");//Ajax request demo,if you use submit form ,then ignore it gt_captcha_obj.onSuccess(function() {geetest_ajax_results()});gt_captcha_obj.onStatusChange(function() {$("#div_verify").notify("", notifyOptions4Hide);});} else {//failback :use your own captcha template//Geetest Server is down,Please use your own captcha systemin your web page//or use the simple geetest failback solutionverifyDefault = false;refreshCaptchaImage();}}});$("#checkPasswordVisible").change(function() {if (this.checked) {$("#password").attr("type", "text");} else {$("#password").attr("type", "password");}});$("#loginname").off("input propertychange").on("input propertychange", function() {$("#password").val("");checkItemTrigger("checkRemember", false);if ($(this).val() == loginnameCookie && passwordCookie) {$("#password").val(passwordCookie);checkItemTrigger("checkRemember", true);}});});/*$(document).keyup(function(e) {var event = e || window.event;var code = event.keyCode || event.which || event.charCode;if (code == 13) {login();}});*/$(document).keydown(function(e) {var event = e || window.event;var code = event.keyCode || event.which || event.charCode;if (code == 13) {login();} else {if ($("#loginname").is(":focus")) {$("#loginname").notify("", notifyOptions4Hide);} else if ($("#password").is(":focus")) {$("#password").notify("", notifyOptions4Hide);} else {if (!verifyDefault) {if ($("#captchaInput").is(":focus")) {$("#captchaImage").notify("", notifyOptions4Hide);}}}}});$("#btnLogin").click(function() {login();});$("#captchaImage").click(function() {refreshCaptchaImage();});function initCookie() {var keepRemember = $.cookie(tagKeepRemember);var loginname = loginnameCookie = $.cookie(tagLoginname);var password = passwordCookie = $.cookie(tagPassword);if (keepRemember == "true") {// $("#checkRemember").attr("checked", true);checkItemTrigger("checkRemember", true);if (loginname) {$("#loginname").val(loginname);$("#password").val(password);return true;}} else {// $("#checkRemember").attr("checked", false);checkItemTrigger("checkRemember", false);if (loginname) {$("#loginname").val(loginname);}}return false;}function geetest_ajax_results() {$.ajax({url : "${urlLoginVerifyResult}",type : "post",data : gt_captcha_obj.getValidate(),success : function(sdk_result) {if (sdk_result.indexOf("success") >= 0) {verifyResult = true;}}});}function login() {var loginname = $("#loginname").val();if (!loginname) {$("#loginname").notify("要填写用户名哟", notifyOptions4Show);$("#loginname").focus();return;}var password = $("#password").val();if (!password) {$("#password").notify("密码未填写", notifyOptions4Show);$("#password").focus();return;}if (verifyDefault) {if (!verifyResult) {gt_captcha_obj.refresh();$("#div_verify").notify("请先通过验证", notifyOptions4Show);return;}} else {var captchaVal = $("#captchaInput").val();if (!captchaVal) {$("#captchaImage").notify("请输入验证码", notifyOptions4Show);$("#captchaInput").focus();return;}}var dataSend = {};dataSend["loginname"] = loginname;dataSend["password"] = password;dataSend["verifyDefault"] = verifyDefault;dataSend["captchaVal"] = captchaVal;$.ajax({type : "POST",url : "${urlLoginCheck}",// data : $("#loginForm").serialize(), // 序列化data : dataSend, // { 'loginname':'wander', 'password':'123456' },success : function(data) {var result = data.result;var msg = data.msg;var idSel = "#" + data.obj;if (result) {var keepRemember = $("#checkRemember").is(":checked");if (keepRemember) {$.cookie(tagKeepRemember, "true");$.cookie(tagLoginname, loginname);$.cookie(tagPassword, password);} else {$.cookie(tagKeepRemember, "false");$.cookie(tagLoginname, loginname);}checkLoginConflict(loginname);} else {$(idSel).notify(msg, notifyOptions4Show);if (data.obj == "captchaImage") {$("#captchaInput").focus();} else {$(idSel).focus();}}}});}function refreshCaptchaImage() {$("#captchaImage").hide().attr("src", "${urlLoginCaptchaImage}").fadeIn();}function checkLoginConflict(loginname) {var websocket = initWebsocket(// "ws://${macroGet('m_serverName')}:${macroGet('m_onlineUserManagerServerPort')}/","${urlWebsocket}",function() {websocket.send(ONLINE_USER_MANAGER_PRE_ONLINE + ONLINE_USER_MANAGER_SEPARATOR + loginname);}, function(type, msg) {if (type == ONLINE_USER_MANAGER_TYPE_ONLINE) {$("#btnLogin").attr("disabled", true);window.location.href = "${urlMain}";} else {$("#loginname").notify("此账号已经登录,您暂时无法登录", notifyOptions4Show);$("#loginname").focus();}}, null, null);}</script></html>
0 0