Java教程

laravel防止XSS攻击(中间件使用)

本文主要是介绍laravel防止XSS攻击(中间件使用),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.首先,创建中间件:

php artisan make:middleware XSS

2.其次,修改app/Http/Middleware/XSS.php文件

XSS.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class XSS
{
    public function handle(Request $request, Closure$next)
    {
        $input = $request->all();
        array_walk_recursive($input,function(&$input) {
            $input = strip_tags($input);
        });
        $request->merge($input);
        return $next($request);
    }
}

3.然后,必须在app/Http/Kernel.php文件中注册中间件:

Kernel.php
class Kernel extends HttpKernel
{
    ....
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        ....
        'XSS' => \App\Http\Middleware\XSS::class,
    ];
}

4.最后,在路由中增加**‘middleware’ => [‘XSS’]**:

Route::group(['namespace'=>'Home','middleware' => ['XSS']],function(){  Route::post('customValiPost','CustomValDemoController@customValiPost');
});

lhttps://blog.csdn.net/huangfenhu/article/details/88550751

 

 

laravel8 防止XSS攻击 预防处理方案从及防sql注入 | Laravel China 社区 (learnku.com)

这篇关于laravel防止XSS攻击(中间件使用)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!