繁体   English   中英

在 Google App Engine 上使用 HttpsURLConnection 检索证书详细信息

[英]Using HttpsURLConnection on Google App Engine to retrieve certificate details

我正在开发一个 Java 应用程序 - 在 App Engine 上运行 - 它会定期检查我们的网络应用程序使用的 SSL 证书,并在它们即将到期时向我们发出警告。

我希望使用以下代码,如果证书在不到 2 周的时间内到期,则会引发异常(我通过删除一些错误检查对其进行了一些简化)。

GregorianCalendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, 14);
Date twoWeeksInTheFuture = cal.getTime();

URL url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

// Need the following line in order to establish a connection with the server
con.getResponseCode();

Certificate[] certs = con.getServerCertificates();
Certificate certificate = certs[0];
X509Certificate x509Certificate = (X509Certificate) certificate;
x509Certificate.checkValidity(twoWeeksInTheFuture);

该代码在 App Engine 之外运行良好(例如在 Java 类的 main 方法中)。 但是,在 App Engine 中运行时会引发以下异常:

java.lang.ClassCastException: com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection cannot be cast to javax.net.ssl.HttpsURLConnection
    at com.bronzelabs.httpschecker.servlets.cron.HttpsChecker.doGet(HttpsChecker.java:101)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)

HttpsURLConnection 位于允许类白名单中,因此必须有一种方法可以在 App Engine 上使用此类(好吧,您会假设)。 看起来你不能使用 URL.openConnection()。

有谁知道建立 HTTPS 连接(在 App Engine 上使用 Java)并查看 SSL 证书详细信息的方法吗?

注意:我看过 Google 的 URLFetchService。 似乎没有任何方法可以访问证书(只有 HTTP 请求的内容)。

正如您(正确地)观察到的那样,在标准环境中无法使用内置的 URLFetchService 来做到这一点。 有关可以使用 URLFetchService 和 HTTPS 执行的操作的详细信息,请参阅此处 您可以使用第三方库来执行此操作,但请求代理也可能是一个障碍(试试sockets API吧?); 您可以根据您的用例尝试使用灵活的环境

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM