java - 对于 Tomcat 服务器上托管的 Jenkins,将 HTTP 重定向到 HTTPS?

标签 java jenkins tomcat redirect http-redirect

我在 tomcat 9(Linux 上)上部署了 jenkins.war,并将其配置为 http 和 https。

server.xml 上的配置

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
           redirectPort="8443" />
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="150" SSLEnabled="true">
    <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/ssl/test.keystore"
                     type="RSA" certificateKeystorePassword="changeit"/>
    </SSLHostConfig>
</Connector>

web.xml 上的配置

   <security-constraint>
        <web-resource-collection>
            <web-resource-name>HTTPSOnly</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

当未托管 jenkins 时,我可以使用上述配置将 http 重定向到 https。但是部署 jenkins.war 后,将 http 重定向到 https 不起作用。

将 jenkins 的 http 重定向到 https 是否需要任何其他配置更改?

最佳答案

如果您向“$CATALINA_BASE/conf/web.xml”添加安全约束,该约束将使用 Servlet 规范中定义的组合约束规则与 Web 应用程序中定义的任何约束组合。

Jenkins 默认定义以下安全约束:

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>other</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <!-- no security constraint --> 
  </security-constraint>

Servlet 规范中的相关规则是:

The combination of user-data-constraints that apply to a common url-
pattern and http-method shall yield the union of connection types accepted by
the individual constraints as acceptable connection types. A security constraint that
does not contain a user-data-constraint shall combine with other user-data-
constraint to cause the unprotected connection type to be an accepted connection
type.

因此 Jenkins 规则与您的规则相结合,结果是允许不 protected (http) 连接。

可能的解决方案包括:

  • 从 jenkins.war 中打包的 web.xml 文件中删除安全约束
  • 从 jenkins.war 中打包的 web.xml 文件编辑安全约束
  • 部署自定义 Valve 或过滤器来强制执行重定向

关于java - 对于 Tomcat 服务器上托管的 Jenkins,将 HTTP 重定向到 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73278351/

相关文章:

Java删除和重命名文件问题

java - 在哪里检查值并抛出异常

jenkins - 如何将构建 Artifactory 的快照版本上传到 Artifactory 中的 gradle 存储库

macos - 如何在 Mac 上从 Jenkins 运行 docker builds?

ruby - 尽管 RVM 插件 Jenkins 使用了错误的 ruby​​ 版本

java - Tomcat 不会在浏览器中加载默认网页

java - 从可绘制文件夹获取图像并调整其大小时出现 NullPointerException

java - 算术题

tomcat - 在应用服务器上运行maven项目

apache - 用于查找 Apache Tomcat 正在运行的端口的批处理脚本