thinkphp适配php8,thinkPHP配置jwt(代码实例)

1890943a675b43d8af09ad9e2cc0f0f5.png

thinkphp5.1-jwt的安装与使用

安装jwt插件

在composer.json的require中加入如下配置

"firebase/php-jwt": "^5.0"

在项目根目录下执行composer update即可.

创建一个auth中间件

php think make:middleware Auth

打开applicationhttpmiddlewareAuth文件<?php

namespace app\http\middleware;

use Firebase\JWT\JWT;

use Firebase\JWT\SignatureInvalid\Exception;

use think\exception\TokenException;

use think\exception\ValidateException;

use think

acade\Cache;

use think

acade\Config;

class Auth

{

public function handle($request, Closure $next)

{

$bearer_token = [];

$bearer = $request->header('authorization');//取header中的token

if ($bearer !== null) {

//不空尝试去匹配

preg_match('/bearers*(S+)/i', $bearer, $bearer_token);

}

if (empty($bearer_token[1])) {

//匹配不到结果尝试去url中获取

if ($request->param('token') !== null) {

$token = $request->param('token');

}else{

throw new TokenException('请登录', 401);

}

}else{

$token=$bearer_token[1];

}

try {

$de_token = JWT::decode($token, Config::get('JWT_KEY'), Config::get('JWT_ENCRYPTION'));

} catch (SignatureInvalidException $exception) {

//捕获JWT解析错误

throw new TokenException('无效令牌', 401);

} catch (Exception $exception) {

throw new TokenException('请重新登录', 401);

}

if ($de_token->voe < time() && $de_token->exp > time()) {

throw new TokenException('请换取新令牌', 402);

} else if ($de_token->voe < time()) {

throw new TokenException('请重新登录', 401);

}

if (Cache::tag('login')->get('token_' . $de_token->data->uid) != $token) {

throw new TokenException('用户信息错误,请重新登录', 401);

}

if ($de_token->data->is_ban == 1) {

throw new ValidateEx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThinkPHP框架中,配置数据库需要在`/config/database.php`文件中进行配置。以下是一个基本的数据库配置示例: ```php return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => 'localhost', // 数据库名 'database' => 'database_name', // 用户名 'username' => 'root', // 密码 'password' => 'root', // 端口 'hostport' => '3306', // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'tp_', // 数据库调试模式 'debug' => true, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0, // 数据库读写是否分离 主从式有效 'rw_separate' => false, // 读写分离后 主服务器数量 'master_num' => 1, // 指定从服务器序号 'slave_no' => '', ]; ``` 在上面的配置中,需要设置`type`、`hostname`、`database`、`username`和`password`等数据库连接信息。其中,`type`表示数据库类型,`hostname`表示服务器地址,`database`表示数据库名,`username`表示数据库用户名,`password`表示数据库密码。 设置好数据库连接信息后,就可以在应用程序中使用数据库了。例如: ```php use think\Db; // 查询数据 $result = Db::name('user')->where('id', 1)->find(); // 插入数据 Db::name('user')->insert(['username' => 'test', 'password' => '123456']); // 更新数据 Db::name('user')->where('id', 1)->update(['password' => '654321']); // 删除数据 Db::name('user')->where('id', 1)->delete(); ``` 以上示例中,使用了`Db`类来进行数据库操作。`Db::name('user')`表示操作`user`表,`where`方法用于设置条件,`find`方法用于查询单条数据,`insert`方法用于插入数据,`update`方法用于更新数据,`delete`方法用于删除数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值