Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat application fails to start after adding CORS support

Tags:

cors

maven

tomcat

I have a Tomcat 7 project which works like a charm on my Eclipse-integrated test server, but fails to start on the production server.

It previously worked on production too, but I had to add CORS support, which somehow made it fail.

To add CORS support, I added this to my pom.xml

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

and this to my 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>

If I remove these lines again, it works on the production server, but CORS doesn't work.

The server throws a ClassNotFoundException when I try to start the application, which is strange, because the class is clearly there in the .war-file.

The catalina log claims a few .jar-files fails to validate because "jar not loaded". The .jars are all in the .war, so I do not understand why it fails.

Output from the logs:

Catalina log:

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 log:

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>

Any suggestions?

like image 357
Suppen Avatar asked May 19 '15 07:05

Suppen


People also ask

What is CORS filter Tomcat?

CORS Filter Introduction. This filter is an implementation of W3C's CORS (Cross-Origin Resource Sharing) specification, which is a mechanism that enables cross-origin requests. The filter works by adding required Access-Control-* headers to HttpServletResponse object.


1 Answers

The server will reject jars which already belong to the servers runtime (tomcat-**.jar, servlet*.jar, ...). Try this CORS filter instead: 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>
like image 192
Stefan Avatar answered Oct 30 '22 15:10

Stefan