Laravel 集成 JWT 认证

安装

执行以下命令安装最新稳定版本:

composer require tymon/jwt-auth

或者添加如下信息到你的 composer.json 文件中 并执行 composer update:

"tymon/jwt-auth": "0.5.*"

然后注册服务提供者到 Laravel config/app.php 中的 providers 数组:

Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,

添加 JWT 门面:

'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,

然后发布相应配置文件:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"

JWT token 生成密钥:

php artisan jwt:generate

添加 JWT 中间件:

'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,

jwt.auth 中间件验证 token 必要性、合法性、过期时间
jwt.refresh 解析请求中 token,并顺序刷新 token(同时废弃老的 token )并将其作为下一个响应的一部分

config/jwt.php 中,你可以配置以下选项:

  • ttl:token有效期(分钟)
  • refresh_ttl:刷新token时间(分钟)
  • algo:token签名算法
  • user:指向User模型的命名空间路径
  • identifier:用于从token的sub中获取用户
  • require_claims:必须出现在token的payload中的选项,否则会抛出TokenInvalidException异常
  • blacklist_enabled:如果该选项被设置为false,那么我们将不能废止token,即使我们刷新了token,前一个token仍然有效
  • providers:完成各种任务的具体实现,如果需要的话你可以重写他们
  • User —— providers.user:基于sub获取用户的实现
  • JWT —— providers.jwt:加密/解密token
  • Authentication —— providers.auth:通过证书/ID获取认证用户
  • Storage —— providers.storage:存储token直到它们失效

创建Token

 public function authenticate(Request $request)
{
    // grab credentials from the request
    $credentials = $request->only('email', 'password');

    try {
        // attempt to verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // all good so return the token
    return response()->json(compact('token'));
}

还可以直接通过用户对象实例创建token:

$user = User::first();
$token = JWTAuth::fromUser($user);

自定义token

使用 JWTFactory 门面可以轻易的创建自定义token

public function authenticate(Request $request)
{
    // grab credentials from the request
    $credentials = $request->only('email', 'password');

    try {
        // attempt to verify the credentials and create a token for the user
        if (!JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    $customClaims = ['user_id' => auth()->user()->id, 'project_id' => '16'];
    $payload = JWTFactory::make($customClaims);

    $token = JWTAuth::encode($payload)->get();

    // all good so return the token
    return response()->json(compact('token'));
}

用户认证

http发送一个需要认证通过的请求,需要设置Authorization头:

Authorization: Bearer {yourtokenhere}

或者在 url 后面加上 token 参数:

http://api.mysite.com/me?token={yourtokenhere}

认证用户信息

我们从 token 中获取认证用户只需要调用 JWTAuth::parseToken()->authenticate()

public function getAuthenticatedUser()
{
    try {

        if (! $user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }

    } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        return response()->json(['token_invalid'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {

        return response()->json(['token_absent'], $e->getStatusCode());

    }

    // the token is valid and we have found the user via the sub claim
    return response()->json(compact('user'));
}

解析自定义token

我们要解析上面自定义 token,不能使用 JWTAuth::parseToken()->authenticate() 这个方法。这种方法是获取认证用户的信息,并不是我们上面自定义的信息,要想获取自定义信息,需要用到 JWTAuth::parseToken()->getPayload() 方法。

public function getAuthenticatedUser()
{
    try {

        if (! $user = JWTAuth::parseToken()->getPayload()->get()) {
            return response()->json(['user_not_found'], 404);
        }

    } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        return response()->json(['token_invalid'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {

        return response()->json(['token_absent'], $e->getStatusCode());

    }
    // the token is valid and we have found the user via the sub claim
    return response()->json(compact('user'));
}

注意:$token = JWTAuth::encode($payload)->get();! $user = JWTAuth::parseToken()->getPayload()->get() 中都调用了 get() 方法,如果不调用,response()->json() 会转换不成功,永远返回空的 josn 数据。

OK,关于 JWT 的基本集成知识与认证方法就介绍到这里

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值