我在使用Symfony5时遇到安全问题。我已经配置了security.yml:
access_control:
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
一切正常,除了我尝试加载文件(PDF)时,即使允许路由也是如此。 pdf显示登录表单,但我已经登录了。我觉得是因为我在控制器中返回了File对象:
public function viewpdf($id, \Knp\Snappy\Pdf $snappy) {
// some code
// load the file from the filesystem
$file = new File($path_file);
// display the file contents in the browser instead of downloading it
return $this->file($file, $file_name, ResponseHeaderBag::DISPOSITION_INLINE);
}
当我要强制下载时,另一个控制器有相同的问题:
return new PdfResponse(
$snappy->getOutput($pageUrl),
$file_name
);
已经登录后如何查看和下载pdf?谢谢,
确定,在两种情况下,我都找到了解决此问题的方法:
// Inside controller
// KNP bbundle does not have the login/session data
$session = $this->get('session');
$session->save();
session_write_close();
$PHPSESSID =$this->get('session')->getId();
$output = $snappy->getOutput($pageUrl, array(
'cookie' => array(
'PHPSESSID' => $PHPSESSID
)));
if($download == 1) {
return new PdfResponse($output, $file_name);
} else {
return new Response(
$output, 200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('filename="%s"', $file_name)
)
);
}