quarkus: CORS not working correctly
Describe the bug I have a simple REST interface that needs to be accessed by a SPA which is failing because of CORS. I checked again and tried to call my interface via POSTMAN and I do not see any cors headers.
quarkus.http.cors=true
quarkus.http.cors.origins=http://localhost:8080
quarkus.http.cors.headers=accept, origin, authorization, content-type, x-requested-with
quarkus.http.cors.methods=GET,POST,OPTIONS
However if I implement the JAX-RS filter the cors header is showing up. Is there a known BUG or could anybody confirm with version 0.21?
@Provider
public class CORSFilter implements ContainerResponseFilter {
// Logger
private final Logger log = LoggerFactory.getLogger(CORSFilter.class);
@Override
public void filter(final ContainerRequestContext requestContext,
final ContainerResponseContext cres) throws IOException {
cres.getHeaders().add("Access-Control-Allow-Origin","http://localhost:8080");
cres.getHeaders().add("Access-Control-Allow-Headers", "accept, origin, authorization, content-type, x-requested-with");
cres.getHeaders().add("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
cres.getHeaders().add("Access-Control-Max-Age", "1209600");
}
}
UPDATE: I figured out that if I set the quarkus.http.cors, the headers for the method OPTIONS are correctly set however the actual “POST” call will cause the following error (this does not happen by setting the JAX-RS header which should be not necessary):
Access to XMLHttpRequest at 'https://DOMAIN/PATH' from origin 'https://DOMAIN' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 8
- Comments: 23 (15 by maintainers)
I’m seeing this error on 0.22. I can’t upgrade right now, but if you get here through Google, that’s what saved my life for a simple demo using this thread as a reference:
application.properties:
The custom CORSFilter:
Then the
POSTmethod has the required header:The
OPTIONSresponse header is correct:If you guys fixed this in the newer versions, that’s great news, because it’s a super common use case for front end developers that are keen to implement backend services with Quarkus.
We really need to get to the bottom of this.
@Manu206 any chance you could provide a sample application so that we could see what’s not working correctly?
I’m testing CORS using http://www.test-cors.org on Quarkus 0.23.2 and also the latest
masterbranch, with this config file:It seems to work just fine.