使用jQuery的CORS请求的Jenkins JSON REST API [英] Jenkins json REST api with CORS request using jQuery

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

问题描述

我正在尝试使用Jenkins json API,但无法使身份验证正常工作.

I'm trying to use the Jenkins json API and cannot get the authentication to work.

设置:

  • Jenkins安全性:Jenkin’s own user database
  • 访问权限:Matrix-gebaseerde beveiliging
  • 通过 Jenkins CORS插件
  • 使用注册用户的用户名/api令牌
  • Jenkins security: Jenkin’s own user database
  • access: Matrix-gebaseerde beveiliging
  • CORS via Jenkins CORS plugin
  • using username/api token of a registered user

尝试:

var username = "username";
var apiToken = "apiToken";

// username / api-token on url (basic authentication)
$.ajax({
  url: "http://"+username+":"+apiToken+"@host:port/job/test/api/json",
  method: "GET"
});

// username / api-token supplied using jQuery's username/password properties
$.ajax({
  url: "http://host:port/job/test/api/json",
  method: "GET",
  username: username,
  password: apiToken
});

// username / api-token by setting "Authorization" header directly
$.ajax({
  url: "http://host:port/job/test/api/json",
  method: "GET",
  headers: {
      "Authorization": "Basic " + btoa(username + ":" + apiToken)
  }
});

所有这些尝试导致相同的结果: 回复:403禁止访问

All these attempts result in the same result: response: 403 Forbidden

推荐答案

我在此方面取得了一些成功.注意:我必须在我们的nginx代理配置中添加两行add_header行...

I have had some success with this. Note: I had to add two add_header lines to our nginx proxy config...

add_header Access-Control-Allow-Origin 'http://localhost:9001';
add_header Access-Control-Allow-Credentials 'true';

然后我可以在詹金斯做到这一点.只要我的浏览器在另一个选项卡中得到了授权(共享相同的会话/cookie),它似乎不需要任何用户/密码/令牌.

Then I can do this in Jenkins. It seems to not need any user/pass/token as long as my browser is authorised in another tab (shares the same session/cookie).

$.ajax({
  url:'https://myserver.com/job/MYJOB/lastBuild/api/json',
  method: 'GET',
  xhrFields: {
    withCredentials: true
  }
}).done(function(data) {
  console.log(data);
}).fail(function() {
  console.error(arguments);
});

这篇关于使用jQuery的CORS请求的Jenkins JSON REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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