Giter Site home page Giter Site logo

hyperf-jwt's Introduction

Hyperf JWT 组件

Latest Stable Version Latest Unstable Version License PHPUnit Release

安装

composer require l1n6yun/hyperf-jwt

发布配置

php bin/hyperf.php vendor:publish l1n6yun/hyperf-jwt

文件位于 config/autoload/jwt.php

配置

<?php

declare(strict_types=1);

use function Hyperf\Support\env;

return [
    'algo' => env('JWT_ALGO', 'HS256'),
    'secret' => env('JWT_SECRET'),
    'ttl' => env('JWT_TTL', 86400),
    'leeway' => env('JWT_LEEWAY', 0),
    'keys' => [
        'public' => env('JWT_PUBLIC_KEY'),
        'private' => env('JWT_PRIVATE_KEY'),
        'passphrase' => env('JWT_PASSPHRASE'),
    ],
    'provider' => 'user',
    'providers' => [
        'user' => \App\Models\UserModel::class, // 需实现 JwtSubjectInterface 接口
    ],
];

生成秘钥

php bin/hyperf.php gen:jwt-secret

#php bin/hyperf.php gen:jwt-public-key

使用

use L1n6yun\HyperfJwt\Contracts\JwtSubjectInterface;

use function L1n6yun\HyperfJwt\auth;

// 模型实现了 JwtSubjectInterface 接口
class UserModel implements JwtSubjectInterface{
    public function getJwtIdentifier(){
        return (string)$this->id;
    };
    
    public static function retrieveById($key){
        return self::findFromCache($key);
    };
}

// 生成token
$userInfo = UserModel::query()->first();
auth()->login($userInfo)

// 退出登陆
auth()->logout();

// 获取载荷
auth()->getPayload();

// 获取用户信息
auth()->user();

// 刷新token
auth()->refresh();

// 检测登陆返回用户ID
auth()->check();

使用注解进行权限验证,注解适用于类和方法

<?php
namespace App\Controller;
use L1n6yun\HyperfJwt\Annotation\Auth;

#[Auth] // 全局注解,所有方法都需要验证
class TestController extends AbstractController
{
    #[Auth] // 方法注解,该方法需要验证
    public function userInfo(){
        
    }
}

异常助理

使用 L1n6yun\HyperfJwt\Exceptions\Handlers\AuthExceptionHandler ,此步骤可选,开发者可以自行捕捉 AuthExceptionUnauthorizedException 进行处理

# config/autoload/exceptions.php
use L1n6yun\HyperfJwt\Exceptions\Handlers\AuthExceptionHandler

return [
    'handler' => [
        'http' => [
            AuthExceptionHandler::class,
        ],
    ],
];

hyperf-jwt's People

Contributors

l1n6yun avatar

Stargazers

laomuji666 avatar Nice avatar  avatar qm avatar JJ avatar  avatar  avatar azhu avatar Zolio Marling avatar HuYihan avatar  avatar wozuidiao avatar  avatar MuaCherish avatar  avatar  avatar happyDev avatar  avatar 锌小子 avatar Woo avatar  avatar  avatar AhmedRabby avatar chiefass avatar Beacas avatar Tsung-Wei Huang avatar  avatar Junbo Yang avatar Andy.Yan avatar  avatar maozi avatar  avatar Jinge Li avatar jiangsiyi avatar Chen Zhao avatar Lujia Jin avatar  avatar  avatar  avatar  avatar JM Li avatar F-x avatar silence avatar  avatar McAloma avatar  avatar hangdada avatar  avatar Will Grant avatar  avatar Jcoder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.