在JIRA REST API中启用CORS [英] Enable CORS in JIRA REST API

查看:258
本文介绍了在JIRA REST API中启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Confluence用户宏中从JavaScript调用JIRA REST API并且我遇到了CORS问题,因为JIRA和Confluence位于两个不同的域上并且预检请求失败。我已经尝试了几种CORS解决方案,如下所述,没有任何成功。所以我请求其他人可能已经解决了这个问题。



失败的JavaScript代码

  AJS。$。ajax({
类型:GET,
url:http://jira.mydomain.com / rest / api / latest / search /?jql = issue%20in%20linkedIssues(SR-45),
dataType:json,
contentType:application / json,
async:false
})

错误消息(来自Firefox):

 阻止跨源请求:同源策略禁止在http://jira.mydomain中读取远程资源。 COM / REST / API /最新/搜索/?JQL =发行20英寸%%20linkedIssues(SR-45)。这可以通过将资源移动到同一域或启用CORS来解决。 

JIRA配置




  • JIRA版本:6.4.12

  • 网址: http://jira.mydomain.com

  • 在前面运行Apache(代理):是


    • 响应标头配置:


      • Access-Control-Allow-Headers:origin,content-type,accept

      • Access-Control-Allow-Methods:POST,GET,OPTIONS

      • Access-Control-Allow-Origin:*



  • Confluence已添加到白名单中:是




汇合配置





使用浏览器测试:




  • Chrome(最新)

  • Safari(最新)

  • Firefox(最新)



使用CURL测试预检请求(OPTIONS):

  ismar .slomic $ curl -X选项http://jira.mydomain.com/rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45) -  v 
*尝试10.107。 1.24 ...
*连接到jira.mydomain.com(127.0.0.1)端口80(#0)
> OPTIONS / rest / api / latest / search /?jql = issue%20in%20linkedIssues(SR-45)HTTP / 1.1
>主持人:jira.mydomain.com
>用户代理:curl / 7.43.0
>接受:* / *
>
*来自服务器的空回复
*连接#0到主机jira.mydomain.com完好无损
卷曲:(52)来自服务器的空回复

这似乎是积极的回应。



测试预检请求(OPTIONS) Crome扩展 Postman

 选项http://jira.mydomain.com/rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45 )

响应错误:无法得到任何回复。这似乎是连接到http://jira.mydomain.com/rest/api/latest/search/?issue%20in%20linkedIssues(SR-45)的错误

解决方案

有点迟到回答这个问题,但我会留在这里作为参考。



<在我的宏中,我反过来解决了这个问题。我没有向JIRA服务器发送直接请求,而是使用Confluence服务器公开的API将我的JIRA请求代理到链接的JIRA实例。



我描述了这个端点另一个答案。使用此方法不会破坏跨源策略。事实上,这就是 JIRA问题 JIRA Chart 宏用于渲染其小部件。



此方法要求JIRA和Confluence实例通过应用程序链接虽然。但是我假设你有调查更改原始政策的管理员访问JIRA和Confluence,所以它不应该是你的阻止。


I´m calling JIRA REST API from JavaScript in a Confluence User Macro and I´m facing CORS issues because JIRA and Confluence are on two different domains and preflight request from browser is failing. I have tried several CORS solutions as described below, without any success. So Im begging for some input from others that probably have solved this issue.

JavaScript snippet that is failing:

AJS.$.ajax({
            type: "GET",
            url: "http://jira.mydomain.com/rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45)",
            dataType: "json",
            contentType: "application/json",
            async: false
        })

Error message (from Firefox):

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://jira.mydomain.com/rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45). This can be fixed by moving the resource to the same domain or enabling CORS.

JIRA Configuration

  • JIRA Version: 6.4.12
  • Url: http://jira.mydomain.com
  • Running Apache in front (proxy): Yes
    • Response Headers Configuration:
      • Access-Control-Allow-Headers:origin, content-type, accept
      • Access-Control-Allow-Methods:POST, GET, OPTIONS
      • Access-Control-Allow-Origin:*
  • Confluence added to the whitelist: Yes

Confluence Configuration

Tested with browsers:

  • Chrome (latest)
  • Safari (latest)
  • Firefox (latest)

Testing preflight request (OPTIONS) with CURL:

ismar.slomic$ curl -X OPTIONS "http://jira.mydomain.com/rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45)" -v
*   Trying 10.107.1.24...
* Connected to jira.mydomain.com (127.0.0.1) port 80 (#0)
> OPTIONS /rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45) HTTP/1.1
> Host: jira.mydomain.com
> User-Agent: curl/7.43.0
> Accept: */*
>
* Empty reply from server
* Connection #0 to host jira.mydomain.com left intact
curl: (52) Empty reply from server

This seems to be positive response.

Testing preflight request (OPTIONS) with Crome extention Postman:

OPTIONS http://jira.mydomain.com/rest/api/latest/search/?jql=issue%20in%20linkedIssues(SR-45)

Response error: Could not get any response. This seems to be like an error connecting to http://jira.mydomain.com/rest/api/latest/search/?issue%20in%20linkedIssues(SR-45)

解决方案

Little bit late to answer this, but I'll leave it here for reference.

In my macro I solved this problem the other way around. Instead of sending a direct request to the JIRA server I used an API exposed by the Confluence server to proxy my JIRA request to the linked JIRA instance.

I described this endpoint in another answer. Using this method you don't break the cross-origin policy. In fact this is what JIRA Issues and JIRA Chart macros use to render their widgets.

This approach requires JIRA and Confluence instances to be connected through an Application Link though. But I assume you have admin access to both JIRA and Confluence as you are investigating changing the origin policies so it shouldn't be a blocker for you.

这篇关于在JIRA REST API中启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆