使用token进行身份验证过程
1.客户端使用账号密码进行登录
2.服务端接受到请求后验证账号以及密码的正确性,若正确则服务端回传一个Token
3.客户端接收到Token后对其进行存储,每次访问时需携带token
4.服务端在接受到客户端请求时需验证token有效性,验证成功则回传数据。
生成与验证token的方法有很多种,我们这里使用的是jwt( Json Web Token)。
使用前提
首先使用方法需要在composer.json中引入firebase/php-jwt,之后进行composer安装。
名称解释
iss (issuer)issuer 请求实体,可以是发起请求的用户的信息,也可是jwt的签发者
sub (Subject)设置主题,类似于发邮件时的主题
aud (audience)接收jwt的一方
exp (expire)token过期时间
nbf (not before)当前时间在nbf设定时间之前,该token无法使用
iat (issued at)token创建时间
jti (JWT ID)对当前token设置唯一标示
class JWTTool extends Controller {
public function __construct(ContainerInterface $container = null)
{
header("Content-Type: text/html; charset=utf-8");
$this->setContainer($container);
}
pr