9

I am developing an app using AngularJS and Resteasy. As expected I am facing the well known problem of

XMLHttpRequest cannot load http://localhost:8080/..... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.

As seen on other stack overflow posts [1], I tried using Resteasy's CorsFilter from a Feature object, but I get:

[33m02:06:57,883 WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-1) failed to execute: javax.ws.rs.ForbiddenException: Origin not allowed: http://localhost:3000
    at org.jboss.resteasy.plugins.interceptors.CorsFilter.checkOrigin(CorsFilter.java:194)
    at org.jboss.resteasy.plugins.interceptors.CorsFilter.filter(CorsFilter.java:134)

My CorsFeature object:

@Provider
public class CorsFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        CorsFilter corsFilter = new CorsFilter();
        corsFilter.getAllowedOrigins().add("*");
        context.register(corsFilter);
        return true;
    }  
}

In web.xml I added:

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>org.jboss.resteasy.plugins.interceptors.CorsFilter</param-value>
</context-param>

I see that when I comment this context-param, I don't get the aforementioned Exception and the response status is 200, rather than 403.

In the angular module config I added:

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

I know there is the option of creating a node.js proxy, but I'd like to solve this the hard way. Can you please help me overcome this big obstacle in life?

Thanks :)

Later edit: I managed to accomplish that by annotating the feature class (CorsFeature) si @Component. That way the application context is aware of it.

Community
  • 1
  • 1
user3159152
  • 611
  • 4
  • 12
  • 20

1 Answers1

19

Solution for Wildfly

  1. edit standalone.xml:

config

<subsystem xmlns="urn:jboss:domain:undertow:3.0">
<buffer-cache name="default"/>
<server name="default-server">
    <http-listener name="default" socket-binding="http" redirect-socket="https"/>
    <host name="default-host" alias="localhost">
        <location name="/" handler="welcome-content"/>
        <filter-ref name="server-header"/>
        <filter-ref name="x-powered-by-header"/>
        <filter-ref name="Access-Control-Allow-Origin"/>
        <filter-ref name="Access-Control-Allow-Methods"/>
        <filter-ref name="Access-Control-Allow-Headers"/>
        <filter-ref name="Access-Control-Allow-Credentials"/>
        <filter-ref name="Access-Control-Max-Age"/>
    </host>
</server>
<servlet-container name="default">
    <jsp-config/>
    <websockets/>
</servlet-container>
<handlers>
    <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
</handlers>
<filters>
    <response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
    <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
    <response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="*"/>
    <response-header name="Access-Control-Allow-Methods" header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT"/>
    <response-header name="Access-Control-Allow-Headers" header-name="Access-Control-Allow-Headers" header-value="accept, authorization,  content-type, x-requested-with"/>
    <response-header name="Access-Control-Allow-Credentials" header-name="Access-Control-Allow-Credentials" header-value="true"/>
    <response-header name="Access-Control-Max-Age" header-name="Access-Control-Max-Age" header-value="1"/>
</filters>
</subsystem>
  1. restart Wildfly
Community
  • 1
  • 1
Eugene Lebedev
  • 1,400
  • 1
  • 18
  • 30
  • 3
    This is not correct if your resources are protected, as `ACA-Credentials: true` cannot be used with `ACA-Origin: *`. Your response's `ACA-Origin` value must be exactly the `Origin` that was presented in the request. – Coderer Jun 30 '20 at 09:43
  • As to fulfill @Coderer comment I post here a link on how to correctly set `` as ACA-Origin value in your standalone XML file: https://developer.mozilla.org/ru/docs/Web/HTTP/Headers/Access-Control-Allow-Origin – Václav May 21 '21 at 08:13
  • 1
    @Václav your link does explain what the value is supposed to be, but does not show how to set it in the Wildfly config. I think there is not an easy way to do this, and it might just be a half-decade-old bug in the implementation of the CorsFilter class. – Coderer May 24 '21 at 10:51