package com.example.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HttpsURLConnection;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
/**
* @author seif
* @Description: TODO
* @date 2018/10/710:24
* @Modified by:
*/
public class GoogleCaptchaUtil {
private static Logger logger = LoggerFactory.getLogger(GoogleCaptchaUtil.class);
private static final String PRIVATE_KEY = "";
private static final String VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify";
public static boolean checkAnswer(String gRecaptchaResponse) {
try {
URL verifyUrl = new URL(VERIFY_URL);
// Open a Connection to URL above.
HttpsURLConnection conn = (HttpsURLConnection) verifyUrl.openConnection();
// Add the Header informations to the Request to prepare send to the server.
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Data will be sent to the server.
String postParams = "secret=" + PRIVATE_KEY //
+ "&response=" + gRecaptchaResponse;
// Send Request
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
// Get the output stream of Connection.
// Write data in this stream, which means to send data to Server.
OutputStream outStream = conn.getOutputStream();
outStream.write(postParams.getBytes());
outStream.flush();
outStream.close();
InputStream in = conn.getInputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
while (true) {
int rc = in.read(buf);
if (rc <= 0)
break;
else
bout.write(buf, 0, rc);
}
in.close();
// Response code return from Server.
// int responseCode = conn.getResponseCode();
// System.out.println("responseCode=" + responseCode);
// String message = conn.getResponseMessage();
JSONObject jsonObject = JSON.parseObject(bout.toString());
boolean success = jsonObject.getBoolean("success");
return success;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
isValid = GoogleCaptchaUtil.checkAnswer(uresponse);
网易
package com.example.demo;
/**
* 密钥对
* Created by captcha_dev on 16-11-10.
*/
public class NESecretPair {
public final String secretId;
public final String secretKey;
/**
* 构造函数
*/
public NESecretPair() {
this.secretId = "";
this.secretKey = "";
}
}
private final NECaptchaVerifier verifier = new NECaptchaVerifier(new NESecretPair());
String user = "{" + account + ":" + 123 + "}";
isValid = verifier.verify(validate, user);