我正在用 java 编写一个小程序,它对端点进行休息调用以验证参考 ID。我用两种不同的方式编写了这个程序,当我从我的 PC 上运行它时,每种方式都可以工作,但是当我在测试中部署 jar 文件时Linux CentOs Server,我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();
}
Run Code Online (Sandbox Code Playgroud)
方式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();
}
Run Code Online (Sandbox Code Playgroud)
所以我.getResponseMessage按照要求做了一个,我得到了以下错误:
访问被拒绝 | api.paystack.co 使用 Cloudflare 来限制 accessbody{margin:0;padding:0} 请启用 cookie。
错误 1010 Ray 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 = {}; `
小智 7
您尝试访问的服务的供应商正在使用 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");
Run Code Online (Sandbox Code Playgroud)
您在 PC 上使用的 JDK 似乎与 CentOS 服务器上使用的 JDK 不同,因此 Java HttpsURLConnection 的签名是不同的。它们当然与 curl 不同。