gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:38:04 26 4
gpt4 key购买 nike

我正在开发一个 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 类的主要方法中)。但是,在 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 在 whitelist of allowed classes 中所以必须有一种方法可以在 App Engine 上使用这个类(好吧,你会假设)。看起来您不能使用 URL.openConnection()。

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

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

最佳答案

正如您(正确地)观察到的那样,在标准环境中无法使用内置的 URLFetchService 来执行此操作。有关您可以使用 URLFetchService 和 HTTPS 执行的操作的详细信息,请参阅 here .您可以使用第三方库来执行此操作,但是 request proxying也可能是一个障碍(试试 sockets API ,也许吧?);你可以尝试使用 flexible environment而不是取决于您的用例。

关于java - 在 Google App Engine 上使用 HttpsURLConnection 检索证书详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com