【问题标题】:403 Forbibben in API Rest CallAPI Rest 调用中的 403 禁止
【发布时间】:2021-11-12 18:37:17
【问题描述】:

我正在用 java 编写一个小程序,它对端点进行休息调用以验证参考 ID。我用两种不同的方式编写了这个程序,当我从我的电脑上运行它时,每一种都可以工作,但是当我在测试Linux CentOs Server 上部署 jar 文件时,我从终点得到一个403 forbidden error,所以我想也许是 ip被阻止了,然后我用 curl 尝试了同样的事情,它工作得很好。

可能是什么问题?会不会是ssl证书版本错误?

方式一:

public String sendGet(String id) {
    restTemplate = new RestTemplate();
    log.info("Authorization :::: {}", "Bearer " + this.getApiAuth());

    HttpHeaders headers = new HttpHeaders();

    headers.set("Authorization", "Bearer " + this.getApiAuth());

    List<MediaType> acceptableMediaTypes = new ArrayList<>();

    acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
    headers.setAccept(acceptableMediaTypes);
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<String> httpRequestEntity = new HttpEntity<>(headers);
    ResponseEntity<String> exchange = getRestTemplate().exchange(this.getUrl() + id, HttpMethod.GET, httpRequestEntity, String.class);

    log.info("Status Code :::: {}", exchange.getStatusCode());
    log.info("Status Code :::: {}", exchange.getBody());
    return exchange.getBody(); 
}

方式 2:

 public String sendGet(String message) throws Exception {
    infoLogger.info("GOING HERE " + url + message);

    java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
    URL obj = new URL(url + message);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    con.setRequestMethod("GET");

    infoLogger.info("Setting Authorization: Bearer " + this.getApiAuth());
    con.setRequestProperty("Authorization", "Bearer " +this.getApiAuth());
    con.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON_VALUE);
    con.setRequestProperty("accept", MediaType.APPLICATION_JSON_VALUE);

    int responseCode = con.getResponseCode();
    infoLogger.info("\nSending 'GET' request to URL : " + url);
    infoLogger.info("Response Code : " + responseCode);

    BufferedReader in = null;
    String inputLine;
    StringBuilder response = new StringBuilder();

    if ((200 <= con.getResponseCode()) && (con.getResponseCode() <= 299)) {
        if (con.getInputStream() != null) {
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        }
    } else if (con.getErrorStream() != null) {
        in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
    } else {
        infoLogger.info("GOT NOTHING ");
    }

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

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

}

所以我按照要求做了一个.getResponseMessage,我得到了以下错误:

访问被拒绝 | api.paystack.co 使用 Cloudflare 限制 accessbody{margin:0;padding:0} 请启用 cookie。

错误 1010 射线 ID:49f12b9b9bb6c5fa • 2019-01-26 07:12:17 UTC

访问被拒绝 发生了什么?

该网站的所有者 (api.paystack.co) 已根据您的浏览器签名 (49f12b9b9bb6c5fa-ua21) 禁止您访问。

Cloudflare Ray ID:49f12b9b9bb6c5fa •您的 IP:104.248.9.123 • Cloudflare 的性能和安全性

window._cf_translation = {}; `

【问题讨论】:

  • 如果 curl 有效,您是否尝试过 --verbose 或 --trace 来查看有关 https 握手、版本、证书等的详细信息?
  • @JensDibbern SSL 连接使用 TLSv1.3 / TLS_AES_256_GCM_SHA384
  • 你正在使用 java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");如果服务器想要强制执行 TLSv1.3,这可能是您的问题。
  • @JensDibbern 如果我删除那部分代码。我得到一个 ssl 异常
  • System.setProperty("https.protocols", "TLSv1.3"); 怎么样?您需要 Java 11 才能使用 TLSv1.3。您可以尝试使用 --tlsv1.2 选项 curl 来验证服务器是否确实需要 v1.3。

标签: java rest


【解决方案1】:

您尝试访问的服务的供应商正在使用 CloudFlare。他们正在使用某种浏览器完整性检查(请参阅https://support.cloudflare.com/hc/en-us/articles/200171806-Error-1010-The-owner-of-this-website-has-banned-your-access-based-on-your-browser-s-signature)。

您可以在您的 java 代码中更改签名:

con.setRequestProperty("User-Agent", "My own REST client");

您的 PC 上使用的 JDK 似乎与 CentOS 服务器上的不同,因此 Java HttpsURLConnection 的签名不同。它们当然与 curl 不同。

【讨论】:

  • 那么你在哪里设置“我自己的 REST 客户端”我可以生成一个唯一标识号并将其设置在那里?
  • 你可以像上面那样试试。如果它仍然不起作用,只需添加额外的唯一编号。
  • 我使用的服务公司都不知道。
【解决方案2】:

对于方式 1,这将起作用:

headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2019-02-21
    • 2018-03-11
    相关资源
    最近更新 更多