0
votes

I'm trying to enable CORS in apache-tomee-plus-1.7.2. Per http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter, I've added the following to my conf/web.xml inside tomcat:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,PUT,DELETE,OPTIONS,HEAD</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
  </init-param>
  <init-param>
    <param-name>cors.exposed.headers</param-name>
    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
  </init-param>
  <init-param>
    <param-name>cors.support.credentials</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>cors.preflight.maxage</param-name>
    <param-value>10</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>  

But it's not working. My client is still receiving following exception

Access to XMLHttpRequest at 'http://localhost:8080/WebServiceTest2/webresources/myService' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've also tried adding a manual filter as shown in this tutorial: https://www.baeldung.com/cors-in-jax-rs, but that's not being executed at all. Even after I add the class to the getClasses() method in the applcation config.

Manual filter solution code: package com.mycompany.webservicetest.two.rest.service;

    import java.io.IOException;

    import javax.ws.rs.container.ContainerRequestContext;
    import javax.ws.rs.container.ContainerRequestFilter;
    import javax.ws.rs.container.ContainerResponseContext;
    import javax.ws.rs.container.ContainerResponseFilter;
    import javax.ws.rs.core.MultivaluedMap;
    import javax.ws.rs.core.UriInfo;
    import javax.ws.rs.ext.Provider;
    import java.io.IOException;


    @Provider
    public class CorsFilter implements ContainerResponseFilter, ContainerRequestFilter {

        @Override
        public void filter(ContainerRequestContext reqContext,
                           ContainerResponseContext resContext) throws IOException {
            System.out.println("-- res info --");
            System.out.println("!!!!!!!!!!!! ----------- Filter called!! ");
            resContext.getHeaders().add("Access-Control-Allow-Origin", "*");
            resContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
            resContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
            resContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
            resContext.getHeaders().add("Access-Control-Max-Age", "1209600");
        }


        @Override
        public void filter(ContainerRequestContext reqContext) throws IOException {
            System.out.println("-- req info --: " + reqContext);

        }
    }

Application config file:

    package com.mycompany.webservicetest.two.rest.service;

    import java.util.Set;
    import javax.ws.rs.core.Application;


    @javax.ws.rs.ApplicationPath("webresources")
    public class ApplicationConfig extends Application
    {

        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
            addRestResourceClasses(resources);
            System.out.println("Registering Cors Filter");
            resources.add(com.mycompany.webservicetest.two.rest.service.CorsFilter.class);
            return resources;
        }

        private void addRestResourceClasses(Set<Class<?>> resources)
        {
            // ... my web service classes that aren't filters
        }

    }
1

1 Answers

0
votes

I think that version of tomcat may not support this filter. I switched to tomee-plume-7.1.0, pasted the same exact (with minor alteration, can't have the credentials "true" if allowed origins is *) filter and it worked.