简体   繁体   English

当RestTemplate调用外部API(Cloudflare服务器)时禁止

[英]Forbidden when RestTemplate call an external API (Cloudflare server)

I'm using one REST API inside my REST service. 我在REST服务中使用了一个REST API。 Everything works when I call the API from Chrome or Postman, but a Forbbiden response is returned when I call from my application. 当我从Chrome或Postman调用API时,一切正常,但是当我从应用程序中调用时,会返回Forbbiden响应。

PS: I'm using a Java Spring Boot project. PS:我正在使用Java Spring Boot项目。

Test Method: 测试方法:

public static void main(String[] args) {
    final String uri = "https://swapi.co/api/planets?search=Alderaan";
    System.out.println(new RestTemplate().getForObject(uri, String.class));
}

Produces: 产生:

20:58:01.436 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET swapi.co/api/planets?search=Alderaan 
20:58:01.461 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
20:58:02.577 [main] DEBUG org.springframework.web.client.RestTemplate - Response 403 FORBIDDEN
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden

The external API: https://swapi.co/documentation 外部API: https//swapi.co/documentation

Your code actually works for me: 您的代码实际上对我有用:

public static void main(String[] args) {
    final String uri = "https://swapi.co/api/planets?search=Alderaan";
    System.out.println(new RestTemplate().getForObject(uri, String.class));
}

The output is: 输出为:

01:20:49.564 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://swapi.co/api/planets?search=Alderaan
01:20:49.573 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
01:20:51.177 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
01:20:51.179 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json"
{"count":1,"next":null,"previous":null,"results":[{"name":"Alderaan","rotation_period":"24","orbital_period":"364","diameter":"12500","climate":"temperate","gravity":"1 standard","terrain":"grasslands, mountains","surface_water":"40","population":"2000000000","residents":["https://swapi.co/api/people/5/","https://swapi.co/api/people/68/","https://swapi.co/api/people/81/"],"films":["https://swapi.co/api/films/6/","https://swapi.co/api/films/1/"],"created":"2014-12-10T11:35:48.479000Z","edited":"2014-12-20T20:58:18.420000Z","url":"https://swapi.co/api/planets/2/"}]}

Maybe you have sent too many requests to a service within a minute or an hour and they have started to block your IP / User Agent or what so ever. 也许您在一分钟或一个小时内向服务发送了太多请求,但它们已经开始阻止您的IP /用户代理或其他阻止。

Everything works when I specify some "user-agent" in the Headers. 当我在标题中指定一些“用户代理”时,一切正常。

It seams to be a restriction on CloudFlare: https://support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do- 它似乎是对CloudFlare的限制: https : //support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do-

The user-agent of my application is my Java version (Java/1.8.0_151). 我的应用程序的用户代理是Java版本(Java / 1.8.0_151)。 If you try this user-agent you will receive a restrict access message from CloudFlare. 如果尝试使用此用户代理,您将收到来自CloudFlare的限制访问消息。

curl: 卷曲:

curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan

response: Access denied | 响应:访问被拒绝| swapi.co used Cloudflare to restrict access swapi.co使用Cloudflare限制访问

This code solve the problem: 这段代码解决了这个问题:

 HttpHeaders headers = new HttpHeaders();
 headers.add("user-agent", "Application");
 HttpEntity<String> entity = new HttpEntity<>(headers);

 String planetFound = restTemplate.exchange(findPlanetUri, HttpMethod.GET, entity, String.class).getBody();

Another solution: 另一个解决方案:

 String planetFound = restTemplate.getForObject(findPlanetUri, String.class);

and

   @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
            request.getHeaders().add("user-agent", "Application");
            return execution.execute(request, body);
        };
        return restTemplateBuilder.additionalInterceptors(interceptor).build();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM