HyeongSeokLee / first

1 stars 1 forks source link

CAPTCHA 소스 #8

Open subutuo opened 7 years ago

subutuo commented 7 years ago

-회원가입 Form- HEAD요소 안에 FORM태그 안에 <div class="g-recaptcha" data-sitekey="6Lfk2ysUAAAAAJUBIL6D4kWNWAjOPBgoG9tZrfGS">


----VerrifyRecaptcha클래스의 스태틱메소드 verify로 인증받았는지 확인---

import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import org.json.JSONObject;

public class VerifyRecaptcha {

public static final String url = "https://www.google.com/recaptcha/api/siteverify";
public static final String secret = "6Lfk2ysUAAAAAA4rVmmzSoWQITSbUZYUZGWmkHnr";

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();

        // request header 추가
        con.setRequestMethod("POST");

        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) {
            System.out.println(inputLine);
            response.append(inputLine);
        }
        in.close();

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

        JSONObject json = new JSONObject(response.toString());
        System.out.println(json);
        System.out.println("key값 success의 값은?"+json.getBoolean("success"));

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

}

-회원가입 Pro- request.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=UTF-8");

boolean verify = VerifyRecaptcha.verify(gRecaptchaResponse); //이걸루 인증확인

/* 이부분은 서블릿에서 했을 경우인데 안해도됨 RequestDispatcher rd = getServletContext().getRequestDispatcher( "/login.html");

        PrintWriter out = response.getWriter();

if (verify) { out.println("이름이나 비밀번호 다름"); } else { out.println("CAPTCHA입력 바람"); } rd.include(request, response); */

subutuo commented 7 years ago

https://www.google.com/recaptcha/intro/index.html