hyperf 和 laravel、lumen防止xss攻击中间件封装

发布时间:2024年01月22日

hyperf 防止xss攻击自定义中间件封装

<?php

declare(strict_types=1);

namespace App\Middleware;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class XSSProtectionMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $input = $request->all();

        array_walk_recursive($input, function(&$input) {
            $input = (strip_tags(htmlspecialchars(htmlentities($input, ENT_QUOTES, 'UTF-8'))));
        });

        $request = $request->withQueryParams($input);

        return $handler->handle($request);
    }
}

laravel、lumen防止xss攻击自定义中间件封装

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class XSSProtectionMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $input = $request->all();

        array_walk_recursive($input, function(&$input) {
            $input = (strip_tags(htmlspecialchars(htmlentities($input, ENT_QUOTES, 'UTF-8'))));
        });

        $request->merge($input);

        return $next($request);
    }
}
文章来源:https://blog.csdn.net/t_fengyun/article/details/135741862
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。