首页 文章

Laravel - 在Middleware Auth上包含资产

提问于
浏览
6

在我的应用程序内部,存在一个名为 admin 的路由组,该组内的任何路由都会调用两个资源: public/css/admin.csspublic/js/admin.js ,但任何未经身份验证的用户都可以访问这些文件 . 如何在Auth Middleware中包含这些文件?

我的管理路线:

Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function () {
    Route::get('/', 'Admin\IndexController@index')->name('panel');

    Route::group(['prefix' => 'users'], function() {});

    Route::group(['prefix' => 'settings'], function() {});

    Route::fallback('Admin\ExceptionController@exception');
});

我的资源链接:

http://localhost:3000/css/admin.css
http://localhost:3000/js/admin.js

我的资源链接应该是:

http://localhost:3000/admin/css/admin.css
http://localhost:3000/admin/js/admin.js

如果我只是在 public 文件夹中创建文件夹 admin ,我刚收到403错误...

我能做些什么呢?

4 回答

  • 5

    Update: 现在我们将使用 storage 而不是 public 目录 .

    虽然我同意您不应该在css / js文件中包含任何敏感信息,但如果您确实要将文件提供给经过身份验证的用户,则可以使用此方法来完成此操作 .

    NOTE: 我已经在git上公开了这个项目,所以你可以根据需要从那里克隆 . Git Repo

    • 使用权限 755 为管理资产创建目录

    • 创建一个辅助函数来提供管理资产 .

    • 使刀片中的辅助功能可用 .

    • 使用帮助程序功能链接资产,以便首先进行身份验证,然后提供文件 .

    Basic Idea

    • 基本思路是拥有一个无人可以通过浏览器访问的目录 .

    • 验证用户

    • 从受保护目录复制文件 .

    • 将文件粘贴到仅与经过身份验证的用户关联的新目录( in storage )中 .

    • 删除用户注销时的关联目录 .

    Implementation

    • public 目录中创建了一个名为 admin_assets 的目录 .

    • 将目录的权限更改为 755 .

    • 创建了一个名为 CommonHelper 的辅助类,并将函数写入 servedelete admin资产 .

    • 使用以下辅助函数为资产提供服务:

    <link href="{{ asset( CommonHelper::serveAdminAssets('app.css', '/css/') ) }}" rel="stylesheet">

    • 在注销时删除了文件 .

    Finally ,就用户登录而言,他/她可以使用文件,一旦用户退出,所有文件都将从文件夹中删除 .

    CommonHelper 课程:

    <?php
    /**
     *
     */
    use Illuminate\Support\Facades\File;
    use Illuminate\Support\Facades\Storage;
    
    class CommonHelper {
      public static function serveAdminAssets($fileName, $filePath) {
    
        if( Auth::check() ) {
          $adminAssetsBasePath = public_path().'/admin_assets';
    
          $source = $adminAssetsBasePath.$filePath.$fileName;
    
          $destDir = 'public/'.Auth::user()->id.$filePath;
    
          $dest = $destDir.$fileName;
    
          Storage::put($dest, file_get_contents($source));
    
          return Storage::url($dest);
        } else {
          return '';
        }
      }
    
      public static function removeAdminAssets($id) {
    
          $destDir = storage_path('app/public/'.Auth::user()->id);
          File::cleanDirectory($destDir);
          File::deleteDirectory($destDir);
      }
    }
     ?>
    

    Notes:

    • 请记住,如果您使用的是本地驱动程序,则应该可以公开访问的所有文件都应放在storage / app / public目录中 . 此外,您应该在public / storage处创建一个指向storage / app / public目录的符号链接 . 文件

    • 在删除目录之前,您应该先清空它 .

  • 2

    我想你正在使用它,因为你不希望未经身份验证的用户知道这些css / js文件的内容 . 您的css / js文件中不应包含任何敏感信息,因此服务它们没有问题 .

    否则,如果要限制对文件的访问,则应通过PHP下载文件 . 例如,您可以将文件放在公共文件夹之外,并通过获取文件内容并供下载的方法使其可以条件下载 .

    您应该可以创建该公共管理文件夹,检查文件权限和文件所有权 .

  • 1

    以下是您可以申请的示例 .

    将您的资产存储在 storage 目录中 .

    然后你可以检查它是否是管理员 .

    在您的视图中,您可以注入Admin Assets Like .

    <script>
      {!! \Storage::disk('urdisk name')->get('admin.js'); !!}
    </script>
    

    为你的CSS你可以

    <style>
      {!! \Storage::disk('urdisk name')->get('admin.css'); !!}
    </style>
    

    希望这可以帮助

  • 0

    要将 auth 应用于需要从laravel进行的资产文件,而不是通过使用 full path 访问文件,您需要通过路由访问css / js文件,以便laravel能够对路由组内的每个文件应用auth .

    P.S 文件必须保存在 storage 文件夹中,i-e storage/admin.css

    Updated route group

    Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function () {
    
    Route::get('/', 'Admin\IndexController@index')->name('panel');
    
    Route::get('{file}', 'StaticFileController@serveFile');
    
    Route::group(['prefix' => 'users'], function() {});
    
    Route::group(['prefix' => 'settings'], function() {});
    
    Route::fallback('Admin\ExceptionController@exception');
    });
    

    Controller

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    Use Response;
    
    use App\Http\Requests;
    
    class StaticFileController extends Controller
    {
        public function serveFile ($file)
        {
    
            $storagePath = storage_path($file);
            $mimeType = mime_content_type($storagePath);
            if( ! \File::exists($storagePath)){
                return view('errorpages.404');
            }
            $headers = array(
                'Content-Type' => $mimeType,
                'Content-Disposition' => 'inline; filename="'.$file.'"'
            );
            return Response::make(file_get_contents($storagePath), 200, $headers);
    
        }
    }
    

    现在您的资源链接将是

    http://localhost:3000/admin/css/admin.css
    http://localhost:3000/admin/js/admin.js
    

    希望这可以帮助

相关问题