【发布时间】: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。