因为我是api接口,同时还是两个顶级域名。所以session问题无法解决,我就将他们存储到redis中去了,在文件/vendor/mews/captcha/src/Captcha.php中改动。
方法create中加入
public function create(string $config = 'default', bool $api = false)
{
$this->backgrounds = $this->files->files(__DIR__ . '/../assets/backgrounds');
$this->fonts = $this->files->files($this->fontsDirectory);
if (version_compare(app()->version(), '5.5.0', '>=')) {
$this->fonts = array_map(function ($file) {
/* @var File $file */
return $file->getPathName();
}, $this->fonts);
}
$this->fonts = array_values($this->fonts); //reset fonts array index
$this->configure($config);
$generator = $this->generate();
$this->text = $generator['value'];
$this->canvas = $this->imageManager->canvas(
$this->width,
$this->height,
$this->bgColor
);
if ($this->bgImage) {
$this->image = $this->imageManager->make($this->background())->resize(
$this->width,
$this->height
);
$this->canvas->insert($this->image);
} else {
$this->image = $this->canvas;
}
if ($this->contrast != 0) {
$this->image->contrast($this->contrast);
}
$this->text();
$this->lines();
if ($this->sharpen) {
$this->image->sharpen($this->sharpen);
}
if ($this->invert) {
$this->image->invert();
}
if ($this->blur) {
$this->image->blur($this->blur);
}
if ($api) {
Cache::put('captcha_record_' . $generator['key'], $generator['value'], $this->expire);
}
//存储到redis中
Redis::setex($generator['key'],600,implode('',$generator['value']));
return $api ? [
'sensitive' => $generator['sensitive'],
'key' => $generator['key'],
'img' => $this->image->encode('data-url')->encoded
] : $this->image->response('png', $this->quality);
}
比对验证码时,直接操作redis? 就好了。但是不是很严谨,因为我们平台用户少,以后再说。原谅我。
laravel中,session存在跨域问题,可使用中间件解决
在app/Http/Kernel.php的中间件中加入以下代码
protected $middleware = [
? ? \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
? ? \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
? ? \App\Http\Middleware\TrimStrings::class,
? ? \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
? ? \App\Http\Middleware\TrustProxies::class,
? ? \Illuminate\Cookie\Middleware\EncryptCookies::class,
? ? \Illuminate\Session\Middleware\StartSession::class,
];
重要的2条:
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
|