Java中如何获取Apache环境变量?
我有一个 Apache 服务器,使用 mod_ssl 进行客户端身份验证的 SSL。 Apache 显然设置了包含用户证书等,但我无法从 System.getProperty() 获取它们。
我已在 httpd-ssl.conf 文件中设置 SSLOptions -StdEnvVars +ExportCertData (这是正确的位置吗?)但无济于事。
I have an Apache server using mod_ssl to do client-authenticated SSL. Apache apparently sets environment variables (see this mod_ssl doc) that contain the user's certificate, etc, but I can't get them from System.getProperty().
I've set SSLOptions -StdEnvVars +ExportCertData in the httpd-ssl.conf file (is that the right place?) to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一位同事发现了此网站,其中包含答案。本质上,这需要将环境变量设置为特殊标头。需要两个设置操作:一个用于初始化为空白并避免伪造,第二个用于检索并设置实际值。对您想要的每个 SSL 环境变量 执行此操作。
这使得该值可用于我们的 ColdFusion 页面。在我们的例子中,在传递到应用程序的 Java 部分之前,必须将其转换为 Java 字符串。
A coworker discovered this web site, which contains the answer. Essentially, this requires the environment variables to be set as special headers. Two set operations are needed: one to initialize to blank and avoid forgery, and a second to retrieve and set the actual value. Do this for each of the SSL environment variables that you want.
This made the value available to our ColdFusion page. In our case, this had to be converted to a Java string before being passed into the Java part of the application.
System.getProperty(...)
获取系统属性,它与环境变量不同。对于环境变量,请使用 System.getenv(...)。如 中所述该方法的 Javadoc:更新以回应下面的OP评论:我真的不知道答案,但我会列出一些你可以尝试帮助缩小问题范围的事情(其中一些你可能已经尝试过) :
ExportCertData
的文档谈到“附加 CGI/SSI 环境变量”(而不是“标准集”) “由StdEnvVars
控制)。文档没有明确说明ExportCertData
完全独立于StdEnvVars
;如果您在没有首先获得标准集的情况下无法获得这些附加变量,我不会感到震惊。因此,可能值得尝试SSLOptions +StdEnvVars +ExportCertData
。 (即使第一次尝试无法解决问题,我建议坚持使用+StdEnvVars
直到您找到一个确实有效的解决方案,并且只有 >然后切换到-StdEnvVars
,因为您可以确保这样做是可以的。)SSLOptions
被记录为可以在.htaccess
,因此您可能想尝试将其放在那里(因为这绕过了httpd-ssl.conf
与httpd.conf
的问题等等)。new java.util.ArrayList((System.getenv().keySet()).toString()
将是一个String
,其中包含您正在获取的所有环境变量的(长)列表可能值得检查它,只是为了看看键是否由于某种原因与您期望的有所不同(The.ArrayList
部分可能是不必要的— 在我的系统上,System.getenv().keySet().toString()
有效 — 但 JDK 文档似乎并不能保证后者。)System.getProperty(...)
gets a system property, which is not the same as an environment variable. For environment variables, useSystem.getenv(...)
. As explained in that method's Javadoc:Update in response to OP comment below: I really don't know the answer, but I'll list some things you can try to help narrow down the problem (some of which you may already have tried):
ExportCertData
speaks of "additional CGI/SSI environment variables" (as opposed to "the standard set" controlled byStdEnvVars
). The documentation doesn't explicitly say thatExportCertData
is completely independent ofStdEnvVars
; it wouldn't shock me if you can't get those additional variables without first getting the standard set. So, it may be worth tryingSSLOptions +StdEnvVars +ExportCertData
. (Even if that doesn't fix the problem on the first try, I'd suggest sticking with+StdEnvVars
until you have a solution that does work, and only then switching to-StdEnvVars
, since you can then make sure that it's O.K. to do so.)SSLOptions
is documented to work in an.htaccess
, so you might want to try putting it there (since that bypasses the question ofhttpd-ssl.conf
vs.httpd.conf
and whatnot).new java.util.ArrayList<String>((System.getenv().keySet()).toString()
will be aString
containing a (long) list of all environment variables that you are getting. It may be worth examining it, just to see if maybe the keys look a bit different for some reason from what you'd expect. (TheArrayList
part is probably unnecessary — on my system justSystem.getenv().keySet().toString()
works — but the JDK documentation doesn't seem to guarantee the latter.)使用
System.getenv(String)
,而不是System.getProperty(String)
,检索环境变量。Use
System.getenv(String)
, notSystem.getProperty(String)
, to retrieve environment vars.