I am developing a plugin that provides a REST API. I want to retrieve some data from the api that i provided and display it on an web application on a different URL. So now i need to enable CORS. But i am running into a lot of problems. The code of my api looks like this:
@Path("/project")
@CrossOrigin("http://localhost:4200/")
public class ProjectResource {
private ProjectMonitorService service;
@Inject
ProjectResource(@ComponentImport ProjectMonitorService service) {
this.service = service;
}
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getMessage() {
return Response.ok(new ProjectResourceModel("Hell World", ""))
.header("Access-Control-Allow-Headers", "Authorization")
.header("Access-Control-Allow-Origin", "http://localhost:4200/")
.header("Access-Control-Allow-Methods", "OPTIONS, GET")
.header("Access-Control-Allow-Credentials", "*")
.build();
}My client looks like this:
return this.http.get('http://localhost:7990/bitbucket/rest/project/1.0/project', { headers: new HttpHeaders().append('Authorization', 'Basic YWRtaW46YWRtaW4=') });
Error:
OPTIONS http://localhost:7990/bitbucket/rest/project/1.0/project/number XMLHttpRequest cannot load http://localhost:7990/bitbucket/rest/project/1.0/project/number. 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:4200' is therefore not allowed access. The response had HTTP status code 401
Info: The authenticatin token is the one of the system admin I tried putting a filter on the server that allows almost everything. The problem that i saw is that the Option method is not allowed and it is now possible to allow it(unsure). The problem is that the browser sends a OPTION preflight request. I have no clue how to enable the CORS, so can someone help me.