白名单 IP 地址 Laravel 11.3.0

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

我正在尝试使用 laravel 11.3.0 阻止某些 IP 地址访问我的网站,我遇到的所有解决方案都建议您在 kernel.php 中注册中间件,但我使用的 laravel 版本没有内核.php。那我该怎么办呢?

我尝试在app.php中注册它

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            \App\Http\Middleware\HandleInertiaRequests::class,
            \Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
        ]);
        $middleware->alias([
            'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
            'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
            'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
        ]);

         
        
        return $middleware;
    })

    ->withMiddleware([
        \App\Http\Middleware\BlockIpMiddleware::class,
    ])
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })
    ->create();

错误 PHP 致命错误:未捕获类型错误:Illuminate\Foundation\Configuration\ApplicationBuilder::withMiddleware():参数 #1 ($callback) 必须是类型?可调用,给定数组,在 C:\xampp\htdocs\swamsite2\Swarmsite\ 中调用Daniel elegram ootstrap pp.php 第 29 行,并在 C:\xampp\htdocs\swamsite2\Swarmsite\Daniel elegram endor\laravel ramework\src\Illuminate\Foundation\Configuration\ApplicationBuilder.php:227 中定义 堆栈跟踪: #0 C:\xampp\htdocs\swamsite2\Swarmsite\Daniel elegram ootstrap pp.php(29): Illuminate\Foundation\Configuration\ApplicationBuilder->withMiddleware(Array) #1 C:\xampp\htdocs\swamsite2\Swarmsite\Daniel elegram rtisan(12): require_once('C:\xampp\htdocs...') #2 {主要} 扔在 C:\xampp\htdocs\swamsite2\Swarmsite\Daniel elegram endor\laravel ramework\src\Illuminate\Foundation\Configuration\ApplicationBuilder.php 第 227 行

php whitelist laravel-11
1个回答
0
投票

Laravel 11 提供了 apiweb 中间件组的分离。 对于我的用例,我所做的与此类似:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(prepend: [
            \App\Http\Middleware\EncryptCookies::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \App\Http\Middleware\BlockIpMiddleware::class,
        ]);

        $middleware->api(append: [
            \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            \Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\BlockIpMiddleware::class,
        ]);
    })->create();

© www.soinside.com 2019 - 2024. All rights reserved.