Spring Boot,Jolokia远程访问返回No'Access-Control-Allow-Origin'标头存在

时间:2015-06-12 02:46:30

标签: cross-domain spring-boot jolokia

我在我的春季启动应用程序中使用jmx设置了jolokia,因此我可以通过HTTP获取jmx信息。

但是我无法通过javascript ajax获取数据,并且错误“No'Access-Control-Allow-Origin'标头出现在请求的资源上。因此,不允许原点'http://localhost:8080'访问。 “

我想让jolokia端点返回'Access-Control-Allow-Origin:*'标头以允许任何来源或者至少返回我在jolokia-access.xml上指定的允许来源的标头,但是我没有得到响应中的标题,我不确定我错过了什么

我正在运行的监控javascript不在具有我的spring启动应用程序的同一台服务器上,因为我想让脚本远程运行。

以下是我的春季启动应用程序设置。

application.properties和jolokia-access.xml都在classpath中,所以我可以看到我对application.properties上的management.port所做的更改以及对jolokia-access.xml的远程帖子访问限制的应用

的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=*

椒-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>

当我通过curl点击jolokia端点时,我可以将响应中的jmx数据作为json,如下所示

$ 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}

然而,javascipt失败,因为来自jolokia端点的resposne没有Access-Control-Allow-Origin'标头。 即使我设置'endpoints.cors.allowed-originins = *'属性,但我没有在响应中看到标题

监控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"
);

请求标头javascript已发送

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

收到javascript的响应标题

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

如上所示,根本没有“Access-Control-Allow-Origin”标题。

1 个答案:

答案 0 :(得分:0)

默认情况下,Jolokia允许任何跨域访问。所以根本不需要jolokia-access.xml

另一方面,我认为你的模式应该有效。但是请在GitHub上提出一个问题,以便我们可以继续解决这个问题。