apache - 将 HTTP 重定向到 HTTPS :PORT in Tomcat

标签 apache tomcat web-applications https server.xml

我有一个正在运行的 tomcat 应用程序,它已经具有以下从 HTTP 到 HTTPs 的重定向规则:

<Connector executor="tomcatThreadPool"
           port="80"
           protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="443" />

是否可以添加一个异常(exception)/规则,一个特定的 HTTP 请求(http://www.example.com)将被重定向到另一个特定的地址,指定一个端口(比如 https://www.example.com:8443/test),而不更改/删除上面的连接器?

最佳答案

您可以通过将此添加到 tomcat_dir/conf/web.xml 的末尾来对部署到 tomcat 的每个应用程序执行此操作:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Entire Application</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <!-- auth-constraint goes here if you requre authentication -->
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

因此您不必在您的网络应用程序的 web.xml 上更改它。

这应该可行,假设您已经在另一个端口(通常是 443)上运行 https。如果不这样做,请确保您的 tomcat_dir/conf/server.xml 如下所示:

<!-- Default tomcat connector, changed the redirectPort from 8443 to 443 -->
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" />

<!-- To make https work on port 443 -->
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
        maxThreads="150" SSLEnabled="true">
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol"/>
    <SSLHostConfig>
        <Certificate certificateKeyFile="/your/own/privkey.pem"
            certificateFile="/eyour/own/cert.pem"
            certificateChainFile="/your/own/chain.pem"
            type="RSA" />
    </SSLHostConfig>
</Connector>

关于apache - 将 HTTP 重定向到 HTTPS :PORT in Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208796/

相关文章:

php - Apache 2 多 View 和图像/* 请求的 406 错误

java - 从 JSP 页面定向到 servlet 并返回到 JSP 会导致不同的 JSP 页面

java - ApacheConnectorProvider : Jersey Client 2. 5.1

mysql - 为什么Vertrigo 2.21(MySQL服务器无法启动)在Windows 7下无法运行?

drupal - 自定义 java webapp 和 drupal 之间的 SSO 集成

hibernate - 启动 Tomcat 9.0.13 时,cglib jar 总是会出现不同版本的错误

java - 调用 renderURL 后 renderRequest 属性始终为 null

python - 使用 Jinja2 模板缩小 Flask 应用程序的 HTML 输出

java - 在 JBoss 上部署问题

apache - Htaccess 多语言 mod_rewrite