I am trying to get a standalone Jetty server running with a web client. However I am running into issues with the preflight requests that are being sent before the actual request. When I make a request using Postman, I get the expected response (preflight request are not used in Postman), but when the request is made from Chrome I get the following error message:
XMLHttpRequest cannot load http://my.server.ip:8000/hello/api/test. Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed
The header I get back is as follows:
HTTP/1.1 200 OK Date: Wed, 26 Oct 2016 14:51:06 GMT Allow:
POST,OPTIONS Date: Wed, 26 Oct 2016 14:51:06 GMT Content-Length: 0
My relevant code is shown below. Any help solving this would be greatly appreciated as I have already spent several days trying to figure this out. If there is a way to fix this while still using JAXRSServerFactoryBean that would be great.
Thanks for the response Ron. I have tried adding the ContainerResponseFilter as you suggested but am still getting the same error. Do I need to be calling or creating an instance of the AccessControlFilter somewhere in my server code or is the @Provider annotation supposed to take care of tying this class to the rest of my code? Thanks.
I'm not sure what version of CXF you are using, but it looks like CXF v2 does not support registering filters and other providers using the @Provider annotation, and that they need to be registered manaully: Apache CXF - Registering custom providers. CXF v3 does support the @Provider annotation.