hyperf2.1+96qbhy/hyperf-auth组件安装使用
swoole  /  管理员 发布于 3年前   2454
我有个项目打算用jwt,在找轮子中找到了这个,感觉很便捷,后面打算用,所以测试记录一下
直接按git上的步骤走
我的测试环境介绍:
hyperf2.1 (还没升级比较懒)
因为我到项目在线上跑,所以要先停一下,进入步骤:
[root@hyperf ~]# netstat -anp | grep 9501
tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 2793/skeleton.Maste
tcp 0 0 192.168.1.98:9501 192.168.1.38:47904 TIME_WAIT -
[root@hyperf ~]# kill -9 2793
进入项目目录开始安装组件
[root@hyperf ~]# cd /home/www/hyperf-skeleton/
[root@hyperf hyperf-skeleton]# composer require 96qbhy/hyperf-auth
Using version ^2.4 for 96qbhy/hyperf-auth
./composer.json has been updated
Running composer update 96qbhy/hyperf-auth
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
- Locking 96qbhy/hyperf-auth (v2.4.3)
- Locking 96qbhy/simple-jwt (v1.3.1)
- Locking doctrine/cache (1.12.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
As there is no 'unzip' command installed zip files are being unpacked using the PHP zip extension.
This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost.
Installing 'unzip' may remediate them.
- Downloading doctrine/cache (1.12.1)
- Downloading 96qbhy/simple-jwt (v1.3.1)
- Downloading 96qbhy/hyperf-auth (v2.4.3)
- Installing doctrine/cache (1.12.1): Extracting archive
- Installing 96qbhy/simple-jwt (v1.3.1): Extracting archive
- Installing 96qbhy/hyperf-auth (v2.4.3): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> rm -rf runtime/container
74 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
生成auth配置文件 (因为是测试我直接在phpstorm批量搜索users 然后用admin替换之)
[root@hyperf hyperf-skeleton]# php bin/hyperf.php vendor:publish 96qbhy/hyperf-auth
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[96qbhy/hyperf-auth] publishes [auth] successfully.
代码就不帖了
生成env中auth信息
[root@hyperf hyperf-skeleton]# php bin/hyperf.php gen:auth-env
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
AUTH_SSO_CLIENTS 已生成!
SSO_JWT_SECRET 已生成!
SIMPLE_JWT_SECRET 已生成!
.env代码信息
AUTH_SSO_CLIENTS=h5,weapp
SSO_JWT_SECRET=OxZca6IHFJjH6E2g
SIMPLE_JWT_SECRET=yRQWfCXW5LAty90c
添加登录验证中间件 (注意:有个点的)
php ./bin/hyperf.php gen:middleware Auth
生成中间件文件:\hyperf\app\Middleware\Auth.php
创建mode文件 : \hyperf\app\Model\Admin.php
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
use Qbhy\HyperfAuth\Authenticatable;
class Admin extends Model implements Authenticatable
{
protected $table = 'admin';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
protected $dates = ['created_at', 'updated_at'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
创建控制器
生成token,删除token,刷新token等操作
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\GetMapping;
use Qbhy\HyperfAuth\Annotation\Auth;
use Qbhy\HyperfAuth\AuthManager;
use App\Model\Admin;
/**
* @Controller()
*/
class AdminController extends AbstractController
{
/**
* @Inject
* @var AuthManager
*/
protected $auth;
/**
* @GetMapping(path="/admin/login")
* @return array
*/
public function login()
{
$user = Admin::query()->where('name','admin')->where('password',123456)->first();
// auth('guard') || auth()->guard('session'); 不设置guard,则使用默认的配置jwt
$token = auth()->login($user);
return [
'token' => $token,
'date' => date('Y-m-d H:i:s')
];
}
/**
* 使用 Auth 注解可以保证该方法必须通过某个 guard 的授权,支持同时传多个 guard,不传参数使用默认 guard
* @Auth("jwt")
* @GetMapping(path="/admin/user")
* @return string
*/
public function user()
{
$user = auth()->user();
return $user;
}
/**
* 退出登录,token失效
* @Auth("jwt")
* @GetMapping(path="/admin/logout")
*/
public function logout()
{
auth()->logout();
return 'logout ok';
}
/**
* 刷新token,旧token就会失效
* @Auth("jwt")
* @GetMapping(path="/admin/retrieve")
*/
public function retrieve()
{
$token = auth()->refresh();
return [
'token' => $token,
'date' => date('Y-m-d H:i:s')
];
}
}
来几张效果图:
用户信息,已经登录了(已拿到token情况)
刷新tk
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号