ramyhakam/symfony-authentication-bundle

symfony 4/5 的jwt认证包

v1.0 2020-04-07 20:43 UTC

This package is auto-updated.

Last update: 2024-09-10 23:03:50 UTC


README

Latest Stable Version Total Downloads License

Symfony Authentication bundle 为您的 symfony 项目提供 JWT API 令牌认证,具有以下功能:

  • 生成和验证RSA加密的JWT令牌
  • 使用 symfony 安全性验证令牌来认证登录用户
  • 在过期前刷新令牌(稍后)

安装

此包需要 symfony v4+ 运行。

使用 Composer 安装

$ composer require ramyhakam/symfony-authentication-bundle

使用Bundle

  1. 首先,确保您已遵循主要的安全指南来创建您的用户类。然后,为了简化,将 apiToken 属性直接添加到您的用户类中

  2. 现在您有一个新的实体用于认证,它实现了 Symfony\Component\Security\Core\User\UserInterface;

  3. ! 这是由 Symfony Flex 自动完成的: 在您的 bundle.php 文件中启用此包,通过添加此行

    return [ 
        //...
    Hakam\AuthenticationBundle\HakamAuthenticationBundle::class => ['all' => true],
    ];
  4. 然后您需要在您的 packages/security.yml 中添加以下行,以将认证器用作您的防火墙中的 auth

    #example firewalls
    
    login:  
       pattern:   /api/login
       stateless: true
       scurity: false
       
     api:
         pattern:   ^/api
         stateless: true
          guard:
              authenticators:
                 - token-authenticator  #using jwt authenticator here
    
  5. 现在您可以注入 JWTTokenAuthenticatorService 并根据用户数据生成用户令牌

    \\ Controller\AccountController.php
        
        namespace App\Controller;
    
    
        use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
        use Hakam\AuthenticationBundle\Services\JWTTokenAuthenticatorService;
    
       public class AccountController extends AbstractController
    
       /**
        * @var JWTTokenAuthenticatorService
        */
        private $authenticatorService;
    
        public function __construct(JWTTokenAuthenticatorService $authenticatorService)
          {
             $this->authenticatorService = $authenticatorService;
          }
    
        //..
    
        public funcion generateToken()
         {
            $authToken = $this->authenticatorService->generateUserToken($user->getApiToken());
         }
  6. 现在您可以在控制器中注入 UserIntetface,在令牌认证验证后,它将被登录用户对象替换。

    \\ Controller\AccountController.php
    
    namespace App\Controller;
    
    
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
    use Symfony\Component\Security\Core\User\UserInterface;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    
    public class AccountController extends AbstractController
    
        /**
         * @param UserInterface $user
         * @Security("is_granted('ROLE_User') 
         */
        public function needsAuthUser( UserInterface $user)
          { 
              $user  // is the logged in user object with ROLE_USER
              //..
          }

配置

  1. 生成您的私钥和公钥
$ openssl genrsa -out config/jwt/private.pem 2048
$ openssl rsa -in config/jwt/private.pem -outform PEM -pubout -out config/jwt/public.pem
  1. 在生成您的公钥和私钥后,您应该创建一个包配置文件,位于 config/packages/hakam_authentication_bundle.yaml,并包含以下配置
 hakam_authentication:
     public_path_key:      '%kernel.project_dir%/config/jwt/public.pem'
     private_path_key:     '%kernel.project_dir%/config/jwt/private.pem'

贡献

想要贡献?太好了!

  • 从存储库中复制您的副本
  • 添加您的新酷炫功能
  • 编写更多的测试
  • 创建一个新的Pull request

许可

MIT

自由软件,太棒了!