使用AJAX生成tableau受信任票证

We are trying to get a trusted ticket from Tableau Server using Ajax. Tableau team provides support for php, java, sharepoint, ruby but not for Ajax.

The code we are working is:

<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../js/tableau_v8.js"></script>
<script type="text/javascript" src="jsonp.js"></script>
<script type="text/javascript">

$(document).ready(function () {
    $("#tableau").each(function () {

        var workbook = $(this).attr("Presents");
        var view = $(this).attr("MyDashboard");
        var username = $(this).attr("userx");
        var ticket;

        $.ajax({
            type: 'POST',
            url: 'http://serverurl/trusted',
            contentType: 'application/json',
            dataType: 'jsonp',
                data: { 
            username:'registeredusername',
            server:'url},
            success: function (result) {
                alert('Ok');
                ticket = result;
            },
            error: function () { 
                //console.log('Erro');
                alert('Error'); 
            }
        });
        var url = "http://serverurl/trusted/" + ticket + "/views/" + workbook + "/" + view;
        var options = {
            width: this.offsetWidth,
            height: this.offsetHeight,
            hideTabs: true,
            hideToolbar: false,
            onFirstInteractive: function () {
            workbook = viz.getWorkbook();
            activeSheet = workbook.getActiveSheet();
            }

            };
        viz = new tableauSoftware.Viz(this, url, options);
    });

  });
</script>

To give you an idea, please find below how it works in php:

TABLEAU_TRUSTED.php

<?php

// Returns a trusted URL for a view on a server for the
// given user.  For example, if the URL of the view is:
//    http://tabserver/views/MyWorkbook/MyView
//
// Then:
//   $server = "tabserver";
//   $view_url = "views/MyWorkbook/MyView";
//
function get_trusted_url($user,$server,$view_url) {
  $params = ':embed=yes&:toolbar=yes';


  $ticket = get_trusted_ticket($server, $user, $_SERVER['REMOTE_ADDR']);
  if($ticket > 0) {
    return "http://$server/trusted/$ticket/$view_url?$params";
  }
  else 
    return 0;
}

// Note that this function requires the pecl_http extension. 
// See: http://pecl.php.net/package/pecl_http

// the client_ip parameter isn't necessary to send in the POST unless you have
// wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
Function get_trusted_ticket($wgserver, $user, $remote_addr) {
  $params = array(
    'username' => $user,
    'client_ip' => $remote_addr
  );

  return http_parse_message(http_post_fields("http://$wgserver/trusted", $params))->body;
}

?>

TABLEAU_SAMPLE_VIEW_PAGE.php

    <p>An embedded view appears below:</p>

<?php

// This user-provided library should define get_user(), which returns the 
// name of the user currently logged into this application.
//
include 'auth.php';

// Tableau-provided functions for doing trusted authentication
include 'tableau_trusted.php';

?>

<iframe src="<?php echo get_trusted_url(get_user(),'localhost','views/Date-Time/DateCalcs')?>"
        width="400" height="400">
</iframe>

<p>
This was created using trusted authentication.
</p>

Any ideas how this php could work in javascript and ajax? Any ideas how could we improve our code?

Thanks, Gabriel

1个回答



Gabriel - 正如Tableau Software论坛上的帖子所警告的那样,您不希望仅使用(客户端)请求受信任的票证 )JavaScript / AJAX。 这样做会让您遇到很多麻烦,主要是打开Tableau Server,以便任何人都可以为其他人请求票证。 实现这种方式,你很快就会失业:)</ p>

你可以使用AJAX来调用服务器端的PHP函数,即使这样做是一种kludge。 但是,由于页面将存在于您信任的单个服务器上,因此您是安全的。</ p>

以下是您的代码的外观:</ p>

首先,修改tableau_trusted.php,添加一个或两个函数:</ p>

  function generateTicket()
{

$ ticket = get_trusted_ticket_direct($ _ POST ['server '],$ _POST ['user'],$ _POST ['targetsite']);
echo $ ticket;

}

if($ _POST ['toDo'] =='generateTicket'){

generateTicket();
}
</ code> </ pre>

以下是调用页面的方法:</ p>

  // 用于提供受信任票证检索的变量
var phpScript =“http://someserver/tableau_trusted.php”,
userName =“someuser”,
serverURL =“tableauserverlocation”;

//用于保存受信任票证的变量
var incomingTicket;

$ .post(phpScript,{
toDo:'generateTicket',
user:userName,
server:serverURL,
targetsite:''
},函数(响应) {
//做一些回应(票)r 在这里
incomingTicket = response;
});
</ code> </ pre>

我猜你正试图重新使用其中一个Tableau样本而不是写作 你自己的代码,没关系。 但是,你所经历的体操可能比写一个简单的网络服务(或Windows服务,或其他)为你做这项工作更麻烦,然后从你的门户页面调用它。 无论如何,这将是一个更清洁的设计模式。 </ p>

祝你好运!</ p>
</ div>

展开原文

原文

Gabriel - As the post on the Tableau Software forums warns, you do NOT want to ask for a trusted ticket using only (client-side) JavaScript/AJAX. Doing so will get you into lots of trouble, essentially opening Tableau Server up so that anyone can request a ticket for anyone else. Implement that way, and you'll shortly be out of a job :)

You can use AJAX to call a server-side PHP function, even though doing so is kind of a kludge. However, since the page will live on a single server, trusted by you, you're safe.

Here's what your code might look like:

First, modify the tableau_trusted.php, adding a function or two:

function generateTicket()
{

       $ticket= get_trusted_ticket_direct($_POST['server'], $_POST['user'], $_POST['targetsite']);
       echo $ticket;

}

if ($_POST['toDo'] == 'generateTicket') {
    generateTicket();
}

Here's how you might call the page:

// variables to feed trusted ticket retrieval
    var phpScript = "http://someserver/tableau_trusted.php",
        userName = "someuser",
        serverURL = "tableauserverlocation";

   // variable to hold trusted ticket
        var incomingTicket;

   $.post(phpScript, {
    toDo: 'generateTicket',
        user: userName,
        server: serverURL,
        targetsite: ''
    }, function(response) {
        // do something with response (the ticket) right here
        incomingTicket = response;
    });

I'm guessing you're trying to re-use one of the Tableau samples vs. writing your own code, and that's fine. BUT, the gymnastics you're putting yourself through may be more troublesome than writing a simple web service (or windows service, or whatever) to do this work for you, then calling it from your portal's page. That would be a cleaner design pattern, anyway.

Good luck!

douxin2011
douxin2011 谢谢! 我们按照你的建议关于AJAX调用服务器端PHP函数,自去年以来它运行良好。 抱歉延迟回答。
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐