auth 高可擴展的認證插件

dev-2.0.1-bk
版本
2023-05-30
版本更新時間
5904
安裝
13
star
為了后期更好的維護,最新 2.0 版本已修改包名,請注意切換
webman-tech/auth
Auth plugin for webman
簡介
提供高可擴展的認證授權(quán)插件,包含以下功能:
- [x] 多用戶認證: AuthManager 管理多 guard 實例
- [x] 多認證方式
- [x] SessionMethod: session 認證
- [x] RequestMethod: 請求參數(shù)認證,token 放在 query 或 post 中
- [x] HttpHeaderMethod: 請求 Header 認證,token 放在 header 中
- [x] HttpAuthorizationMethod: 請求 Header 中的 Authorization 認證
- [x] HttpBasicMethod: 請求 Basic 認證
- [x] HttpBearerMethod: 請求 Bearer 認證
- [x] TinywanJwtMethod: 使用 tinywan/jwt 進行 jwt 認證
- [x] CompositeMethod: 組合以上多種認證方式
- [x] 多認證失敗處理器
- [x] RedirectHandler: 重定向處理器
- [x] ResponseHandler: 響應(yīng) 401 http status
- [x] ThrowExceptionHandler: 拋出 UnauthorizedException 異常
安裝
composer require webman-tech/auth
配置
詳見: auth.php
使用
認證授權(quán)方法
use WebmanTech\Auth\Auth;
$guard = Auth::guard(); // 獲取默認的 guard
$guard = Auth::guard('admin'); // 獲取指定名稱的 guard
$guard->login($user); // 用戶登錄
$guard->logout(); // 用戶退出登錄
$guard->getId(); // 獲取當前登錄用戶的 id
$guard->getUser(); // 獲取當前登錄用戶的實例
$guard->isGuest(); // 判斷當前用戶是否為游客
其他方法詳見: GuardInterface
中間件
- 全局切換 Guard:
WebmanTech\Auth\Middleware\SetAuthGuard
- 認證授權(quán):
WebmanTech\Auth\Middleware\Authentication
擴展
支持擴展以下接口:
- 認證方式接口:
AuthenticationMethodInterface
- 認證失敗處理方式接口:
AuthenticationFailureHandlerInterface
- Guard 接口:
GuardInterface
例子
多種用戶體系(前端用戶api接口,后端用戶session)