All Projects → vartruexuan → hyperf-http-auth

vartruexuan / hyperf-http-auth

Licence: MIT license
hyperf框架http登录授权组件

Programming Languages

PHP
23972 projects - #3 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to hyperf-http-auth

Mosquitto Go Auth
Auth plugin for mosquitto.
Stars: ✭ 212 (+1225%)
Mutual labels:  auth
restish
Restish is a CLI for interacting with REST-ish HTTP APIs with some nice features built-in
Stars: ✭ 453 (+2731.25%)
Mutual labels:  auth
vuejwtauth
Client-side JWT auth package for Vue.js (and Quasar) applications
Stars: ✭ 13 (-18.75%)
Mutual labels:  auth
Django Rest Registration
User-related REST API based on the awesome Django REST Framework
Stars: ✭ 240 (+1400%)
Mutual labels:  auth
requests auth
Authentication classes to be used with requests
Stars: ✭ 28 (+75%)
Mutual labels:  auth
laravel-otp-login
Adds a customizable, translatable, configurable OTP verification step to Laravel Auth. You can add your own SMS provider too.
Stars: ✭ 16 (+0%)
Mutual labels:  auth
Qtfirebase
An effort to bring Google's Firebase C++ API to Qt + QML
Stars: ✭ 208 (+1200%)
Mutual labels:  auth
socketio-jwt-auth
Socket.io authentication middleware using Json Web Token
Stars: ✭ 87 (+443.75%)
Mutual labels:  auth
hutplate
A Go library over standard net/http library with auth, session, err handling and more.
Stars: ✭ 28 (+75%)
Mutual labels:  auth
insight auth
身份验证服务
Stars: ✭ 32 (+100%)
Mutual labels:  auth
Leacmf
leacmf是一款基于ThinkPHP5.1+layui的极速后台和api开发框架。
Stars: ✭ 244 (+1425%)
Mutual labels:  auth
vue-auth-boilerplate
This is a simple Vue template/starter kit, scaffolded on vue-cli 3, with full Auth functions to Login & Register
Stars: ✭ 77 (+381.25%)
Mutual labels:  auth
rocket auth
An implementation for an authentication API for Rocket applications.
Stars: ✭ 65 (+306.25%)
Mutual labels:  auth
Simpleauth
A easy to use social authentication android library. (Facebook, Google, Twitter, Instagram)
Stars: ✭ 216 (+1250%)
Mutual labels:  auth
HyperVue
🚀 基于 Hyperf + Vue + ElementUI 前后端分离的通用型管理后台
Stars: ✭ 23 (+43.75%)
Mutual labels:  hyperf
Twitch4j
Modular Async/Sync/Reactive Twitch API Client / IRC Client
Stars: ✭ 209 (+1206.25%)
Mutual labels:  auth
Sphinx
Authorization library for Phoenix web framework
Stars: ✭ 19 (+18.75%)
Mutual labels:  auth
casdoor-go-sdk
Go client SDK for Casdoor
Stars: ✭ 37 (+131.25%)
Mutual labels:  auth
rust cms
使用Rust编写一个CMS(内容管理系统)可以做为个人博客,公司网站
Stars: ✭ 32 (+100%)
Mutual labels:  auth
gcp auth
Minimal authentication library for Google Cloud Platform (GCP)
Stars: ✭ 42 (+162.5%)
Mutual labels:  auth

hyperf-http-auth

安装

composer require vartruexuan/hyperf-http-auth

发布配置文件

php bin/hyperf.php vendor:publish vartruexuan/hyperf-http-auth

创建 User model 文件 并且实现接口 IdentityInterface

<?php

namespace App\Model;

use Vartruexuan\HyperfHttpAuth\User\IdentityInterface;

class User implements IdentityInterface
{
    /**
     * 获取用户对象
     *
     * @param $id
     *
     * @return \App\Model\User
     */
    public static function findIdentityById($id)
    {
        // TODO: Implement findIdentityById() method.
        return User::query()->find($id);
    }

    /**
     *
     * @return mixed
     */
    public function getId()
    {
        // TODO: Implement getId() method.
        return $this->id;
    }

}

修改配置文件

<?php

declare(strict_types=1);

return [
    'default' => [
        // 用户权限配置
        "user"=>[
            'identityClass'=>'App\Model\User', // 指定用户model	
	// 目前只支持 HttpHeaderAuth 您也可以自己重构,实现Vartruexuan\HyperfHttpAuth\Auth\AuthInterface 这里指定您自己的类即可
            'authClass'=>\Vartruexuan\HyperfHttpAuth\Auth\HttpHeaderAuth::class, // 默认HttpHeaderAuth
            'expire'=>24*3600, // 过期时长
        ],
    ]
];

设置中间件 (这里展示配置方式/路由方式,具体可参考 hyperf 官方文档)

1.配置方式 config/autoload/middlewares.php

<?php

declare(strict_types=1);

return [
    'http' => [
        Vartruexuan\HyperfHttpAuth\Middleware\AuthMiddleware::class, // 登录权限
    ],
];

2.路由方式

<?php

use Hyperf\HttpServer\Router\Router;

// backend
router::addGroup('/backend/', function () {
    // Index
    router::addGroup('index/', function () {
        Router::addRoute(['GET', 'OPTIONS'], 'index', 'App\Module\backend\Controller\IndexController@index');
        // 登录
        Router::addRoute(['POST', 'OPTIONS'], 'login', 'App\Module\backend\Controller\IndexController@login');
        // 退出登录
        Router::addRoute(['POST', 'OPTIONS'], 'logout', 'App\Module\backend\Controller\IndexController@logout');
        Router::addRoute(['POST', 'OPTIONS'], 'test', 'App\Module\backend\Controller\IndexController@test');
    });


}, [
    'middleware' => [
        // 登录权限验证
      Vartruexuan\HyperfHttpAuth\Middleware\AuthMiddleware::class,

    ]
]);

使用

<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Vartruexuan\HyperfHttpAuth\Annotation\FreeLogin;

/**
 * Class IndexController
 *
 * @package App\Controller
 */
class IndexController extends BaseController
{

    /**
     * @Inject
     * @var \App\Service\Uc\UserService
     */
    public $userService;

    /**
     * 登录
     *
     * @FreeLogin
     */
    public function login()
    {
        $username = $this->request->post('username', '');
        $password = $this->request->post('password', '');
        if(!$identity=User::validatePassword($username,$password)){
		return $this->sendError("密码错误");
	}
        // 授权登录信息
        $userContainer = AuthHelper::getUserContainer();
        $userContainer->login($identity);

        return $this->sendSuccess([
            'access_token' => $userContainer->getAccessToken()
        ]);
    }

    /**
     * 退出登录
     *
     */
    public function logout()
    {
        AuthHelper::getUserContainer()->logout();
        return $this->sendSuccess();
    }

    public function info()
    {
        // 获取当前用户对象
        $user= AuthHelper::getUserContainer()->getIdentity();

    }



}

免登录:目前只支持注解方式FreeLogin(后期加上配置方式),类注解应用于当前控制器, method 注解只应用于当前method

<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Vartruexuan\HyperfHttpAuth\Annotation\FreeLogin;

/**
 * @Freelogin
 */
class IndexController extends BaseController
{

    /**
     * 退出登录
     * @Freelogin
     */
    public function list()
    {

    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].