如何解决laravel上的“白名单” IP出现问题也许我对中间件的路由有疑问,或者我对解决方案的方法有误?
我在编写中间件路由时遇到麻烦。那里什么都没有。如果使用其他IP,我仍然可以看到页面 我的任务-白名单。当我访问无法访问IP的资源时,它将在另一个页面上路由。 我使用中间件
<?php
namespace App\Http\Middleware;
use Closure;
class FilterIps
{
const ALLOWED = [
'1.1.1.1',];
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request,Closure $next)
{
abort_unless(in_array($request->ip(),self::ALLOWED),403);
return $next($request);
}
}
添加到内核文件:
protected $middlewareGroups = [
'web' => [
FilterIps::class,],];
我在网络上的路线。也许他不正确?还是我的任务解决方法不正确?
Route::group(['middleware' => 'web'],function () {
Route::get('/demo/loginjs',function () {
return view('auth.login');
});
});
解决方法
如果您想这样使用它
Route::group(['middleware' => 'web'],function () {
您需要将中间件添加到protected $routeMiddleware
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,//...
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,'filter' => \App\Http\Middleware\FilterIps::class,];
然后您就可以使用它
Route::group(['middleware' => 'filter'],function () {
Route::get('/demo/loginjs',function () {
return view('auth.login');
});
});
如果您希望它出现在web.php
中的所有路由上,请将其添加到网络组中而不用在web.php
中调用它,它将应用于它们。
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,//...
\Illuminate\Routing\Middleware\SubstituteBindings::class,\App\Http\Middleware\FilterIps::class,],'api' => [
'throttle:60,1',\Illuminate\Routing\Middleware\SubstituteBindings::class,];
如果您想知道为什么不需要将其添加到web.php
文件中,则答案在\App\Providers\RouteServiceProvider::class
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace('App\Http\Controllers')
->group(base_path('routes/web.php'));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。