stru/stru-hyperf-auth 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stru/stru-hyperf-auth

Composer 安装命令:

composer require stru/stru-hyperf-auth

包简介

集成到Hyperf中类似 Laravel 的 Auth 组件, 配合 stru/stru-hyperf-ui 使用可提供界面

README 文档

README

Description

类似laravel auth,实现了session,jwt登录验证,其他方式可自行实现。jwt使用了hyperf-ext/jwt 现成的方法进行了集成,在此表示感谢。

Installation

composer require stru/stru-hyperf-auth

Publish

php bin/hyperf.php vendor:publish hyperf/session
php bin/hyperf.php vendor:publish stru/stru-hyperf-auth
// jwt 新增发布
php bin/hyperf.php vendor:publish hyperf-ext/jwt

Database

php bin/hyperf.php migrate

Config

// config/authload/middlewares.php 添加session
\Hyperf\Session\Middleware\SessionMiddleware::class,
\Hyperf\Validation\Middleware\ValidationMiddleware::class,

// config/authload/exceptions.php 添加异常处理
\Stru\StruHyperfAuth\AuthExceptionHandler::class,

// 添加模型 App\Model\User.php  [数据库在上面设置中通过migrate发布]
1. 如果要实现注册在模型下添加
protected $fillable = ['name','account','email','password','mobile'];
2. 要使用验证 需实现接口 Authenticatable,(use Stru\StruHyperfAuth\Authenticatable;)
3. User模型中添加如下代码
public function getAuthIdentifierName(): string
{
    return $this->getKeyName();
}

public function getAuthIdentifier()
{
    return $this->getKey();
}

public function getAuthPassword(): string
{
    return $this->password;
}
4. 补充jwt实现后在原基础上还需要实现 HyperfExt\Jwt\Contracts\JwtSubjectInterface 并添加如下方法
  public function getJwtIdentifier()
    {
        return $this->getKeyName();
    }

    public function getJwtCustomClaims(): array
    {
        return [];
    }

Use

// 在使用登录注册视图之前要先安装 “stru/stru-hyperf-ui” 具体使用方法详见其文档,同时要取消如下注释
1. resources/views/layouts/app.blade.php
    38,45,48,58 行注释取消
2. resources/views/home.blade.php  
    69,71,74 行的注释取消  (如果无需使用该模板,则无需此操作)

// 添加控制器(LoginController),在控制器编写如下代码
<?php


namespace App\Controller\Auth;

use Hyperf\Contract\ContainerInterface;
use Hyperf\Contract\SessionInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Stru\StruHyperfAuth\AuthManager;
use function Hyperf\ViewEngine\view;

/**
 * Class LoginController
 * @package App\Controller\Auth
 * @Controller(prefix="auth")
 */
class LoginController
{
    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;
    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;
    /**
     * @Inject
     * @var ResponseInterface
     */
    protected $response;
    /**
     * @Inject
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;
    /**
     * @Inject
     * @var AuthManager
     */
    protected $auth;
    /**
     * @Inject()
     * @var SessionInterface
     */
    protected $session;
    /**
     * @RequestMapping(path="login",methods="GET")
     */
    public function showLogin()
    {
        return view('auth.login');
    }

    /**
     * @RequestMapping(path="login",methods="POST")
     */
    public function login()
    {
        if ($errMessage = $this->validateLogin()){
            return view('auth.login',[
                'error_message' => $errMessage
            ]);
        }
        $email = $this->request->input('email','');
        $password = $this->request->input('password','');

        if(! $this->auth->guard()->attempt(['email' => $email,'password' =>$password]))
        {
            return view('auth.login',[
                'error_message' => '用户不存在或密码错误'
            ]);
        }
        return $this->sendLoginResponse();
    }
    /**
     * @RequestMapping(path="register",methods="GET")
     */
    public function showRegister()
    {
        return view('auth.register');
    }

    /**
     * @RequestMapping(path="register",methods="POST")
     */
    public function register()
    {
        if ($errMessage = $this->validateRegister()){
            return view('auth.register',[
                'error_message' => $errMessage
            ]);
        }
        $params = $this->request->all();
        if (! $this->auth->guard()->checkRegister($params)){
            return view('auth.register',[
                'error_message' => '邮箱|账号|手机号 已注册或系统异常'
            ]);
        }
        return $this->response->redirect('/auth/login');
    }
    /**
     * @RequestMapping(path="logout",methods="POST")
     */
    public function logout()
    {
        $this->auth->guard()->logout();
        return $this->response->redirect('/auth/login');
    }

    protected function sendLoginResponse()
    {
        $path = $this->session->get('url:auth_before');
        return $path ? $this->response->redirect($path) : $this->response->redirect('/home');
    }

    private function validateLogin()
    {
        $validator = $this->validationFactory->make(
            $this->request->all(),
            [
                'email' => 'required|email',
                'password' => 'required|string'
            ]
        );
        if ($validator->fails()){
            return $validator->errors()->first();
        }
        return null;
    }

    protected function validateRegister()
    {
        $validator = $this->validationFactory->make(
            $this->request->all(),
            [
                'name' => 'required|string|max:50',
                'email' => 'required|email|max:50',
                'password' => 'required|string',
                'password_confirmation' => 'required|string|same:password'
            ],
            [
                'name.max' => '用户名最大支持50位',
                'email.max' => '邮箱最大支持50位',
                'password_confirmation.same' => '两次密码不一致',
            ]
        );
        if ($validator->fails()){
            return $validator->errors()->first();
        }
        return null;
    }
    
    // jwt 变更,目前只写了登录,其他的可自行实现
    /**
    * @RequestMapping(path="login",methods="POST")
    */
    public function login()
    {
        [$errMessage, $errs] = $this->validateLogin();
        if ($errMessage){
            return $this->responseFail($errMessage, json_decode($errs, true));
        }
        $account = $this->request->input('account','');
        $password = $this->request->input('password','');
        // 验证用户存在
        $user = User::where('account', $account)->first();
        if (!$user){
            return $this->responseFail("用户不存在", []);
        }
        // 验证密码
        if(!$token = $this->auth->guard()->attempt(['account' => $account,'password' => $password])) {
            return $this->responseFail("密码错误", []);
        }
        return $this->sendLoginResponse($token);
    }
    
    /**
    * @RequestMapping(path="logout",methods="POST")
    */
    public function logout()
    {
        $this->auth->guard()->logout();
        return $this->responseSuccess();
    }

    protected function sendLoginResponse(string $token)
    {
        $user = $this->auth->guard()->user()->toArray();
        unset($user['password']);
        
        return $this->responseData([
            'user' => $user,
            'token' => $token
        ]);
    }
}

stru/stru-hyperf-auth 适用场景与选型建议

stru/stru-hyperf-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stru/stru-hyperf-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stru/stru-hyperf-auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 stru/stru-hyperf-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-25