【问题标题】:Forbidden when RestTemplate call an external API (Cloudflare server)RestTemplate 调用外部 API(Cloudflare 服务器)时被禁止
【发布时间】:2019-07-31 23:09:37
【问题描述】:

我在我的 REST 服务中使用一个 REST API。当我从 Chrome 或 Postman 调用 API 时一切正常,但从应用程序调用时返回 Forbbiden 响应。

PS:我正在使用 Java Spring Boot 项目。

测试方法:

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));
}

生产:

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

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

【问题讨论】:

  • 你也用过 Spring Security 吗?

标签: java rest spring-boot integration-testing


【解决方案1】:

当我在标头中指定一些“用户代理”时,一切正常。

这似乎是对 CloudFlare 的限制: https://support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do-

我的应用程序的用户代理是我的 Java 版本 (Java/1.8.0_151)。如果您尝试使用此用户代理,您将收到来自 CloudFlare 的限制访问消息。

卷曲:

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

响应:拒绝访问 | swapi.co 使用 Cloudflare 限制访问

这段代码解决了问题:

 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();

另一种解决方案:

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

   @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();
    }

【讨论】:

    【解决方案2】:

    您的代码实际上对我有用:

    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));
    }
    

    输出是:

    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/"}]}
    

    也许您在一分钟或一小时内向服务发送了太多请求,并且它们已经开始阻止您的 IP / 用户代理或类似情况。

    【讨论】:

    • 与您使用的代码相同: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 - 响应 403 禁止
    • 我将使用您的代码编辑我的问题,以使其更简单。 Tks
    • @VictorSoares 您是否尝试过联系“swapi.co”服务背后的支持(或开发人员)?也许他们有某种 DDOS 保护系统,你会受到影响。另外,您是否使用任何代理来访问“swapi.co”服务?
    • 啊!我看到你自己对你的问题的回答:正如我所怀疑的,你被 DDOS 保护系统阻止了,该系统具有简单的规则来过滤某些用户代理发送的请求。
    • 完全是弗拉基米尔
    猜你喜欢
    • 2021-09-09
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多