场景
- laravel/passport Consuming Your API With JavaScript 使用laravel/passport 访问自身的接口的时候 十分简单, 只是需要在 web middleware graoup 添加
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class
但是laravel5.5版本却有个bug ,导致axios访问会一直返回Unauthenticated 500
- 环境重现
- php7.2
- laravel5.5
- nginx
- ubuntu16.04
- “laravel/passport”: “~4.0”
- 进行的laravel/passport的配置
- config/auth.php api .driver= ‘passport’
- app/Providers/AuthServiceProvider Passport::routes();
- axios 默认配置
- app/Http/Kernel.php web group middlware add
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
Typically, if you want to consume your API from your JavaScript application, you would need to manually send an access token to the application and pass it with each request to your application. However, Passport includes a middleware that can handle this for you. All you need to do is add the CreateFreshApiToken middleware to your web middleware group:
分析
- 尝试过laravel5.7 完全没有问题, 所以可能是版本问题
- 有问题当然github了
解决
- 找到了两种解决方案
- App\Http\Middleware\EncryptCookies 增加属性 protected static $serialize = true;
- [增加一个判断的方法] (https:///laravel/passport/issues/805)
在修改过后 可能会遇到unserialize(): Error at offset 0 of 40 bytes
- 清除浏览器的cookie就可以了