laravel 流明认证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35667780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lumen Authentication
提问by Rob
Just can't get the Lumen authentication to work at all.
根本无法让 Lumen 身份验证正常工作。
I have a fresh install and trying to follow the docs here:
我进行了全新安装并尝试按照此处的文档进行操作:
https://lumen.laravel.com/docs/5.2/authentication
https://lumen.laravel.com/docs/5.2/authentication
I've Uncommented the AuthProvider
line in the app.php
file (along with everything else, facade, etc). Then in a simple controller I just do dd(Auth::use())
.
我已取消注释文件中的AuthProvider
行app.php
(以及其他所有内容、外观等)。然后在一个简单的控制器中,我只做dd(Auth::use())
.
I just can't get around this error:
我就是无法解决这个错误:
Undefined index: provider
in AuthManager.php line 152
at Application->Laravel\Lumen\Concerns\{closure}('8', 'Undefined index: provider', '/home/vagrant/Code/gryd/api.gryd.com/vendor/illuminate/auth/AuthManager.php', '152', array('name' => 'api', 'config' => array('driver' => 'token'))) in AuthManager.php line 152
Any ideas?
有任何想法吗?
EDIT:
编辑:
Since someone asked for a code sample.
由于有人要求提供代码示例。
- Install Lumen
- Uncomment everything in app.php
Put this in routes:
$app->get('/api/v1/users/{id}', function () { dd(\Auth::user()); });
- 安装流明
- 取消注释 app.php 中的所有内容
把它放在路由中:
$app->get('/api/v1/users/{id}', function () { dd(\Auth::user()); });
采纳答案by Rob
Well I still haven't found out how to change the api request type via .env
. But for now switching it to token
seems to work.
好吧,我仍然没有找到如何通过.env
. 但是现在将其切换为token
似乎有效。
Changed Auth::viaRequest('api', functi
to Auth::viaRequest('token', funct
.
改Auth::viaRequest('api', functi
到Auth::viaRequest('token', funct
。
回答by Shaji Ahmed
This is what I've got so far, which is working but not quite how I'd like it. The following works for Token-based auth, which is the default setting in Lumen.
到目前为止,这就是我所拥有的,它正在工作,但不是我想要的。以下适用于基于令牌的身份验证,这是 Lumen 中的默认设置。
Enable Authentication
启用身份验证
Register routeMiddleware
and AuthServiceProvider
by un-commenting the following lines in bootstrap/app.php
.
注册routeMiddleware
并AuthServiceProvider
取消注释bootstrap/app.php
.
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
and
和
$app->register(App\Providers\AuthServiceProvider::class);
Configuration
配置
Copy vendor/laravel/lumen-framework/config/auth.php
to config/auth.php
. Create the root config
folder if you have to.
复制vendor/laravel/lumen-framework/config/auth.php
到config/auth.php
. config
如果需要,请创建根文件夹。
Inside we will find four items (defaults
, guards
, providers
, passwords
). We're concerned with the first three.
在里面,我们会发现四个项目 ( defaults
, guards
, providers
, passwords
)。我们关心前三个。
First we name the default guardas ABC.
首先,我们将默认守卫命名为 ABC。
'defaults' => [
'guard' => env('AUTH_GUARD', 'ABC'),
],
Next we define the ABC guard with token
as its driverand XYZ
as its provider.
接下来我们定义与ABC后卫token
作为其驱动程序,并XYZ
作为其供应商。
'guards' => [
'ABC' => [
'driver' => 'token',
'provider' => 'XYZ'
],
],
And the XYZ provider is defined with eloquent
as the driverand App\User::class
as the model.
和XYZ提供商与定义eloquent
为驾驶员和App\User::class
作为模型。
'providers' => [
'XYZ' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
Completing Setup
完成设置
Finally, we use the auth
middleware in our routing setup, as usual.
最后,我们auth
像往常一样在路由设置中使用中间件。
$app->group(['middleware' => 'auth'], function () use ($app) {
So this is what gets the token auth up and running. It uses the api_token
field in the users table to authenticate, which can be found in TokenGuard
.
所以这就是启动和运行令牌身份验证的原因。它使用api_token
用户表中的字段进行身份验证,可以在TokenGuard
.
I still haven't found out what effect AuthServiceProvider
and $this->app['auth']->viaRequest('api', function ($request) {
have on my app yet.
我还没有发现什么样的影响AuthServiceProvider
和$this->app['auth']->viaRequest('api', function ($request) {
对我的应用程序呢。