无法修改request的post、get数据,求其它解决办法

邹意

问题描述

前端post过来的数据比如说有n个值,但都被加密最后变成一个值:encrypt_data=xxx,后端中间件需解密encrypt_data后把值重新装进post里面,但webman不支持修改post的数据,以前使用thinkphp是支持的,目前想到的办法:

1、把解密的值复制给 request()->xxx = xxx ,控制器或模型中就可以调用了;感觉不是很科学,不够完美,控制器模型里面获取值的地方都要改掉
2、直接修改request类,或重写;感觉也不完美也麻烦,也担心后续框架升级会有影响问题

原则是不喜欢修改框架本身自带的功能,不知道还有哪些办法

为此你搜索到了哪些方案及不适用的原因

搜索答案中都不支持修改post get数据

720 6 1
6个回答

小W

控制器模型里面获取值的地方都要改掉

无论如何都需要修改,是改动多少问题,可以重新封装 Request类。

  • 暂无评论
ersic

改 support\Request.php 也没事吧,没什么东西

TM

在中间件里面封装一层解密可以吗

  • 暂无评论
ric
<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace support;

/**
 * Class Request
 * @package support
 */
class Request extends \Webman\Http\Request
{
    //重写获取path用于域名路由
    public function path()
    {
        if (!isset($this->_data['path'])) {
            $uri = $this->uri();
            if(config('domain.enable', false)){
                //如果开启了域名路由
                $bind = config('domain.bind', []);
                $domain = $this->host(true);
                if(isset($bind[$domain])) {
                    $uri = '/' . $bind[$domain] . $uri;
                }
            }
            $this->_data['path'] = (string)\parse_url($uri, PHP_URL_PATH);
        }
        return $this->_data['path'];
    }

    //添加setRoute方法,用于字段加解密后覆盖post的方法
    public function setRoute(array $data):void{
        $this->_data['post'] = $data;
    }
}
  • 暂无评论
软饭工程师

继承\Webman\Http\Request,对你需要的方法进行重写,比我我发现,我需要每个接口把传过来的参数去除空格,然后我就把post 方法重写了

    /**
     * $_POST.
     * 对该方法进行重写,过滤空格
     * @param string|null $name
     * @param mixed|null $default
     * @return mixed|null
     */
    public function post($name = null, $default = null)
    {
        if (!isset($this->_data['post'])) {
            $this->parsePost();
        }
        if (null === $name) {
            $postData = [];
            foreach ($this->_data['post'] as $item => $value) {
                if (!is_array($value)) $value = trim($value);
                $postData[$item] = $value;
            }
            return $postData;
        }
        return isset($this->_data['post'][$name]) ? trim($this->_data['post'][$name]) : $default;
    }

你可以对他进行修改,增加其他的操作

  • 暂无评论
artisan

$currentBuffer = $request->rawBuffer();

POST /xx/xx HTTP/1.1
X-Real-IP: 127.0.0.1
Host: xxx.xx
X-Forwarded-Proto: http
Content-Length: 378
User-Agent: Apifox/1.0.0 (https://apifox.com)
Accept: */*
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary=--------------------------572433850403036298600651

----------------------------572433850403036298600651
Content-Disposition: form-data; name="name"

jhon
----------------------------572433850403036298600651
Content-Disposition: form-data; name="num"

100
----------------------------572433850403036298600651
Content-Disposition: form-data; name="code"

120.567
----------------------------572433850403036298600651--

//修改$currentBuffer
$newBuffer = processFunctiontName($currentBuffer);
$newRequest = new Request($newBuffer);

  • 暂无评论
🔝