【问题标题】:Google reCatpcha: validate user response in the server side with JavaGoogle reCaptcha:使用 Java 在服务器端验证用户响应
【发布时间】:2015-04-01 02:21:07
【问题描述】:

我正在尝试实现如 https://developers.google.com/recaptcha/docs/verifyGoogle reCAPTCHA: how to get user response and validate in the server side 中所示的 Google reCAPTCHA,但我无法在响应中获得成功字段。

不知道问题出在请求还是解析响应,这是我的代码:

public Resolution contact(){
    String charset = java.nio.charset.StandardCharsets.UTF_8.name();
    try {
        String reCaptcha = context.getRequest().getParameter("g-recaptcha-response");
        String urlReCaptcha = String.format("https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s" 
            , URLEncoder.encode(mySecretKey,charset)
            , URLEncoder.encode(reCaptcha,charset)
            , URLEncoder.encode(context.getRequest().getHeader("X-FORWARDED-FOR")!=null?context.getRequest().getHeader("X-FORWARDED-FOR"):context.getRequest().getRemoteAddr(),charset));
        URLConnection connection;
        connection = new URL(urlReCaptcha).openConnection();
        InputStream response = connection.getInputStream();
        JSONObject json = new JSONObject(response);
        String success = json.get("success").toString();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new ForwardResolution(VIEW_SUCCESS);
}

JSONObject来自https://github.com/douglascrockford/JSON-java,json总是为空,但是在响应InputStream中找不到成功字段,所以不知道bug在哪里。

我用的是stripes框架,JSP代码是:

    <stripes:form beanclass="es.package.ContactActionBean">
        <fieldset class="form-group">
            <!-- Some fields -->
            <input type="submit" name="contact" value="SEND"  class="btn btn-success btn-sm"/>
        </fieldset>
        <div class="g-recaptcha" data-sitekey="my_public_key"></div>
    </stripes:form>

任何建议将不胜感激。

【问题讨论】:

    标签: java json recaptcha


    【解决方案1】:

    哎呀,从InputStream中取出字符串然后实例化JSONObject就够了:

            ...
            InputStream response = connection.getInputStream();
            // Take the string, Plain old JDK4 style
            StringBuilder inputStringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
            String line = bufferedReader.readLine();
            while(line != null){
                inputStringBuilder.append(line);
                inputStringBuilder.append('\n');
                line = bufferedReader.readLine();
            }
            JSONObject json = new JSONObject(inputStringBuilder.toString());
            ...
    

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2015-03-02
      • 2020-05-22
      • 1970-01-01
      • 2017-08-09
      • 2016-01-01
      相关资源
      最近更新 更多