g4t / keycloak
1.0.1
2023-04-27 18:58 UTC
Requires
- php: ^8.0
- firebase/php-jwt: ^6.3
This package is auto-updated.
Last update: 2024-09-16 11:43:26 UTC
README
keycloak-guard-laravel 包提供了 Keycloak 认证服务器与 Laravel 应用程序的集成。它允许您将 Keycloak 作为 Laravel 应用的认证提供者。
安装
composer require g4t/keycloak
现在发布配置文件,运行以下命令
php artisan vendor:publish
并选择 g4t\Keycloak\KeycloakGuardServiceProvider
提供者
配置
要配置此包,您需要将 Keycloak 服务器详细信息添加到您的 Laravel .env 文件中
K_REALM_PUBLIC_KEY= K_LOAD_USER_FROM_DATABASE=true # get user data from database or keycloak K_USER_PROVIDER_CREDENTIAL=username # This setting specifies the unique column name in your user provider table that will be used to retrieve the user's credentials for authentication. K_TOKEN_PRINCIPAL_ATTRIBUTE=preferred_username # This setting specifies the key name for the attribute in the Keycloak token that will be used to check against the unique column specified in K_USER_PROVIDER_CREDENTIAL. The attribute should contain the user's unique identifier, such as a username or email address. K_TOKEN_EXPIRED=false # Enable this when you are sure that you have set the Keycloak server time correctly.
您还需要配置您的 Laravel 应用以使用 keycloak 守护。为此,请将以下内容添加到您的 config/auth.php
文件中
'guards' => [ // Other guards... 'keycloak' => [ 'driver' => 'keycloak', 'provider' => 'users', ], ], 'providers' => [ // Other providers... 'users' => [ 'driver' => 'keycloak', ], ],
用法
安装并配置此包后,您可以使用 keycloak 守护在 Laravel 应用程序中验证用户。要验证用户,您可以使用 Auth::guard('keycloak')->attempt($credentials) 方法,其中 $credentials 是用户凭证数组。
例如
return auth('keycloak')->attempt([ 'url' => 'https://:8080', 'realm' => 'realm-name', 'username' => 'username', 'password' => 1234, 'client_id' => 'client_id', 'client_secret' => 'client_secret', 'grant_type' => 'password', ]);
您还可以使用 auth('keycloak')->check() 方法来检查用户是否已验证
if (auth('keycloak')->check()) { // User is authenticated } else { // User is not authenticated }