【问题标题】:Is Tomcat 8 reading the web.xml and context.xml files in my Java Rest API (war)?Tomcat 8 是否正在读取我的 Java Rest API (war) 中的 web.xml 和 context.xml 文件?
【发布时间】:2018-11-02 02:01:57
【问题描述】:

我在 Eclipse(包括 Javax 和 Jersey 库)中创建了一个 Java Rest API(动态 Web)项目并将其部署到我的 Tomcat 8 服务器。

当我通过 HTTP(例如http://www.mydomain.co.uk:8080/MyJavaProject/context_string/path_params)调用它时,它会返回一个基本的 JSON 响应,但是当我在服务器上启用 SSL/TLS 并使用正版证书时,它会在 Postman 中提供一个 404 Not Found

error.log/path/MyJavaProject/context_string/path_params 上显示file does not exist 错误,但该文件不存在,因为它是我的 REST API 中的参考参数,所以我认为 Tomcat 不知道使用 WAR 文件。

如果我调用https://www.mydomain.co.uk/MyJavaProject,我可以进入目录结构导航器。

我的日志一切正常,现在没有显示错误。我已将安全值放在我的应用程序的web.xml 中的WEB-INF 中。 WAR 及其分解文件夹的权限均为777

我已经读到这个区域使用了一个叫做context.xml的东西,但是如果设置了autoDeploy-true(我的是),则可能会或可能不会使用。

我的问题是:如何确定 Tomcat 是否真的在我的应用程序中读取 context.xmlweb.xml?这是我尝试解决 404 错误的最后一个想法。

这是我应用的web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MyProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>
com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <security-constraint>
<web-resource-collection>
<web-resource-name>MyProject</web-resource-name>
<url-pattern>/context_string/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>

这是我应用的context.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="" path="" reloadable="true">
</Context>

【问题讨论】:

  • 告诉我们更多关于您的设置的信息。您是否在 tomcat 或放置在 tomcat 前面的前端服务器上启用了 HTTPS?
  • 我相信我已经在 Tomcat 本身上启用了。根据论坛帖子,在 conf 文件中设置 *.443 VirtualHost,在 server.xml 中设置 8443 连接器。当我在浏览器中输入 URL(我在其中获得 WAR 文件目录结构)时,它显示为“安全”,我可以看到正在使用的正确证书。
  • 端口号有冲突。要么在 VirtualHost 和 Connectors 中都使用 8443,要么在 VirtualHost 和 Connectors 中都使用 443。
  • 我将 .conf 中的 8443 替换为 443,但没有发现差异

标签: java rest tomcat jersey tomcat8


【解决方案1】:

您需要在 apache2 或 nginx 中使用反向代理,以在内部将请求重定向到 tomcat。

https 在服务器上使用 443 端口作为默认端口。因此,您需要使用 https 定义新端口或重定向相同的端口。 阅读thisthis 以在apache2 中进行更改。

这更像是评论,但我无法添加评论。

【讨论】:

  • 我会试试这个。昨晚我确实看到了一个帖子,其中有人在(我认为是)conf 文件中有“反向代理”标签,所以我会试试看。谢谢
  • 实现了这一点,它使错误在error.log中消失了。 Systemctl config 显示它“没问题”。所有的日志看起来都很开心。我可以看到反向代理值的更改会更改 404 浏览器错误。但是当我使用 Postman 时,404 仍然存在。我仍然认为我的 web.xml 或(现已删除)context.xml 中缺少某些内容。谢谢
【解决方案2】:

在此调用中使用两个 cmets 解决了问题。

  1. 有一个端口冲突,所以我将 server.xml 和 apache2/../000default.conf 都更改为使用 8443(我发现 443 气质)。 与此同时,我现在在所有 URL 调用中都将“8443”作为端口,而不是没有端口。

  2. 我将此添加到 .conf 文件中:

    SSLProxyEngine 开启

    ProxyPass /MyProject/context https://www.mydomain.co.uk/MyProject/context

    ProxyPassReverse /MyProject/context https://www.mydomain.co.uk/MyProject/context

  3. 还必须 a2enmod proxy_http 和 将 VirtualHost 标记设置为 ip 而不是 '*',即 ipaddress.8443 为了更好地衡量,请使用

  4. 检查它是否有效

apache2ctl 配置测试

我并没有对我的应用程序中的 web.xml 做更多的事情,我从我的应用程序中删除了 context.xml。

这确实是一个调整旋钮和转盘直到它起作用的案例,我花了大约 2 周的时间。为什么不容易。

这是我的文件供参考

apache2/..conf

<VirtualHost ipaddress:8443>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

        LogLevel debug
        #SetEnv HTTPS on
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/mydomain_co_uk.crt
        SSLCertificateKeyFile /etc/ssl/private/www.mydomain.co.uk.key
       # SSLCertificateKeyFile /etc/ssl/certs/mydomain_co_uk.pem
        SSLCACertificateFile /etc/ssl/certs/mydomain_co_uk.ca-bundle
        ServerName www.mydomain.co.uk
SSLProxyEngine on
    ProxyPass /MyProject/context https://www.mydomain.co.uk/MyProject/context 
    ProxyPassReverse /MyProject/context https://www.mydomain.co.uk/MyProject/context

Alias /ProjectName"/var/lib/tomcat8/webapps/ProjectName"


<Directory "/var/lib/tomcat8/webapps/ProjectName">
Options Indexes FollowSymLinks
AllowOverride all
Allow from all
</Directory>



<Location />
Require all granted
</Location>



        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

服务器.xml

<Connector
           protocol="org.apache.coyote.http11.Http11AprProtocol"
           port="8443" maxThreads="200" clientAuth="false"
           scheme="https" secure="true" SSLEnabled="true"
           SSLCertificateFile="/etc/ssl/certs/mydomain_co_uk.crt"
           SSLCertificateKeyFile="/etc/ssl/private/www.mydomain.co.uk.key"
           SSLPassword="planetx"
           SSLVerifyClient="optional" SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"/>


    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

来自我的应用程序 web-inf 的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Projectname</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>
com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <security-constraint>
<web-resource-collection>
<web-resource-name>Projectname</web-resource-name>
<url-pattern>/context</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>

【讨论】:

  • 恕我直言 - 一旦您了解代理、反向代理、连接器、它们的端口以及如何编排它们以实现最终结果,这并不难。
猜你喜欢
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多