尝试在 Lumen 上创建我自己的身份验证时出现“非法偏移类型”

     2023-03-14     88

关键词:

【中文标题】尝试在 Lumen 上创建我自己的身份验证时出现“非法偏移类型”【英文标题】:'Illegal offset type' while trying to create my own Authentication on Lumen 【发布时间】:2021-02-21 09:16:56 【问题描述】:

我正在尝试基于外部 Oauth2 提供程序为我的 API 创建自己的身份验证。我使用 Lumen 作为我的 API 后端,我必须为没有有效访问令牌的用户保护我的一些端点。

所以我按照docs 中的内容开始。我在bootstrap/app.php 中取消了$app->register(App\Providers\AuthServiceProvider::class); 的注释

我在AuthServiceProvider中创建了特定的登录:

class AuthServiceProvider extends ServiceProvider

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    
        //
    

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
    public function boot()
    
        // Here you may define how you wish users to be authenticated for your Lumen
        // application. The callback which receives the incoming request instance
        // should return either a User instance or null. You're free to obtain
        // the User instance via an API token or any other method necessary.

        $this->app['auth']->viaRequest('api', function ($request) 
            // my logic
        );
    

我还在routes/web/php 中保护了我的路线:

$router->group(['prefix' => 'api', 'middleware' => 'auth'], function () use ($router)
    $router->get('grant-access', ['uses' => 'DatabaseController@grantAccess']);
    $router->get('refresh-access', ['uses' => 'DatabaseController@refreshAccess']);
);

我调查过AuthManager 中有一个方法guard() 会导致问题:

 /**
 * Attempt to get the guard from the local cache.
 *
 * @param  string|null  $name
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
 */
public function guard($name = null)

    $name = $name ?: $this->getDefaultDriver();
    return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);

因为变量$name是一个对象,这个对象不能用作PHP数组中的键。

所以我的问题是我错过了在 Lumen 中打开身份验证的一些东西吗?

【问题讨论】:

【参考方案1】:

好吧,我花了几分钟,但我发现我必须在 bootstrap/app.php 中取消注释:

 $app->routeMiddleware([
     'auth' => App\Http\Middleware\Authenticate::class,
 ]);

我认为 Lumen docs 中那个红色注释旁边也应该提到这一点。

【讨论】:

你发现了吗?就在the page you linked

当我尝试实现身份验证时出现 Firebase.app() 错误

】当我尝试实现身份验证时出现Firebase.app()错误【英文标题】:Firebase.app()errorwhenitrytoimplementauth【发布时间】:2021-02-0617:34:09【问题描述】:我正在尝试将数据库连接到我的App.js,它可以工作,但是当我尝试进行身份验证时,会... 查看详情

尝试在 Eve Flask 框架中验证用户时出现 401

】尝试在EveFlask框架中验证用户时出现401【英文标题】:401whentryingtoauthenticateuserinEveflaskframework【发布时间】:2015-02-1413:13:40【问题描述】:我正在使用很棒的EveREST-framework创建一个带有JWT身份验证的CRUDAPI。我查看了教程postedhere... 查看详情

尝试使用 Firebase 身份验证时出现错误

】尝试使用Firebase身份验证时出现错误【英文标题】:IgetanErrorwhenItrytouseFirebaseAuth【发布时间】:2021-05-2400:14:22【问题描述】:我第一次构建包含firebase(firebaseauth)的网站。在浏览器中运行HTML代码时出现错误。未捕获的类型错误... 查看详情

Azure 应用服务身份验证 - 尝试获取 /.auth/me 时出现 302

】Azure应用服务身份验证-尝试获取/.auth/me时出现302【英文标题】:AzureAppServiceAuthentication-302whentryingtoGET/.auth/me【发布时间】:2018-11-1420:06:07【问题描述】:我有一个使用应用服务身份验证托管在Azure应用服务上的reactjsWeb应用。我... 查看详情

获取用户数据时出现未经身份验证的错误

...题描述】:我使用Laravel作为端点,使用Angular作为前端来创建一个简单的应用程序,这个问题在我登录后显示我试图获得当前的身份验证每次显示此消息“未经身份验证”时的用户数据AuthController.phpclassAuthControlle 查看详情

创建 mongo debezium 连接器时出现身份验证错误

...nector【发布时间】:2020-07-1004:01:09【问题描述】:我正在尝试在下面创建mongodebezium连接器,但出现身份验证错误。请在下面找到详细信息发布http://localhost:8083/connectors有效载荷:"name":"mongo 查看详情

在 AWS EKS 上使用 Kubernetes 身份验证方法部署 Hashicorp Vault 时出现证书错误

...AWSEKS【发布时间】:2019-05-0820:40:28【问题描述】:我正在尝试在AWSEKS上使用KubernetesAuthMethod部署HashicorpVault。Hash 查看详情

在 Lumen 中创建多个身份验证提供程序

...nLumen【发布时间】:2018-01-1918:06:10【问题描述】:我正在尝试为我的API创建多个AuthGuard和服务。我希望特定用户可以使用特定组(更像是没有会话的RBAC)。如果用户尝试访问以admin:auth作为中间件的组,则会检查其权限。如果它... 查看详情

UnauthorizedError:身份验证时出现 jwt 格式错误的错误

...Auth0对我的react应用程序的用户进行身份验证。我一直在尝试访问服务器端的资源,但我不断收到UnauthorizedError:jwtmalformedasanErro 查看详情

尝试调用 AIA 时,Keycloak“处理对身份提供者的身份验证请求时出现意外错误”

】尝试调用AIA时,Keycloak“处理对身份提供者的身份验证请求时出现意外错误”【英文标题】:Keycloak"Unexpectederrorwhenhandlingauthenticationrequesttoidentityprovider"whentryingtoinvokeAIA【发布时间】:2022-01-0623:49:11【问题描述】:在其... 查看详情

当我尝试将我的应用程序连接到 Firebase 身份验证时出现 MissingPluginException 异常

】当我尝试将我的应用程序连接到Firebase身份验证时出现MissingPluginException异常【英文标题】:MissingPluginExceptionexceptionwhenItrytoconnectmyapptofirebaseauthentication【发布时间】:2021-02-0811:22:05【问题描述】:当我尝试使用我的颤振项目初... 查看详情

如何在带警卫的流明中实现多重身份验证?

...实现了身份验证,并且效果非常好。现在的问题是,当我尝试使用authguard中间件实现多个身份验证时,我不知道如何实现它。到目 查看详情

尝试使用 Cognito 向 Battle.net OIDC 进行身份验证时出现错误的 id_token 颁发者

】尝试使用Cognito向Battle.netOIDC进行身份验证时出现错误的id_token颁发者【英文标题】:Badid_tokenissuerwhentryingtoauthenticatewithBattle.netOIDCwithCognito【发布时间】:2020-01-2707:05:11【问题描述】:过去几天我一直在尝试设置Cognito以使用战... 查看详情

使用 Team Explorer 连接到 Codeplex 时出现代理身份验证错误

...述】:与thisquestion类似,我在Codeplex上有一个项目,当我尝试在VisualStudio2012(在Windows8上)中连接到CodeplexTF 查看详情

尝试在 Web API 请求上检索 Azure Blob 时出现 403 错误

】尝试在WebAPI请求上检索AzureBlob时出现403错误【英文标题】:Getting403errorwhentryingtoretrieveanAzureblobonWebAPIrequest【发布时间】:2017-07-2810:47:59【问题描述】:我正在尝试让经过身份验证的(通过JWT)GET请求返回存储在Azureblob中的图... 查看详情

尝试在 Laravel 中设置身份验证时出现“ReflectionException Function () 不存在”

】尝试在Laravel中设置身份验证时出现“ReflectionExceptionFunction()不存在”【英文标题】:"ReflectionExceptionFunction()doesnotexist"whentryingtosetupauthenticationinLaravel【发布时间】:2021-02-2009:00:23【问题描述】:我在Laravel中让身份验证10... 查看详情

Lumen 中的用户身份验证

...nLumen【发布时间】:2017-09-1114:05:42【问题描述】:我正在尝试在我的Lumen应用程序中启用基本用户身份验证username和password。在app.php文件中,如https://lumen.laravel.com/docs/5.4/authentication中所述,以下内容已取消注释$app->withFacades( 查看详情

创建 Firestore 文档时出现权限错误

...cument【发布时间】:2020-12-1914:21:36【问题描述】:我正在尝试在不需要用户身份验证的情况下安全地创建Firestore文档,并为此定义了以下规则:allowcreate:ifrequest.resource.data.deviceid==resource.id;根据我 查看详情