Java 整合极验滑动验证码、Java 接入极验滑动验证码教程

极验验证码我相信大家都用过吧,目前多数 discuz 论坛都支持极验验证码。极验验证码应该是随着 12306 的找图验证码推出以后兴起的。极验验证码支持很多种编程语言,但是我们今天只使用java编程语言来为你的网站对接极验验证码。

使用极验验证码之前,我们先熟悉3个常用的接口。

是否开启验证码接口

接口名:/api3/account/captcha/open
http://localhost:8080/api3/account/captcha/open
参数非空:mobileNo;countryNo;
请求方式:get

预处理验证码接口

接口名:/api3/account/captcha/start
无参数:
请求方式:get

获取验证码接口

接口名:/api3/account/captcha/verify
参数非空:geetest_challenge,geetest_validate,geetest_seccode,
mobileNo,countryNo。
请求方式:post
{"success":1,"gt":"b46d1900d0a894591916ea94ea91bd2c","challenge":"aca5d5a08c397aba47a973a1f968e9af"}

java对接极验验证码

@RequestMapping("/StartCaptchaServlet")
protected void StartCaptchaServlet(HttpServletRequest request,
					 HttpServletResponse response) throws ServletException, IOException {
	GeetestLib gtSdk = new GeetestLib(GeetestConfig.getCaptcha_id(), GeetestConfig.getPrivate_key());
	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);
}
@RequestMapping("/VerifyLoginServlet")
protected void VerifyLoginServlet(HttpServletRequest request,
					  HttpServletResponse response) throws ServletException, IOException {
	GeetestLib gtSdk = new GeetestLib(GeetestConfig.getCaptcha_id(), GeetestConfig.getPrivate_key());
	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();
		Map<String,Object> data = new HashMap<String, Object>();
		try {
			data.put("status", "success");
			data.put("version", gtSdk.getVersionInfo());
		} catch (Exception e) {
			e.printStackTrace();
		}
		JsonSerializer json = new JsonSerializer();
		out.println(json.serialize(data.toString()));
	} else {
		// 验证失败
		Map<String,Object> data = new HashMap<String, Object>();
		try {
			data.put("status", "fail");
			data.put("version", gtSdk.getVersionInfo());
		} catch (Exception e) {
			e.printStackTrace();
		}
		PrintWriter out = response.getWriter();
		JsonSerializer json = new JsonSerializer();
		out.println(json.serialize(data.toString()));
	}
//	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());
//	}
}

更多api和教程,大家可以参考官方文档:http://docs.geetest.com/

Java 整合极验滑动验证码、Java 接入极验滑动验证码教程

: » Java 整合极验滑动验证码、Java 接入极验滑动验证码教程

原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/tech/java/251601.html

(0)
上一篇 2022年5月3日 07:01
下一篇 2022年5月3日 07:05

相关推荐

发表回复

登录后才能评论