【问题标题】:Getting data from one tomcat server to another tomcat server从一个 tomcat 服务器获取数据到另一个 tomcat 服务器
【发布时间】:2016-08-08 07:46:05
【问题描述】:

我正在使用 saiku 服务器进行数据分析,该服务器在一个 tomcat 中运行,我在另一个 tomcat 中运行我的 spring boot 应用程序,我想从 saiku 服务器获取数据到我的 spring boot 应用程序以从一个 tomcat 生成图表(即)服务器到另一个 tomcat 服务器。我遇到了错误,例如

这是我从 saiku 获取数据到我的 springboot 应用程序的代码

<script type="text/javascript" data-ng-hide=true>

var myClient = new SaikuClient({
    server: "http://localhost:8080/saiku",
    path: "/rest/saiku/embed",
    user: "admin",
    password: "admin"
});
myClient.execute({
    file: "/homes/home:admin/sample_reports/.saiku",
    htmlObject: "#saiku2",
    render: "chart",
    mode: "line",
    chartDefinition: {
            width: 560,
            colors: ['grey','red','blue'],
            extensionPoints: {
                    legend: true,
                    legendShape: 'circle',
                    legendSize: {width: '100%'},
                    legendLabel_textStyle: "#990000",
                    legendFont: 'normal 11px "Open Sans"'
            }
    },
    zoom: true
});
function displayChart(path, htmlDiv){
    myClient.execute({
        file: path,
        htmlObject: "#"+htmlDiv,
        render: "chart",
        mode: "line",
        chartDefinition: {
                width: 560,
                colors: ['grey','red','blue'],
                extensionPoints: {
                        legend: true,
                        legendShape: 'circle',
                        legendSize: {width: '100%'},
                        legendLabel_textStyle: "#990000",
                        legendFont: 'normal 11px "Open Sans"'
                }
        },
        zoom: true
    });
}
$(document).ready(function(){
    console.log("Hello world")
});
    $.get( "http://localhost:8080/saiku/rest/saiku/api/repository?type=saiku,sdb", function( data ) {
        console.log(data);
        console.log(data.length);
        var response = (data);
        for(var i=0;i<data.length;i++){
            console.log("JSON ==>"+data[i].type);
            // get reports only under homes
            if(data[i].type=="FOLDER" && data[i].name=="homes"){
            console.log("data[i].repoObjects ==>"+data[i].repoObjects)
                // for file only we generate the saiku reports
                if(data[i].repoObjects && data[i].repoObjects.length>0){
                    console.log("i am inside"+data[i].repoObjects.length)
                    var arr = [];
                    arr = (data[i].repoObjects);
                    for (var k=0;k<arr.length;k++){
                        console.log("repo ==>"+arr[k].name);
                        if(arr[k].name=="home:admin"){
                            // retrieve all reports under home:admin folder
                            console.log("repo ==>"+JSON.stringify(arr[k].repoObjects[0].repoObjects));
                            // arr[k] is the home:admin folder. iterate all the files
                            for(var z=0;z<arr[k].repoObjects.length;z++){
                                // this is now the list of files and folders under homes/home:admin
                                if(arr[k].repoObjects[z]){
                                    var folderObjects =new Array(arr[k].repoObjects[z].repoObjects);
                                    console.log(arr[k].repoObjects[z].repoObjects.length)
                                    for(var x=0;x<arr[k].repoObjects[z].repoObjects.length;x++){
                                        console.log(x)
                                        if(arr[k].repoObjects[z].repoObjects[x]){
                                            console.log(arr[k].repoObjects[z].repoObjects[x].path)
                                            displayChart(arr[k].repoObjects[z].repoObjects[x].path,"saiku"+x);
                                        }
                                    }
                                }

                            }                       


                        }

                    }
                }


            }
        }
    });



</script>

我的错误是

XMLHttpRequest cannot load http://localhost:8080/saiku/rest/saiku/embed/export/saiku/json?formatter=fl…ned&file=%2Fhomes%2Fhome%3Aadmin%2Fsample_reports%2F.saiku&_=1470641532009. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. The response had HTTP status code 401

【问题讨论】:

    标签: java spring tomcat saiku


    【解决方案1】:

    我从未使用过 Saiku 服务器,但也许我可以大致了解一下为什么会发生此错误。

    您应该禁用访问控制或正确实施它。

    这里有关于正确实现 Spring 功能的详尽指南: https://spring.io/guides/gs/rest-service-cors/

    您还应该在此处查看什么是“跨源资源共享”以及为什么需要它: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

    如果您的 @SpringBootApplication 类扩展了 WebMvcAutoConfigurationAdapter,那么您可以像这样禁用 CORS:

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
    

    您也可以直接在 Tomcat 中禁用 CORS: http://enable-cors.org/server_tomcat.html

    但在执行此操作之前,请阅读 CORS 并了解为什么需要它以及禁用它会产生什么后果。

    【讨论】:

    • 始终欢迎提供指向潜在解决方案的链接,但请add context around the link,以便您的其他用户知道它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。考虑到仅仅是指向外部站点的链接Why and how are some answers deleted? 的一个可能原因。
    • @FrankerZ 我会在这方面建立起来,给我一些时间。
    【解决方案2】:

    您需要使用saiku rest API 进行登录。例如:

    $.post("http://localhost:8080/saiku/rest/saiku/session/", { 用户名:"admin",密码:"admin" });

    检查这个: https://groups.google.com/a/saiku.meteorite.bi/forum/#!msg/user/djic697fUbk/IPWlTyazAwAJ

    如果saiku运行在其他tomcat上,可以在saiku tomcat web.xml中使用CorsFilter

    请谨慎使用 Cors...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多