我编写了一些显示附件列表的代码。但问题是,我希望用户能够在不登录的情况下查看附件。有没有办法对用户进行身份验证?这是我的代码:
//to get cases. this returns a list of attachments, with the url in Jira
$attachments = $jira_case->issues[0]->fields->attachment;
//iterates over the lists and creates links
foreach ($attachments as $i=> $attachment) {
$html .="
<tr>
<td style='padding-left: 10px; width:0px; padding-top: 20px;' colspan='3'>
". ($i+1) .") <a target = '_blank' href ='". $attachment->content ."'>". nl2br($attachment->filename) ."</a>
</td>
</tr>";
}
问题是,当用户单击链接时,如果他们没有登录,他们将被要求登录。我不希望这样,因为这是我的应用程序进行身份验证,以便用户不需要 jira帐户。这可能吗?也许通过传递某种令牌?
从 JIRA 的角度来看,来自用户浏览器的请求不包含任何身份验证标头。 JIRA 对附件使用与问题相同的权限集,因此每当用户能够查看特定的 JIRA 问题时,他也能够查看(和下载)附件。因此,除非您让匿名用户可以访问您的项目和问题,否则具有适当权限的人必须进行身份验证。如果您希望用户只能通过您的应用程序下载附件,作为您的应用程序用户,那么您需要通过您的应用程序以某种方式代理它。您需要生成指向您的服务器的链接,而不是在页面上呈现直接指向 JIRA 的链接,然后您的服务器应该联系 JIRA 以获取附件,并将自己验证为您的应用程序用户(有权查看包含附件的问题)并传输JIRA 对最终用户的响应。
如果其他人想知道如何做到这一点:
$url = "https://mySite.atlassian.net/secure/attachment/". $attachment_id ."/". $attachment_name ."?os_username=". $this->jira_user_name ."&os_password=" . $this->jira_password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$file = curl_exec($ch);
curl_close($ch);
if($file){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$attachment_name);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo ($file);
exit;
}else{
throw new Exception("Error: file now found.");
}
//导入
使用 Unirest\Request 作为 UnirestRequest;
//Laravel 代码 //控制器
$url =“https://your-domain.atlassian.net/rest/api/3/attachment/content/{id}”;
$headers = array('Accept' => 'application/json');
UnirestRequest::auth(env('JIRA_PROJECT_USER'), env('JIRA_PROJECT_KEY'));
$response = UnirestRequest::get($url, $headers);
返回$响应->正文;