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
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 ;)
Most helpful comment
+1 a CORS filter configurable in the properties and/or a
@CORS
annotation would really sparks joy for the developer ;)