I would like to implement reCAPTHCA on my service, https://mirm.jp, too, and I'm in trouble if it is accessed by post when creating a new server. It's a memorandum, so it's pretty rough.
https://www.google.com/recaptcha/admin First of all, please register to get the recaptcha key.
<head>
。。。。。。
<script src='https://www.google.com/recaptcha/api.js'></script>
。。。。。。
</head>
Please mix in the form.
<div class="g-recaptcha" data-callback="clearcall" data-sitekey="Put your site key here"></div>
<input type="submit" value="Create!">
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
public class getJsonFromWeb {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) {
try (InputStream is = new URL(url).openStream()) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
return new JSONObject(jsonText);
} catch (IOException | JSONException e1) {
return new JSONObject("{\"success\": false,\"error-codes\": \"jsondecodeerror\"}");
}
}
}
Make a query. .. ..
//recapthca
if (recaptchacode == null) {
result = create_recaptchaerror;
elog("[ReCapError1]"+ip+ua+"[srvid]"+srvid);
continue;
}
String secretKey = "----------";
JSONObject jsoned = readJsonFromUrl("https://www.google.com/recaptcha/api/siteverify?secret="+secretKey+"&response="+recaptchacode);
try {
if (!jsoned.getBoolean("success")) {
result = create_recaptchaerror;
continue;
}
}catch (Exception e){
result = create_recaptchaerror;
continue;
}
//////////////
It's a memo, but it looks like this.
https://liapoc.com/recaptcha.html
https://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java
Recommended Posts