我在 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/