Отключить самоподписанный сертификат - PullRequest
0 голосов
/ 03 января 2019

Когда я делаю POST-запрос к REST-серверу, я получаю следующее исключение: Ошибка построения пути PKIX: sun.security.provider.certpath.SunCertPathBuilderException: не удалось найти допустимый путь сертификации для запрошенного целевого исключения. Я хочу создать диспетчер доверия, который не проверяет цепочки сертификатов, и я не уверен, как этого добиться в этом контексте.

В HttpPostReq у меня есть:

public class HttpPostReq {


HttpPost createConnectivity(String restUrl, String username, String password)
{
    HttpPost post = new HttpPost(restUrl);
    String auth=new StringBuffer(username).append(":").append(password).toString();
    byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
    String authHeader = "Basic " + new String(encodedAuth);
    post.setHeader("AUTHORIZATION", authHeader);
    post.setHeader("Content-Type", "application/json");
        post.setHeader("Fintech-Platform-TenantId", "default");
              return post;
}


void executeHttpRequest(String jsonData,  HttpPost httpPost)  throws UnsupportedEncodingException, IOException
{
    HttpResponse response=null;
    String line = "";
    StringBuffer result = new StringBuffer();
    httpPost.setEntity(new StringEntity(jsonData));
    HttpClient client = HttpClientBuilder.create().build();
    response = client.execute(httpPost);
    System.out.println("Post parameters : " + jsonData );
    System.out.println("Response Code : " +response.getStatusLine().getStatusCode());
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    while ((line = reader.readLine()) != null){ result.append(line); }
    System.out.println(result.toString());
}

void executeReq(String jsonData, HttpPost httpPost)
{
    try{
        executeHttpRequest(jsonData, httpPost);
    }
    catch (UnsupportedEncodingException e){
        System.out.println("error while encoding api url : "+e);
    }
    catch (IOException e){
        System.out.println("ioException occured while sending http request : "+e);
    }
    catch(Exception e){
        System.out.println("exception occured while sending http request : "+e);
    }
    finally{
        httpPost.releaseConnection();
    }
}

}

В HttpClientBridge у меня есть:

public static void main(String[] args) throws JSONException  {

    String restUrl="https://localhost:8449/wesecbs/api/v1/loans";
    String username="myname";
    String password="mypassword";
    loan.put("interestType", "0");
    loan.put("locale", "en_GB");
    String jsonData=loann.toString();
    HttpPostReq httpPostReq=new HttpPostReq();
    HttpPost httpPost=httpPostReq.createConnectivity(restUrl , username, password);
    httpPostReq.executeReq( jsonData, httpPost);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...