Spring Boot, Jolokia Remote Access returns No 'Access-Control-Allow-Origin' header is present

3.4k views Asked by At

I setup jolokia with jmx on my spring boot application, so I can get jmx information over HTTP.

However I can't get the data by javascript ajax with the error "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access."

I want to make jolokia endpoint return 'Access-Control-Allow-Origin: *' header to allow any origin or at lease return the header with allowed origin I specified on jolokia-access.xml, but I don't get the header on response and I'm not sure what I missed

The monitoring javascript I'm running is not on same server which has my spring boot application, since I want to make the script run remotely.

Below is my spring boot application setup.

application.properties and jolokia-access.xml both are in classpath, so I can see the change I made for management.port on application.properties and remote post access restriction on jolokia-access.xml are applied

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.4.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
    </dependency>
        :

application.properties

management.port=8888

endpoints.jolokia.path=/jolokia
endpoints.jolokia.enabled=true

endpoints.jmx.enabled=true

endpoints.cors.allowed-origins=*
endpoints.cors.allowed-methods=*
endpoints.cors.allowed-headers=*

jolokia-access.xml

<?xml version="1.0" encoding="UTF-8"?>
<restrict>
    <remote>
        <host>127.0.0.1</host>
        <host>localhost</host>
    </remote>

    <http>
        <method>post</method>
        <method>get</method>
    </http>

    <commands>
        <command>read</command>
        <command>list</command>
        <command>version</command>
    </commands>

    <cors>
        <allow-origin>http://localhost:*</allow-origin>
        <allow-origin>http://127.0.0.1:*</allow-origin>
    </cors>
</restrict>

When I hit the jolokia endpoint by curl, I can have jmx data on response as json like below

$ curl -X POST http://127.0.0.1:8888/jolokia -d '{
    "type":"read",
    "mbean":"org.springframework.boot:type=Endpoint,name=healthEndpoint",
    "attribute":"Data"
}'
{"request":{"mbean":"org.springframework.boot:name=healthEndpoint,type=Endpoint","attribute":"Data","type":"read"},
"value":{"diskSpace":{"threshold":10485760,"free":204441415680,"status":"UP"},"db":{"database":"H2","hello":1,"status":"UP"},"status":"UP"},
"timestamp":1433908260,"status":200}


$ curl -X POST http://127.0.0.1:8888/jolokia -d '{
    "type":"read", 
    "mbean":"java.lang:type=Memory", 
    "attribute":"HeapMemoryUsage", 
    "path":"used"
}'
{"request":{"path":"used","mbean":"java.lang:type=Memory","attribute":"HeapMemoryUsage","type":"read"},
"value":69036712,"timestamp":1434075946,"status":200}

However below javascipt failed since resposne from jolokia endpoint doesn't have Access-Control-Allow-Origin' header. Even if I setup 'endpoints.cors.allowed-origins=*' properties, but I don't see the header on the response

Monitoring javascript

var j4p = new Jolokia({url: "http://127.0.0.1:8888/jolokia", fetchInterval: 1000});

var context = cubism.context()
    .serverDelay(0)
    .clientDelay(0)
    .step(1000)
    .size(594);
var jolokia = context.jolokia(j4p);

var memory = jolokia.metric(
    function (resp1, resp2) {
        return Number(resp1.value) / Number(resp2.value);
    },
    {type:"read", mbean:"java.lang:type=Memory", attribute:"HeapMemoryUsage", path:"used"},
    {type:"read", mbean:"java.lang:type=Memory", attribute:"HeapMemoryUsage", path:"max"}, "Heap-Memory"
);

Request header the javascript sent

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:127.0.0.1:8888
Origin:http://127.0.0.1:8080
Referer:http://127.0.0.1:8080/monitor/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36

Response header the javascript received

Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Fri, 12 Jun 2015 02:27:17 GMT
Expires:0
Pragma:no-cache
Server:Jetty(9.2.10.v20150310)
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

As you see above, there is no 'Access-Control-Allow-Origin' header at all.

1

There are 1 answers

2
Roland Huß On

By default, Jolokia allows any cross origin access. So there is no need for a jolokia-access.xml at all.

On the other hand, I think your pattern should work. But please raise an issue on GitHub so that we can continue there to fix this.