有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

elasticsearch我无法使用java以302响应登录elastic search,但它与postman一起工作

事实上,我想在没有kibana的情况下直接访问ElasticSearch。我使用Java来设计这个过程。当我使用postman测试登录步骤时,在标题中使用token和kbn-xsrf(图中显示1:https://i.stack.imgur.com/ti9Nf.pngenter image description here ,我可以从邮递员那里得到结果。 然而,当我使用Java设计这个过程时,302失败了。我尝试了RequestEntity方法和WebClient方法,两种方法都失败了(图片显示为here)。任何人都可以帮助解决这个问题,非常感谢

package com.sap.ngom.collection.client;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;

import org.cloudfoundry.uaa.tokens.AbstractToken;
import org.cloudfoundry.uaa.tokens.GetTokenByPasswordResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestOperations;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


@Service
public class ElasticSearchClient {

    private static final String CF_TOKEN_URL = "https://uaa.cf.us10.hana.ondemand.com";
    private static final String CF_LOGS_URL = "https://logs.cf.us10.hana.ondemand.com";
    private static ObjectMapper mapper = new ObjectMapper();

    String username = System.getenv("USER");
    String password = System.getenv("PWD");
    String token = "";

    @Autowired
    private RestOperations restOperations;

    public JsonNode getLogsByElasticSearch() throws JsonParseException, JsonMappingException, IOException{

        String query = mapper.readValue(new File(getFileURI("/query.json")), JsonNode.class).toString();
        String index = mapper.readValue(new File(getFileURI("/index.json")), JsonNode.class).toString();        
        String queryBody = index + "\n" + query + "\n" + index + "\n" + query;      

        ResponseEntity responseEntity = retrieveLog(queryBody);

        if (responseEntity.getStatusCode() == HttpStatus.FOUND){

            dealWithRedirect(responseEntity);
        }

        return null;
    }

    private void dealWithRedirect(ResponseEntity responseEntity){
        HttpHeaders responseHeaders = responseEntity.getHeaders();
        String loction = responseHeaders.get("Location").toString();
        String loginUrl = loction.substring(1, loction.length()-1);

        String setCookie = responseHeaders.get("Set-Cookie").toString();
        setCookie = setCookie.substring(1, loction.length()-1);

        HttpHeaders loginHeaders = new HttpHeaders();
        loginHeaders.set("Cookie", setCookie);
        loginHeaders.set(HttpHeaders.AUTHORIZATION, "Basic Y2Y6");
        loginHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 

        String body = "grant_type=password&username=jane.wang03@sap.com&password="+ System.getenv("PWD") + "&response_type=user_token";

        RequestEntity<String> loginRequestEntity = new RequestEntity<>(body, loginHeaders, HttpMethod.POST, URI.create(loginUrl));
        ResponseEntity loginResponse = restOperations.exchange(loginRequestEntity, Object.class);

        String temp = "";
    }

    private void passAuthentication(String state){
        String url = CF_TOKEN_URL + "/oauth/authorize?grant_type=authorization_code&client_id=sleeve-app-logs&response_type=code&state=" + state;

        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.AUTHORIZATION, "bearer " + token);

        RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, URI.create(url));

        HttpStatus responseEntity = restOperations.exchange(requestEntity, Object.class).getStatusCode();        
    }


    private ResponseEntity retrieveLog(String queryBody){

        if (token == ""){
            token = getToken();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.AUTHORIZATION, "bearer " + token);
        headers.set("kbn-xsrf", "reporting");  

        String url = CF_LOGS_URL + "/elasticsearch/_msearch";
        RequestEntity<String> requestEntity = new RequestEntity<>(queryBody, headers, HttpMethod.POST, URI.create(url));
        ResponseEntity responseEntity = restOperations.exchange(requestEntity, Object.class);

        return responseEntity;
    }


    private String getToken() {

        String body = "grant_type=password";
        try {
            body += "&username=" + URLEncoder.encode(username, "UTF-8");
            body += "&password=" + URLEncoder.encode(password, "UTF-8");
            body += "&response_type=user_token";
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }

        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.AUTHORIZATION, "Basic Y2Y6");
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);       

        RequestEntity<String> requestEntity = new RequestEntity<>(body, headers, HttpMethod.POST, URI.create(CF_TOKEN_URL + "/oauth/token"));
        ResponseEntity<GetTokenByPasswordResponse> responseEntity = restOperations.exchange(requestEntity, GetTokenByPasswordResponse.class);
        AbstractToken token = responseEntity.getBody();              

        return token.getAccessToken();
    }


    private URI getFileURI(String path) {        
        URL ruleURL = ElasticSearchClient.class.getResource(path);
        URI uri = null;

        try {
            uri = ruleURL.toURI();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return uri;
    }
}

共 (0) 个答案