在 Laravel 中同时使用 api 和 webguard
我希望我的 Laravel 应用程序既可以用作 REST API,也可以用作普通网站。问题是我不能同时进行默认身份验证,并且我需要这两个防护来使我的应用程序正常工作。
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
],
当我在页面上添加两个表单时不起作用
'defaults' => [
'guard' => 'web',
'guard' => 'api',
'passwords' => 'users',
],
I want my laravel application to be used as a rest api but also as a normal website. The problem is that I can't do both as default authentication and i need this two guards that my application work.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
],
When i add both my form on the page does not work
'defaults' => [
'guard' => 'web',
'guard' => 'api',
'passwords' => 'users',
],
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @lagbox 提到的,你可以这样做,
但我总是尽量避免将 Web 防护和 api 防护放在一起。
因为如果所有的守卫都无法获取用户,laravel默认的auth中间件最终会重定向到登录url,所以如果你想让它返回json,你必须重写这个中间件。
尝试像这样更改 App\Http\Middleware\Authenticate 类,它应该可以工作。
As @lagbox mentioned you can do it like this
but I always try to avoid putting the web guards and api guards together.
because the laravel default auth middleware will finally redirect to login url if all the guards failed to get user, so you must rewrite this middleware if you want's it to return json.
try to change the App\Http\Middleware\Authenticate class like this, it should work.