Laravel - 在 Middleware Auth 上包含资产

Laravel - Include assets on Middleware Auth

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

我的管理路线:

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 错误...

我该怎么办?

我想您使用它是因为您不希望未经身份验证的用户知道这些 css/js 文件的内容。 您的 css/js 文件中不应该有任何敏感信息,因此提供它们没有问题。

否则,如果您想限制对文件的访问,您应该通过 PHP 下载文件。例如,您可以将文件放在 public 文件夹之外,并通过获取文件内容并提供下载服务的方法使其有条件地可下载。

不过您应该可以创建 public 管理文件夹,检查文件权限和文件所有权。

更新: 现在我们将使用 storage 而不是 public目录。

尽管我同意您的 css/js 文件中不应包含任何敏感信息,但如果您真的想将这些文件提供给经过身份验证的用户,您可以通过此变通办法做到这一点。

注意: 我已经使项目 public 可以在 git 上使用,因此您可以根据需要从那里克隆。 Git Repo

  1. 使用权限 755
  2. 为管理资产创建一个目录
  3. 创建一个辅助函数来为管理资产提供服务。
  4. 使 blade 中的辅助功能可用。
  5. Link 使用辅助函数的资产,以便首先验证然后提供文件。

基本思路:

  • 基本的想法是有一个目录,没有人可以通过 浏览器。
  • 验证用户
  • 从受保护目录复制文件。
  • 将文件粘贴到仅与经过身份验证的用户关联的新目录(存储)。
  • 在用户注销时删除关联的目录。

实施:

  1. public 目录中创建了一个名为 admin_assets 的目录。
  2. 更改目录权限为755
  3. 创建了一个名为 CommonHelper 的助手 class,并将函数写入 servedelete 管理资产。
  4. 使用以下辅助函数为资产提供服务:

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

  1. 注销时删除了文件。

最后,只要用户登录,文件将可用于 him/her,一旦用户注销,所有文件将从文件夹中删除.

CommonHelper class:

<?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);
  }
}
 ?>

备注:

  1. Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory. Docs

  2. Before deleting a directory you should empty it first.

这是您可以应用的示例。

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

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

在您看来,您可以注入 Admin Assets Like。

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

对于你的 css 你可以

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

希望对您有所帮助

要将 auth 应用到您需要从 laravel 遍历的资产文件,而不是通过使用 完整路径 访问文件,您需要通过路由访问 css/js 文件,以便 laravel 能够对路由组内的每个文件应用身份验证。

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

已更新路由组

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');
});

控制器

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

希望对您有所帮助

对于 blade 模板,您可以使用 @auth 指令。

当您将这些文件包含在 blade 模板中时,您可以将脚本标记包装在 @auth 指令中,它们只会在用户通过身份验证时呈现。

例如

@auth

<script src="{{ asset('path/to/your/js') }}"></script>

@endauth

还有一个可选参数,允许您设置要检查的守卫,例如@auth('your-guard').

它在 Laravel 5.5+

中可用

Here's the documentation for a better overview of its use.