在单个tomcat实例上混合使用https和http

时间:2016-06-25 10:46:58

标签: apache tomcat protocols server.xml

我有一个通常在https中运行的应用程序。 Tomcat侦听端口8443:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
           clientAuth="false" sslProtocol="TLS"
           keyAlias="MY_ALLIAS" keystoreFile="PATH_TO_MY_KEY"
           keystorePass="MY_PASWORD" />

Apache侦听80并重定向到8443:

<VirtualHost *:80>
        ServerAdmin MY_EMAIL_ADDRESS
        ServerName localhost
        ServerAlias localhost
        ProxyPass / http://localhost:8443/
        ProxyPassReverse / http://localhost:8443/
        DocumentRoot /var/www/html

最后在web.xml中我添加了:

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

不幸的是,我必须将带有http网站的IFRAME添加到我的一个网站中。安全不是问题。我的问题是Tomcat配置。我想我会用Apache调度流量。但现在我的问题是如何设置Tomcat,以便我可以提供网站http://localhost:8080/siteA,所有其他网站将在https://localhost:8443/myOtherSites上投放?我尝试删除redirectPort =“8443”,但这还不够。我正在使用Tomcat 9.0.0.M4(如果我需要,移动到Tomcat 8不是问题)。 请帮忙!

1 个答案:

答案 0 :(得分:1)

要解决此问题,请在web.xml中再添加一个<security-constraint>标记,如此

<security-constraint>
<web-resource-collection>
<web-resource-name>Unsecured resources</web-resource-name>
<url-pattern>/siteA</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint> `

由于您已将transport-guarantee设置为NONE,因此tomcat不会验证其是否为安全资源。通过这种方式,此<security-constraint>将有助于您通过http访问siteA,而您已宣布的另一个<security-constraint>代码将帮助您访问https上的其他网站。请记住在<url-pattern>标记中给出要保留为http或https的页面的路径。如果这可以解决您的问题,请告诉我:)。