프록시 구성

Google Cloud 클라이언트 라이브러리는 서비스와의 기본 통신에서 HTTPS 및 gRPC를 사용합니다. 두 프로토콜 모두 https.proxyHost 및 선택적으로 https.proxyPort 속성을 사용하여 프록시를 구성할 수 있습니다.

HTTP로 프록시 구성

HTTP 클라이언트의 경우 http.proxyHost 및 관련 시스템 속성을 사용하여 기본 프록시를 구성할 수 있습니다. 이는 Java 네트워킹 및 프록시에 설명되어 있습니다.

커스텀 프록시 (예: 인증된 프록시)의 경우 GoogleCredentials에 커스텀 HttpTransportFactory를 제공합니다.

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentials;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;


import java.io.IOException;


public class ProxyExample {
 public GoogleCredentials getCredentials() throws IOException {
   HttpTransportFactory httpTransportFactory = getHttpTransportFactory(
       "some-host", 8080, "some-username", "some-password"
   );


   return GoogleCredentials.getApplicationDefault(httpTransportFactory);
 }


 public HttpTransportFactory getHttpTransportFactory(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) {
   HttpHost proxyHostDetails = new HttpHost(proxyHost, proxyPort);
   HttpRoutePlanner httpRoutePlanner = new DefaultProxyRoutePlanner(proxyHostDetails);


   CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
   credentialsProvider.setCredentials(
       new AuthScope(proxyHostDetails.getHostName(), proxyHostDetails.getPort()),
       new UsernamePasswordCredentials(proxyUsername, proxyPassword)
   );


   HttpClient httpClient = ApacheHttpTransport.newDefaultHttpClientBuilder()
       .setRoutePlanner(httpRoutePlanner)
       .setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE)
       .setDefaultCredentialsProvider(credentialsProvider)
       .build();


   final HttpTransport httpTransport =