https://github.com/vartruexuan/hyperf-http-auth
hyperf框架http登录授权组件
https://github.com/vartruexuan/hyperf-http-auth
auth http-auth hyperf
Last synced: 10 months ago
JSON representation
hyperf框架http登录授权组件
- Host: GitHub
- URL: https://github.com/vartruexuan/hyperf-http-auth
- Owner: vartruexuan
- License: mit
- Created: 2021-04-06T07:32:20.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-10-27T12:33:30.000Z (over 3 years ago)
- Last Synced: 2024-04-28T16:21:57.185Z (almost 2 years ago)
- Topics: auth, http-auth, hyperf
- Language: PHP
- Homepage:
- Size: 92.8 KB
- Stars: 17
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hyperf-http-auth
### 安装
```bash
composer require vartruexuan/hyperf-http-auth
```
### 发布配置文件
```bash
php bin/hyperf.php vendor:publish vartruexuan/hyperf-http-auth
```
### 创建 User model 文件 并且实现接口 IdentityInterface
```php
find($id);
}
/**
*
* @return mixed
*/
public function getId()
{
// TODO: Implement getId() method.
return $this->id;
}
}
```
### 修改配置文件
```php
[
// 用户权限配置
"user"=>[
'identityClass'=>'App\Model\User', // 指定用户model
// 目前只支持 HttpHeaderAuth 您也可以自己重构,实现Vartruexuan\HyperfHttpAuth\Auth\AuthInterface 这里指定您自己的类即可
'authClass'=>\Vartruexuan\HyperfHttpAuth\Auth\HttpHeaderAuth::class, // 默认HttpHeaderAuth
'expire'=>24*3600, // 过期时长
],
]
];
```
### 设置中间件 (这里展示配置方式/路由方式,具体可参考 [hyperf](https://hyperf.wiki/2.1/#/zh-cn/middleware/middleware) 官方文档)
1.配置方式 config/autoload/middlewares.php
```php
[
Vartruexuan\HyperfHttpAuth\Middleware\AuthMiddleware::class, // 登录权限
],
];
```
2.路由方式
```php
[
// 登录权限验证
Vartruexuan\HyperfHttpAuth\Middleware\AuthMiddleware::class,
]
]);
```
### 使用
```php
request->post('username', '');
$password = $this->request->post('password', '');
if(!$identity=User::validatePassword($username,$password)){
return $this->sendError("密码错误");
}
// 授权登录信息
$userContainer = AuthHelper::getUserContainer();
$userContainer->login($identity);
return $this->sendSuccess([
'access_token' => $userContainer->getAccessToken()
]);
}
/**
* 退出登录
*
*/
public function logout()
{
AuthHelper::getUserContainer()->logout();
return $this->sendSuccess();
}
public function info()
{
// 获取当前用户对象
$user= AuthHelper::getUserContainer()->getIdentity();
}
}
```
### 免登录:目前只支持注解方式FreeLogin(后期加上配置方式),类注解应用于当前控制器, method 注解只应用于当前method
```php