Tomcat:如何在Java中获取安全端口号?

发布于 2024-08-01 13:58:59 字数 301 浏览 3 评论 0原文

我想在我的一个不安全页面中嵌入一个指向我的应用程序安全页面的链接。 安全 tomcat 端口在 server.xml 文件中配置。 在某些部署中,它是 443、8443 等。 所以我需要的是一种从 tomcat 配置读取安全端口以在链接中使用它的方法。 那可能吗?

或者,简单地访问 server.xml 配置(从请求的上下文中)并自己解析它以找出端口号也是可以接受的,但不太理想。

我意识到可能有多个连接器和多个安全连接器,因此我将让程序的逻辑来决定选择哪一个。 问题是 - 我如何获取该信息?

谢谢!

I'd like to embed a link to a secure page of my application in one of my unsecure pages.
The secure tomcat port is configured in the server.xml file. In some deployments it's 443, 8443 etc.
So what I need is a way to read the secure port from tomcat configuration to use it in the link. Is that possible?

Alternatively, simply getting access to the server.xml configuration (from within the context of the request) and parsing it myself to figure out the port number is also acceptable, but less desired.

I realize there could be several connectors, and several secure ones, so I'll leave it to my program's logic to decide which one to choose. Problem is - how do I get that info?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-08-08 13:58:59

最简单的解决方案可能是在 servlet 的 init() 方法中搜索 server.xml 文件,解析它并将端口号存储在某处。 servlet 应该自动加载。

另一种选择是将此代码放入构建脚本中,并在构建时将值复制到 web.xml 文件中。 但这意味着您必须在本地安装相同的 Tomcat,或者必须能够远程访问 server.xml 文件。

我更喜欢第一个解决方案,因为您可以让 servlet 提前失败,并且如果无法确定端口,则 web 应用程序将不会出现。 这样,您就不会在将来某个未指定的时间遇到​​神秘错误,并且您不需要在每个请求期间浪费时间(如果不重新启动 Tomcat,端口号就无法更改)。

The most simple solution is probably to search for the server.xml file in the init() method of a servlet, parse it and store the port number somewhere. The servlet should be auto-loaded.

Another option would be to put this code into your build script and copy the value into the web.xml file at build time. But that means that you must have the same Tomcat installed locally or you must have remote access to the server.xml file.

I prefer the first solution since you can have the servlet fail early and the webapp won't come up if the port can't be determined. That way, you won't have a mysterious error at some unspecified time in the future and you won't need waste time during each request (the port number can't change without a restart of Tomcat).

美煞众生 2024-08-08 13:58:59

我很确定没有 API 可以做到这一点。 您可以通过 web.xml 中的 servlet 环境参数使其可配置。 明显的缺点是您现在有 2 个位置配置了 SSL 端口号。

另一种方法是在 web.xml iirc 中配置安全性,例如

 <security-constraint>
    <web-resource-collection> 
        <web-resource-name>MyLoginPage</web-resource-name>
        <url-pattern>/login</url-pattern> 
        <http-method>GET</http-method> 
        <http-method>POST</http-method> 
    </web-resource-collection> 
    <user-data-constraint> 
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
     </user-data-constraint> 
 </security-constraint>

您可以使用登录页面的普通链接,并且 tomcat 应该自动将指令发送到 ssl 连接器,无论配置为哪个端口。

I'm pretty sure there's no API for that. You could probably keep it configurable via a servlet environ parameter in web.xml. The obvious drawback is you now have 2 places the SSL port number is configured.

Another approach is to configure security in web.xml iirc something like

 <security-constraint>
    <web-resource-collection> 
        <web-resource-name>MyLoginPage</web-resource-name>
        <url-pattern>/login</url-pattern> 
        <http-method>GET</http-method> 
        <http-method>POST</http-method> 
    </web-resource-collection> 
    <user-data-constraint> 
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
     </user-data-constraint> 
 </security-constraint>

You can just use normal links to the login page, and tomcat should automatically send redicts to the ssl connector, whichever port that is configure to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文