如何在所有查看模板中访问CSP的自定义$ nonce? 我正在构建一个Laravel 11应用程序,并为该项目使用自定义CSP中间件。我已经在应用程序/http/middleware文件夹中创建了contentsecuritypolicy.php,它包含此代码: ` 名称空间应用\ http \

问题描述 投票:0回答:1

我在Bootstrap/app.php中注册了中间件(laravel 11中的kernel.php的替代方案)这样: `
 use Illuminate\Foundation\Application;
 use Illuminate\Foundation\Configuration\Exceptions;
 use Illuminate\Foundation\Configuration\Middleware;
 use App\Http\Middleware\ContentSecurityPolicy;

 return Application::configure(basePath: dirname(__DIR__))
->withRouting(
    web: __DIR__ . '/../routes/web.php',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
    $middleware->prepend(ContentSecurityPolicy::class);
})
->withExceptions(function (Exceptions $exceptions) {
    //
})->create();

但如果在内联脚本中使用$ nonce:

<script nonce="{{$nonce}}"></script>

我得到此错误:“未定义的变量$ nonce”。
我该如何解决?
    
okay 我找到了解决方案。 我要做的就是移动这条线

view()->share("nonce",$nonce);
php laravel-11
1个回答
0
投票

$response = $next($request);

所有的一切都会如预期的。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.