Google Recaptcha V2 人机验证 在jsp,servlet基础java web上的使用

Google Recaptcha V2 人机验证 在jsp,servlet基础java web上的使用

----First Step 注册site-key—

进入google recaptcha(需要翻墙)

https://www.google.com/recaptcha/intro/v3.html#
在这里插入图片描述在这里插入图片描述我这里已经登录了就直接进去申请一个新的验证码就可以了。没有注册的同学自行百度在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

–引入Js页面,直接放到jsp中,根据你的jsp进行相应修改–

<html>
  <head>
    <title>reCAPTCHA demo: Simple page</title>
     <script src="https://www.recaptcha.net(默认为www.google.com,这里将它替换为www.re...net,绕过谷歌可以在国内直接使用)/recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="your_site_key"(刚刚提到的引入码放在这)></div>
      <br/>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

在这里插入图片描述
现在可以在jsp页面中看到你引入的人机验证码了,如果打开jsp文件没看到验证码,说明引入错误下一步进行后台处理。

—到servlet 进行后台处理----

1.recaptcha的大致原理

在controller端(servlet)获取到‘’g-recaptcha‘’返回的值(request.getparameter(“g-recaptcha-response”)注意这里要加一个response具体作用不是很清楚,不加获取不到),然后返回给google,在google服务器上进行验证,google后续会反馈一个json文件回来,对json文件字段进行解析就好了,成功的话json里的success字段值为ture反之false从而判断验证是否成功,这里需要用到javax.json包对json数据进行处理,笔者从外网获取到一个工具类大家可以直接用。

package com.dly.utils;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.net.ssl.HttpsURLConnection;

public class VerifyRecaptcha {
    public static final String url = "https://recaptcha.net/recaptcha/api/siteverify";
    public static final String secret = "6LcA9pIUAAAAAGMYpAzNWRD6oSaQApHtMJ_B4AgN"(刚刚网站上的核对密码,自己改一下);
    private final static String USER_AGENT = "Mozilla/5.0";

    public static boolean verify(String gRecaptchaResponse) throws IOException {
        if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
            return false;
        }

        try{
            URL obj = new URL(url);
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

            // add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

            String postParams = "secret=" + secret + "&response="
                    + gRecaptchaResponse;

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(postParams);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Post parameters : " + postParams);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());

            //parse JSON response and return 'success' value
            JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
            JsonObject jsonObject = jsonReader.readObject();
            jsonReader.close();

            return jsonObject.getBoolean("success");
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

进行简单的逻辑判断就好了。
教程到此结束
这里需要的jar包链接放下面了,积分不够了赚点积分望理解,当然你也可以直接外网下资源很多。
https://download.csdn.net/download/qq_37585801/10972905

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值