基础介绍
基于tp6的jwt模块 下载地址
数据库
如果多模块的话,需要建立对应的用户表、用户分组表、用户权限表
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `jm_admin`;
CREATE TABLE `jm_admin` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`group_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '组别ID',
`username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '用户名',
`nickname` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '昵称',
`password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '密码',
`salt` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '密码盐',
`email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '电子邮箱',
`mobile` varchar(11) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '手机号',
`avatar` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '头像',
`level` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '等级',
`gender` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '性别:0=女,1=男',
`birthday` date DEFAULT NULL COMMENT '生日',
`bio` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '格言',
`money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '余额',
`score` int(10) NOT NULL DEFAULT '0' COMMENT '积分',
`successions` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '连续登录天数',
`maxsuccessions` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '最大连续登录天数',
`prevtime` int(10) DEFAULT NULL COMMENT '上次登录时间',
`logintime` int(10) DEFAULT NULL COMMENT '登录时间',
`loginip` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '登录IP',
`loginfailure` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '失败次数',
`joinip` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '加入IP',
`jointime` int(10) DEFAULT NULL COMMENT '加入时间',
`createtime` int(10) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(10) DEFAULT NULL COMMENT '更新时间',
`token` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT 'Token',
`status` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '状态',
`verification` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '验证',
PRIMARY KEY (`id`),
KEY `username` (`username`),
KEY `email` (`email`),
KEY `mobile` (`mobile`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会员表';
DROP TABLE IF EXISTS `jm_admin_group`;
CREATE TABLE `jm_admin_group` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '组名',
`rules` text COLLATE utf8mb4_unicode_ci COMMENT '权限节点',
`createtime` int(10) DEFAULT NULL COMMENT '添加时间',
`updatetime` int(10) DEFAULT NULL COMMENT '更新时间',
`status` enum('normal','hidden') COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '状态',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会员组表';
DROP TABLE IF EXISTS `jm_admin_rule`;
CREATE TABLE `jm_admin_rule` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(10) DEFAULT NULL COMMENT '父ID',
`name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名称',
`title` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '标题',
`remark` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '备注',
`ismenu` tinyint(1) DEFAULT NULL COMMENT '是否菜单',
`createtime` int(10) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(10) DEFAULT NULL COMMENT '更新时间',
`weigh` int(10) DEFAULT '0' COMMENT '权重',
`status` enum('normal','hidden') COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '状态',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会员规则表';
DROP TABLE IF EXISTS `jm_admin_token`;
CREATE TABLE `jm_admin_token` (
`token` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Token',
`user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '会员ID',
`createtime` int(10) DEFAULT NULL COMMENT '创建时间',
`expiretime` int(10) DEFAULT NULL COMMENT '过期时间',
PRIMARY KEY (`token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会员Token表';
引用
下载到tp6的extend\jmwl下
use jmwl\realjwt\Token;
use jmwl\realjwt\Auth;
驱动和配置
$init= [
'type' => 'Mysql',
'table' =>"admin_token",
'userprefix'=>"up:",
'tokenprefix'=>"tp:",
'key' => '30tvUyXl8NkOx7YKDAoLu5FQRIGTh4qa',
'hashalgo' => 'ripemd160',
'expire' => 0,
];
$usertable="admin";
token类的使用和功能
这里基本不会用到,主要用的是auth类
/**
Token::init($init);
Token::check($token, $user_id);
Token::get($token, $default = false);
Token::set($token, $user_id, $expire = null);
Token::delete($token);
Token::clear($user_id = null);
auth的羁绊用法
基础用法
php的公共构造方法使用 验证权限
$this->auth = Auth::instance($init, $usertable);
$this->request = Request::instance();
$token = $this->request->server('HTTP_TOKEN');
$controllername = $this->request->controller();
$actionname = $this->request->action();
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
$this->auth->setRequestUri($path);
if (!$this->auth->match($this->noNeedLogin)) {
$this->auth->init($token);
if (!$this->auth->isLogin()) {
return $this->error('Please login first', null, 401);
}
if (!$this->auth->match($this->noNeedRight)) {
if (!$this->auth->check($path)) {
return $this->error('You have no permission', null, 403);
}
}
} else {
if ($token) {
$this->auth->init($token);
}
}
var_dump($this->auth->getUser());`
|