添加CORS支持后,Tomcat应用程序无法启动

时间:2015-05-19 07:28:15

标签: maven tomcat cors

我有一个Tomcat 7项目,它在我的Eclipse集成测试服务器上就像一个魅力,但无法在生产服务器上启动。

它以前也在制作过,但我不得不添加CORS支持,不知何故让它失败了。

要添加CORS支持,我将其添加到我的pom.xml

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-catalina</artifactId>
  <version>8.0.22</version>
  <scope>provided</scope>
</dependency>

这是我的web.xml

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

如果我再次删除这些行,它可以在生产服务器上运行,但CORS不起作用。

当我尝试启动应用程序时,服务器会抛出ClassNotFoundException,这很奇怪,因为该类显然位于.war文件中。

catalina日志声称一些.jar文件无法验证,因为&#34; jar未加载&#34;。 .jars都在.war中,所以我不明白它失败的原因。

日志输出:

Catalina日志:

May 19, 2015 9:10:29 AM org.apache.catalina.util.LifecycleBase stop
INFO: The stop() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/APIMR]] after stop() had already been called. The second call will be ignored.
May 19, 2015 9:10:29 AM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/APIMR]
May 19, 2015 9:10:36 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat/webapps/APIMR.war
May 19, 2015 9:10:37 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/share/tomcat/webapps/APIMR/WEB-INF/lib/tomcat-el-api-8.0.22.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
May 19, 2015 9:10:37 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/share/tomcat/webapps/APIMR/WEB-INF/lib/tomcat-servlet-api-8.0.22.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/APIMR] startup failed due to previous errors

localhost日志:

May 19, 2015 8:22:11 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@d45f6a5')
May 19, 2015 9:10:05 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter CorsFilter
java.lang.ClassNotFoundException: org.apache.catalina.filters.CorsFilter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    <snip>


May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter CorsFilter
java.lang.ClassNotFoundException: org.apache.catalina.filters.CorsFilter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    <snip>

有什么建议吗?

3 个答案:

答案 0 :(得分:10)

服务器将拒绝已经属于服务器运行时的jar(tomcat - **。jar,servlet * .jar,...)。请尝试使用此CORS过滤器:http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter

<强>的pom.xml

<dependency>
 <groupId>com.thetransactioncompany</groupId>
 <artifactId>cors-filter</artifactId>
 <version>2.4</version>
</dependency>

<强>的web.xml

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

答案 1 :(得分:2)

在我的情况下,添加依赖项不起作用。当我在tomcat lib文件夹中添加两个jar时,它可以工作,如下所示:

  

CORS滤波器-2.5.jar

     

java的属性-utils的-1.10.jar

答案 2 :(得分:0)

虽然是一个老帖子,但这非常有帮助。我不能不感激地分享这个......你拯救我的人多了几天。

以防万一其他人遇到同样的问题,这就是我所做的:

  1. 将maven依赖项添加到项目的pom.xml文件中: http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter/2.6
  2. 将过滤器添加到项目的web.xml /WEB-INF/web.xml文件中,如下所示,重新启动tomcat:
  3. </web-app>
        <servlet>
            <servlet-name>Jersey Web Application</servlet-name>
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>packages</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Jersey Web Application</servlet-name>
            <url-pattern>/appapi/*</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>CORS</filter-name>
            <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CORS</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    

    这似乎与tomcat服务器,8和9完美配合。如果更改不会生效,请在我的情况下重新启动IDE“ eclipse ”。 再次感谢你们这些出色的工作人员。 :)