ApiPlatformOpenApi不使用JWT令牌。
创始人
2024-09-08 02:31:34
0

在Api Platform中配置JWT令牌并使用它作为身份验证机制,需要以下几个步骤:

  1. 安装LexikJWTAuthenticationBundle或其它JWT Bundle,例如LCobucciJWTBundle。

  2. 在config/packages/security.yaml中添加JWT配置:

    security:
         encoders:
             App\Entity\User:
                 algorithm: auto
                 cost: 12
         providers:
             my_provider:
                 entity:
                     class: App\Entity\User
                     property: email
         firewalls:
             main:
                 anonymous: ~
                 guard:
                     authenticators:
                         - lexik_jwt_authentication.jwt_token_authenticator
                 provider: my_provider
                 stateless: true
     lexik_jwt_authentication:
         secret_key: '%env(APP_SECRET)%'
         public_key: '%env(PUBLIC_KEY_PATH)%'
         authentication_token_ttl: 86400
    
  3. 在config/routes.yaml中添加JWT路由:

     api_login:
         path: /api/login_check
         methods: ['POST']
    
  4. 在User类中实现UserInterface接口:

     namespace App\Entity;
    
     use Doctrine\ORM\Mapping as ORM;
     use Symfony\Component\Security\Core\User\UserInterface;
    
     /**
      * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
      */
     class User implements UserInterface
     {
         /**
          * @ORM\Id()
          * @ORM\GeneratedValue()
          * @ORM\Column(type="integer")
          */
         private $id;
    
         /**
          * @ORM\Column(type="string", length=180, unique=true)
          */
         private $email;
    
         /**
          * @ORM\Column(type="json")
          */
         private $roles = [];
    
         /**
          * @ORM\Column(type="string", length=100)
          */
         private $password;
    
         public function getId(): ?int
         {
             return $this->id;
         }
    
         public function getEmail(): ?string
         {
             return $this->email;
         }
    
         public function setEmail(string $email): self
         {
             $this->email = $email;
    
             return $this;
         }
    
         public function getRoles(): array
         {
             $roles = $this->roles;
    
             // guarantee every user at least has ROLE_USER
             $roles[] = 'ROLE_USER';
    
             return array_unique($roles);
         }
    
         public function setRoles(array $roles): self
         {
             $this->roles = $roles;
    
             return $this;
         }
    
         public function getPassword(): ?string
         {
             return $this->password;
         }
    
         public function setPassword(string $password): self
         {
             $this->password = $password;
    
             return $this;
         }
    
         public function getSalt()
         {
    

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...