写一个springboot 调用kibana api 在kibana中创建用户和角色的demo

动态 未结 0 65
小小草
小小草 LV1 2023年9月23日 23:40 发表
续上一篇回答,以下是KibanaService类的完整代码: ```java import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Service public class KibanaService { @Value("${kibana.url}") private String kibanaUrl; @Value("${kibana.username}") private String kibanaUsername; @Value("${kibana.password}") private String kibanaPassword; private final ObjectMapper objectMapper = new ObjectMapper(); public void createUser(String username, String password) throws IOException { Map requestBody = new HashMap<>(); requestBody.put("password", password); requestBody.put("full_name", username); String jsonBody = objectMapper.writeValueAsString(requestBody); HttpPost request = new HttpPost(kibanaUrl + "/api/security/v1/users"); request.setHeader("Content-Type", "application/json"); request.setHeader("kbn-xsrf", "true"); request.setHeader("Authorization", "Basic " + getAuthHeader()); StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON); request.setEntity(entity); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { String responseBody = EntityUtils.toString(responseEntity); System.out.println(responseBody); } } } public void createRole(String roleName, String[] indices) throws IOException { Map requestBody = new HashMap<>(); requestBody.put("elasticsearch", new HashMap<>()); requestBody.put("kibana", new HashMap<>()); Map kibana = (Map) requestBody.get("kibana"); kibana.put("global", new String[]{"all"}); kibana.put("space", new HashMap<>()); for (String index : indices) { Map
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复