Angular.js app cannot access Boot Actuator endpoints over CORS
My defined endpoints seem to work fine through the simple CORS filter from http://spring.io/guides/gs/rest-service-cors/
However, when I try to access the actuator endpoints with my angularjs app in chrome (running through grunt) I get the following:
XMLHttpRequest cannot load http://localhost:9091/manage/health. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9000' is therefore not allowed access.
XMLHttpRequest cannot load http://localhost:9091/manage/metrics. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9000' is therefore not allowed access.
Completing a request for the endpoint in Chrome I get the appropriate response
{"status":"ok","database":"HSQL Database Engine","hello":1}
I know that Chrome has some issues doing CORS on localhost for development due to security permissions, but some endpoints (those that I have defined) are accessible. There just seems to be issues accessing the actuator endpoints.
Is there something I need to change in my CORS filter or a property that needs to specified for the actuator endpoints?
Works for me:
$ curl -v -X OPTIONS localhost:8080/metrics
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> OPTIONS /metrics HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
< Access-Control-Max-Age: 3600
< Access-Control-Allow-Headers: x-requested-with
< Strict-Transport-Security: max-age=31536000 ; includeSubDomains
< WWW-Authenticate: Basic realm="Spring"
< Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
< Transfer-Encoding: chunked
< Date: Wed, 18 Jun 2014 17:13:22 GMT
<
* Connection #0 to host localhost left intact
Can you share a project that doesn't work for you?
I currently do not have any publicly available version of the project. It is a proof of concept batch processing application running off Spring Boot that provides similar functionality to Spring Batch Admin just without any Java Templates and using an AngularJS client with a Spring REST service. I have implemented some of the batch admin controllers and endpoints that appear to be working. Everything is normal Spring Boot autoconfiguration except for Batch. It is all JavaConfig.
This was my result from curl
E:\AJMDev\DataEngineUI> curl -v -X OPTIONS localhost:9091/manage/metrics
* Adding handle: conn: 0x1dc34e0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1dc34e0) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 9091 (#0)
* Trying ::1...
* Connected to localhost (::1) port 9091 (#0)
> OPTIONS /manage/metrics HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:9091
> Accept: */*
>
< HTTP/1.1 200 OK
< Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
< Content-Length: 0
* Server Jetty(9.1.5.v20140505) is not blacklisted
< Server: Jetty(9.1.5.v20140505)
<
* Connection #0 to host localhost left intact
E:\AJMDev\DataEngineUI> curl -v -X OPTIONS localhost:9090/greeting
* Adding handle: conn: 0x1e23408
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1e23408) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 9090 (#0)
* Trying ::1...
* Connected to localhost (::1) port 9090 (#0)
> OPTIONS /greeting HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:9090
> Accept: */*
>
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
< Access-Control-Max-Age: 3600
< Access-Control-Allow-Headers: x-requested-with
< X-Application-Context: application:dev:9090
< Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
< Content-Length: 0
* Server Jetty(9.1.5.v20140505) is not blacklisted
< Server: Jetty(9.1.5.v20140505)
<
* Connection #0 to host localhost left intact
You have the actuator endpoints on a different port. That makes it a different container and a different ApplicationContext. Can't you just put them on the same port?
Ok, that make sense. That shouldn't be an issue having them on the same port and seems to have fixed the problem.
Thank you.
I'm having this same issue and actually want my management endpoints on a different port. Is there a way to configure the Access-Control-* headers for the actuator endpoints?