물흐르듯코딩

Spring Cloud OpenFeign Https 에러 해결 - SSL Ignore 본문

BE

Spring Cloud OpenFeign Https 에러 해결 - SSL Ignore

AquaDev 2024. 4. 26. 12:20

 

 

 

OpenFeign 적용되어 있는 부분에서 HTTPS 통신을 시도할 때 아래와 같은 오류가 반복적으로 발생했다.

ERROR SSLHandshakeException: No subject alternative names present

 

오류의 원인을 찾기 위해 OpenFeign은 무엇인지, SSL 적용은 어떻게 해야하는지

열심히 삽질한 결과를 기록한다🔍

 

 


 

OpenFeign란?

 

Open Feign은 Netflix에 의해 처음 만들어진 Declarative(선언적인) HTTP Client 도구이다. 현재는 오픈소스로 전환되었으며 Spring Cloud Framework 프로젝트 중 하나이다.

Open Feign은 인터페이스에 어노테이션들만 붙여주면 외부 API를 호출할 수 있어 Http 요청을 간편하게 만들어 보낼 수 있다는 장점이 있다. 

@FeignClient(name = "${feign.name}", url = "${feign.url}")
public interface StoreClient {
    //..
}
@SpringBootApplication
@EnableFeignClients
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

 

 

 

Feign https 통신

@FeignClient의  url에 프로토콜을 명시해주지 않으면 기본적으로 http 요청으로 생성된다. 따라서 https로 요청을 생성 해야하는 상황이라면 SSL을 무시하도록 설정하는 코드가 필요하다.

FeignConfiguration에서 feign.Client(feignClient) 을 재정의해서 해결할 수 있다.

public class SslCertificationIgnoreConfiguration {
    private SSLSocketFactory sslContextFactory() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext ssl_ctx = SSLContext.getInstance("TLS");
        TrustManager[] certs = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }

                    public void checkClientTrusted(X509Certificate[] certs, String t) {
                    }

                    public void checkServerTrusted(X509Certificate[] certs, String t) {
                    }
                }};
        ssl_ctx.init(null, certs, new SecureRandom());
        return ssl_ctx.getSocketFactory();
    }

    @Bean
    public feign.Client client() throws KeyManagementException, NoSuchAlgorithmException {
        return new Client.Default(sslContextFactory(), (hostname, session) -> true);
    }
}

 

 

 

OpenFeign 공식문서

https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/

 


 

🏷 참고

https://bj-lee.tistory.com/11