java - 从服务器运行应用程序时,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 也无法正常工作。
感谢帮助。
解决方案
您忘记在 URL (http://) 中指定协议。
String elasticUrl = "http://localhost:9200/" + route + "/_bulk";
如果这没有帮助,请通过设置org.apache.http.wire
来启用线路日志记录,DEBUG
并查看正在发送到服务器的标头以及确切的响应是什么。
推荐阅读
- c# - 在 ac# Web 应用程序中使用 HttpClient
- c++ - 从不同的文件访问一个类而不#include它?
- javascript - CORS 错误:我的 WebApp 是否需要某种服务器来订阅 Web Socket Feed?
- performance - perf 输出中的“1.234 CPUs used”是什么意思?
- react-native - React 原生后台服务崩溃应用
- javascript - 控制台中的 Jquery MIME 错误未返回足够的数据
- c# - 如何将 JSON 数组填充到 ASP:repeater
- java - 如果我们试图将它放在 hashmap 中,我们如何使用 jackson 处理 JSON 中的 JSON?
- laravel - 无法将 Xdebug 与 Laradock 一起使用
- c - 如何在C中以随机顺序打印数字1-10而不重复