您好,登錄后才能下訂單哦!
場(chǎng)景:手頭的項(xiàng)目需要對(duì)外提供API服務(wù),需要對(duì)請(qǐng)求數(shù)據(jù)進(jìn)行簽名校驗(yàn)。提交接口的數(shù)據(jù)會(huì)有''空字符值存在,laravel的Illuminate\Foundation\Http\Request接收到參數(shù)后,會(huì)將空字符強(qiáng)制轉(zhuǎn)換為NULL,導(dǎo)致簽名校驗(yàn)通不過(guò)。
經(jīng)過(guò)一番努力,找到幕后黑手。
Illuminate/Foundation/Http組件中有定義這個(gè)中間件。
vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php
<?php
namespace Illuminate\Foundation\Http\Middleware;
class ConvertEmptyStringsToNull extends TransformsRequest
{
/**
* Transform the given value.
*
* @param string $key
* @param mixed $value
* @return mixed
*/
protected function transform($key, $value)
{
return is_string($value) && $value === '' ? null : $value;
}
}
解決方法:
既然是中間件作祟,肯定在內(nèi)核中有注冊(cè)。找到內(nèi)核注釋該中間件就好。
app/Http/Kernel.php
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
//\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,//會(huì)將''轉(zhuǎn)化為null
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Session\Middleware\StartSession::class,
];
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。