Quarkus: Include a CORS Filter that can be enabled via configuration

Created on 17 Apr 2019  路  4Comments  路  Source: quarkusio/quarkus

Enabling CORS is a common need when developing javascript client apps(Angular, Vue, React) connected to a Quarkus backend.
With developer joy in mind, It will be nice to have a CORS filter that could be enabled/disabled or configured in the application.properties

areresteasy kinenhancement

Most helpful comment

+1 a CORS filter configurable in the properties and/or a @CORS annotation would really sparks joy for the developer ;)

All 4 comments

Also having a similar issue,

I can enable CORS via adding a class in my application as such:

package org.acme.FruitService;

import org.jboss.resteasy.plugins.interceptors.CorsFilter;

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

@Provider
public class CORSProvider implements Feature {
    @Override
    public boolean configure(FeatureContext context) {
        CorsFilter filter = new CorsFilter();
        filter.getAllowedOrigins().add("*");
        filter.setAllowedMethods("GET, POST, OPTIONS, HEAD");
        filter.setAllowedHeaders("accept, content-type, origin");
        context.register(filter);
        return true;
    }
}

But there is still a CORS issue when loading resources, for example if I wanted to load the application definition swagger file from META-INF/resources/swagger.yaml

That's because the provider you added is only executed for REST resources. You need to add a Servlet filter to cover all the requests.

Try adding a filter like this https://github.com/fabric8-launcher/launcher-backend/blob/master/web/src/main/java/io/fabric8/launcher/web/providers/CorsFilter.java

Thanks to @gastaldi

Thanks @cristhiank, that seems to do the trick.

I agree that an extension or app property way to enable CORS on the REST resources and/or app resources would be great.

+1 a CORS filter configurable in the properties and/or a @CORS annotation would really sparks joy for the developer ;)

Was this page helpful?
0 / 5 - 0 ratings