Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

CORS Feature in 8.1.1 #498

Closed
PhilippS93 opened this issue Dec 28, 2020 · 12 comments · Fixed by #499
Closed

CORS Feature in 8.1.1 #498

PhilippS93 opened this issue Dec 28, 2020 · 12 comments · Fixed by #499
Labels

Comments

@PhilippS93
Copy link

PhilippS93 commented Dec 28, 2020

Describe the bug
The CORS feature seems not to work, even when specifying wildcard operator. Seems like org/springframework/web/cors/CorsConfiguration.java is calling getAllowedOrigins() before the value is initialized by setAllowedOrigins().

Expected behavior
CORS settings are are applied

@PhilippS93 PhilippS93 added the bug label Dec 28, 2020
@oliemansm
Copy link
Member

@PhilippS93 So you've configured it like this?

graphql.servlet.cors.allowed-origins: "*"

@PhilippS93
Copy link
Author

Exactly, I tried (.yml file):

graphql:
  servlet:
    cors:
      allowed-origins:
        - "*"
        - "http://mydomain.com"
        - "http://mydomain.com:8080"
        - "^(http?://(?:.+\.)?mydomain\.com(?::\d{1,5})?)$."

This should have covered everything.

Best,
Philipp

@oliemansm
Copy link
Member

oliemansm commented Dec 28, 2020

And this exact same configuration does work with version 8.0.0?

@PhilippS93
Copy link
Author

On 8.0.0, no configuration was needed. Because of the change in 8.1.0 (CORS), this configuration seems to be mandatory. But it is not working either in 8.1.0 nor 8.1.1.

@oliemansm
Copy link
Member

Found the problem which will be fixed in next version. That'll contain an upgrade to graphql-java 16.1 too.

Could you set the allowed headers in the meantime?

graphql.servlet.cors.allowed-headers: GET, HEAD, POST

@oliemansm oliemansm linked a pull request Dec 28, 2020 that will close this issue
oliemansm added a commit that referenced this issue Dec 28, 2020
@PhilippS93
Copy link
Author

I added

      allowed-headers:
        - "GET"
        - "HEAD"
        - "POST"

and also

      allowed-origins:
        - "*"

but I am still getting Invalid CORS request in my response request (403).

@oliemansm
Copy link
Member

Sorry my mistake. You should add allowed-methods instead of headers.

@PhilippS93
Copy link
Author

Thanks, that solved it.

@moose-byte
Copy link

moose-byte commented Feb 8, 2021

I ran into this same problem and adding allowed-methods solved it for me! I created a PR to update the README and add that to the GraphQL Servlet section

#530

@remo87
Copy link

remo87 commented Jan 22, 2022

I have this config and still it doesn't work
graphql:
servlet:
exception-handlers-enabled: true
cors:
allowed-origins:
- "*"
allowed-methods:
- "GET"
- "HEAD"
- "POST"

@hilbertglm
Copy link

@remo87, I am having problems with this as well, but you might want to try the comma-separated list for allowed methods instead of the YAML array notation.

@VladReutCoqniteq
Copy link

VladReutCoqniteq commented Apr 7, 2022

i'm having same issue with @remo87 and @hilbertglm.
find two solution works for me
1. application.yml
graphql:
servlet:
exception-handlers-enabled: true
cors:
allowed-origins: ""
allowed-methods: "
"
allowed-headers: ""
2. bean Filter
@configuration
public class Filter {
@bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("
");
config.addAllowedHeader("");
config.addAllowedMethod("
");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants