![](/img/trans.png)
[英]Check whether a user is authenticated inside an ExceptionController
[英]How do I check whether a user is authenticated before accessing/viewing a file from the filesystem public disk?
我正在使用laravel文件系統來存儲和檢索文件。 public/storage
與storage/app/public
符號鏈接。 在storage/app/public
目錄下,我有兩個文件夾,其中包含各自的文件(以便可以在瀏覽器中訪問它們):
當用戶嘗試從protected-files
目錄訪問文件時,我想檢查該用戶是否已通過身份驗證。 我們能做到嗎?
我按照鏈接中的答案進行操作,但完全沒有效果。
web.php
Route::get('storage/protected-files/{file}', function() {
//logic to check whether the user is authenticated
})->where(['file' => '.*']);
在訪問文件之前,您可以簡單地檢查是否使用此代碼對用戶進行了身份驗證。
Auth::check():
storage/protected-files
路徑是指服務器上文件的位置,但實際上,公眾可以通過http://example.com/file-name.pdf
訪問此位置。 laravel中的路由旨在處理URL。 所以應該嘗試像:
Route::get('/{file}', function() {
//check whether the user is authenticated
if (!auth()->user()) { // or may use Auth::check();
return 'unauthorized';
}
})->where(['file' => '.*']);
如果文件在/ public目錄中,則Web服務器將返回該文件。該請求甚至不會發送到laravel。
因此,您必須將文件從/storage/app/public
目錄移動到/storage/app/xxxx
目錄。 並執行相同的身份驗證。
您可以使用寫入存儲目錄/應用程序目錄的“本地”磁盤,因此在寫入文件時必須指定目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.