简体   繁体   English

从AngularJS访问Jenkins API

[英]Accessing Jenkins API from AngularJS

I'm trying to retrieve information about one job build from the api rest provided by Jenkins with Angularjs. 我正在尝试从Jenkins和Angularjs提供的api休息中检索有关一个作业构建的信息。

Jsonp is actually disabled on Jenkins: 在Jenkins上实际上禁用了Jsonp:

Jenkins Security Advisory 2013-02-16 詹金斯安全咨询2013-02-16

so this piece of code can't work: 所以这段代码不能工作:

var url = 'http://jenkins-server:8080/job/job-name/api/json?jsonp=callback';
$http.jsonp(url).success(function (data) {
    console.log(data);
});

throw: 扔:

Uncaught SyntaxError: Unexpected token : 

Cors is not enabled by default... to be honest I can't find the way to install this plugins: 默认情况下不启用Cors ...说实话我找不到安装此插件的方法:

and this code can't work as well 而且这段代码不能正常工作

var url = 'http://jenkins-server:8080/job/job-name/api/json'
$http({url: url, method: 'GET'}).success(function(data){console.log(data)})

You can use this CORS filter plugin: 您可以使用此CORS过滤器插件:

https://wiki.jenkins-ci.org/display/JENKINS/Cors+Filter+Plugin https://wiki.jenkins-ci.org/display/JENKINS/Cors+Filter+Plugin

Or you can host your Angular app on the Jenkins server using the User Content mechanism: 或者,您可以使用用户内容机制在Jenkins服务器上托管Angular应用程序:

https://wiki.jenkins-ci.org/display/JENKINS/User+Content https://wiki.jenkins-ci.org/display/JENKINS/User+Content

There seems to be a plugin now for whitelisting JSON requests...Just go to the plugins, and search for JSON. 现在似乎有一个插件用于将JSON请求列入白名单......只需转到插件,然后搜索JSON即可。

The Secure Access plugin. 安全访问插件。

@Mauro, starting with Jenkins 1.537 you can implement " jenkins.security.SecureRequester " and allow the json request to work. @Mauro,从Jenkins 1.537开始,你可以实现“ jenkins.security.SecureRequester ”并允许json请求工作。

You just have to implement the method permit(StaplerRequest req, Object bean) and have your validations there and just return true (based on your validation result) to allow the request. 您只需实现方法许可(StaplerRequest req,Object bean)并在那里进行验证,然后返回true(基于验证结果)以允许请求。

Once you done that you can simply use the first code snipped you have mentioned in your question. 完成后,您只需使用您在问题中提到的第一个代码剪切。

Example SecureRequester Implementation : - 示例SecureRequester实现: -

import hudson.Extension;
import jenkins.security.SecureRequester;
import org.kohsuke.stapler.StaplerRequest;

@Extension
public class AllowRequest implements SecureRequester {

    public boolean permit(StaplerRequest req, Object bean) {

        // A method to validate the request and return the appropriate result
        return YOUR_VALIDATION_METHOD(req,bean); 
    }

    private boolean YOUR_VALIDATION_METHOD(StaplerRequest req, Object bean) {
        // validation goes here
    }

}

You need to build this as a plugin and install it in you Jenkins setup to work. 您需要将其构建为插件并将其安装在Jenkins设置中才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM