Loading a custom key store in Google App Engine Java application

前端 未结 4 818
孤城傲影
孤城傲影 2020-12-14 17:44

I want to open a HTTPS connection in a Google App Engine app using the URLFetch service. To be able to verify the SSL certificate of the server my a

4条回答
  •  独厮守ぢ
    2020-12-14 18:22

    I was recently facing the same issue and using the HttpClient implementation packaged with appengine-api-stubs worked for me.

    Maven Dependency:

    
      com.google.appengine
      appengine-api-stubs
      1.9.18
    
    

    Code:

    // create SSL Context which trusts your self-signed certificate
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(ClassLoader.getSystemResourceAsStream("myKeystoreFile"), "password".toCharArray());
    trustManagerFactory.init(keystore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagers, null);
    
    // register your trusting SSL context
    Protocol.registerProtocol("https",
            new Protocol("https", (ProtocolSocketFactory) new SocketFactoryWrapper(sslContext.getSocketFactory()), 443));
    
    // make the https call
    HttpClient httpclient = new HttpClient();
    GetMethod httpget = new GetMethod("https://myendpoint.com");
    httpclient.executeMethod(httpget);
    System.out.println(httpget.getStatusLine());
    

    This does essentially the same thing as

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    

    But for one reason or another app engine doesn't block it.

提交回复
热议问题