설치&설정 관련

특정 도메인의 API를 사용시 발생하는 ssl certificate_verify_failed 오류

lahuman 2020. 6. 29. 22:40
728x90

특정 도메인의 API를 사용시 발생하는 ssl certificate_verify_failed 오류

https 통신시 발생을 합니다.

도메인이나 다른 문제가 없어 보이는테 이상하게 httpclient, axios로 통신을 시도하면 발생하는 문제입니다.

언어별로 해별방법이 비슷합니다.

python에서 해결 방법

import urllib.request
import ssl

def callApi(requst):
    context = ssl._create_unverified_context() 
    r = urllib.request.urlopen('https://lahuman.github.com', context = context)    

java에서 해결 방법

httpclient 4.4 이상을 사용하면 다음과 같이 쉽게 해결이 가능합니다.

void contextLoads() throws IOException {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
        HttpPost httpPost = new HttpPost("https://lahuman.github.com");
    httpPost.addHeader("API-KEY","adsfasdfasdfasdfasdfasdf" );
        httpPost.addHeader("Content-Type","application/json" );
        httpPost.setEntity(new ByteArrayEntity(String.format("{\"ABC\":123}").getBytes()));

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            String text = null;
            try (Scanner scanner = new Scanner(entity.getContent(), StandardCharsets.UTF_8.name())) {
                text = scanner.useDelimiter("\\A").next();
            }
            System.out.println(text);
            EntityUtils.consume(entity);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

nodejs에서 해결 방법

const https = require('https');
const axios = require('axios');

const instance = axios.create({
  baseURL: 'https://lahuman.github.com',
  timeout: 120000,
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  })
});

const response = await instance.post('/api', { abc:123 });

결론

결국 https의 Verifier를 사용하지 않는 옵션을 추가 한다.

728x90