首页 > 解决方案 > 从服务器运行应用程序时,ElasticSearch 在 PUT 上抛出 403,但在本地计算机上运行时有效

问题描述

我正在使用 RestTemplate 将数据发布到 ElasticSearch:

 String elasticUrl = "localhost:9200/" + route + "/_bulk";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<String> request = new HttpEntity<>(json,headers);
    LOG.info("Sendin data to elastic url: " + elasticUrl + "\n" + request.getBody());

    ResponseEntity<String> response;
    try{
         response = restTemplate.exchange(elasticUrl,HttpMethod.PUT,request,String.class);
         LOG.info("Response from elastic:\n" + response.getBody());
    }catch(Exception e ) {
        LOG.debug(e.getMessage());
    }

弹性在远程服务器上运行(我有 ssh 隧道,所以我使用的是 localhost)。这可以正常工作,但是当我在运行弹性的服务器上部署应用程序时,这会抛出

403 - 禁止

但是,使用该服务器上的 curl 效果很好。我没有找到任何理由说明 API 在本地机器上的行为与部署时的行为不同。这种行为的原因可能是什么?

如果在弹性配置中出现问题,来自服务器的 curl 也无法正常工作。

感谢帮助。

标签: javaelasticsearch

解决方案


您忘记在 URL (http://) 中指定协议。

  String elasticUrl = "http://localhost:9200/" + route + "/_bulk";

如果这没有帮助,请通过设置org.apache.http.wire来启用线路日志记录,DEBUG并查看正在发送到服务器的标头以及确切的响应是什么。


推荐阅读