You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中已设置的JWT Cookie无法读取问题求助

Laravel中Cookie读取失败问题

在Laravel项目中(含两次全新创建的测试项目),遇到Cookie读取异常:浏览器控制台已确认jwt Cookie存在,但在DashboardController中使用$request->cookie('jwt')无法获取该Cookie,$request->hasCookie('jwt')始终返回false。已尝试多种方法并查阅资料,问题仍未解决。

环境信息

  • Laravel 10.46.0
  • PHP 8.2.7(符合Laravel ^8.1版本要求)
  • Debian 12.5

设置Cookie的控制器(LoginController.php)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller {
    public function login(Request $request) {
        try {
            // Validate input...
            $validatedData = $request->validate([
                'email' => 'required|email|max:100',
                'password' => 'required|string|max:100'
            ]);

            // Check email...
            $client = DB::table('table')->where('email', $validatedData['email'])->first();
            if (is_null($client)) {
                Log::error('Wrong email!');
                return response()->json(['error' => 'Email not found!'], 401);
            }

            // Check password...
            $passwordState = Hash::check($validatedData['password'], $client->password);
            if ($passwordState) {
                $responseData = [
                    'status' => 'OK',
                    'message' => 'Login successful, JWT generated',
                ];
                $response = response()->json($responseData, 200);
                setcookie('jwt', 'qwerty@qwerty.com', 9001);
                return $response;
            } else {
                Log::error('Wrong password');
                return response()->json(['error' => 'Wrong password!'], 401);
            }
        } catch (ValidationException $error) {
            Log::error('Error validating login details!');
            return response()->json(['error' => 'Login details has wrong format!'], 501);
        } catch (Exception $error) {
            Log::error('Unknown error logging in!');
            return response()->json(['error' => 'Error logging in!'], 500);
        }
    }
}

读取Cookie的控制器(DashboardController.php)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cookie;
use App\Models\Users;

class DashboardController extends Controller {
    public function dashboard(Request $request) {
        $token = $request->cookie('jwt');
        Log::debug($token); // 日志输出空白内容
        if ($request->hasCookie('jwt')) {
            Log::debug('Has cookie!');
            $token = $request->cookie('jwt');
            Log::debug($token); // 永远不会执行到此处
            // 使用Cookie数据...
            return view('dashboard');
        } else {
            Log::error('No cookie!'); // 始终执行此分支
        }
    }
}

预期结果

  • $request->cookie('jwt')能成功获取到Cookie值
  • $request->hasCookie('jwt')返回true

实际结果

  • Log::debug($token)在laravel.log中输出空白内容
  • Log::error('No cookie!')始终被执行

进一步测试

  • 使用Cookie::make('jwt', 'qwerty@qwerty.com', 60)方式设置Cookie,问题依旧
  • 使用$cookieValue = Cookie::get('jwt')读取,同样无法获取值
  • 使用$cookie = Cookie::make('jwt', 'qwerty@qwerty.com', 9001); return $response->withCookie($cookie)设置,无效果
  • 修改Cookie名称、更换浏览器均未改善问题

内容的提问来源于stack exchange,提问作者cman99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:15:17