digitaldream/symfony-access-token
Composer 安装命令:
composer require digitaldream/symfony-access-token
包简介
Symfony Access Token Authentication Bundle.
README 文档
README
Installation
composer require digitaldream/symfony-access-token
Setup
Step 1:
copy config/packages/access_token.yaml from vendor folder
Step 2:
Copy config/routes/access_token.yaml from vendor folder
Step 3
Add these environment variables to your .env file
JWT_SECRET="YourSecretKey" JWT_KEY= JWT_ISSUER=localhost:8000 JWT_ALGORITHM=HS256 JWT_EXPIRE_AT='+24 hours'
Step 4
security: firewalls: api: pattern: ^/api provider: app_user_provider #your user provider stateless: true user_checker: AccessToken\Security\UserChecker access_token: token_handler: AccessToken\Security\AccessTokenHandler failure_handler: AccessToken\Security\AuthenticationFailureHandler access_control: - { path: ^/api, roles: ROLE_USER } # Change this line according to your project USER ROlES
Calling the Login API
fetch('/api/login',{ body: { username: 'john@example.com', password: 'YourPassword' } })
You can create your own login route. Just remove package route and use AccessToken\Services\CreateAccessTokenService
namespace App\Controller use AccessToken\Entity\AccessToken; use AccessToken\Services\CreateAccessTokenService; use AccessToken\Services\UserCredentialsRequest; use Symfony\Component\HttpFoundation\Request; class LoginController { public function __construct(private CreateAccessTokenService $accessTokenService) {} public function login(Request $request): { //Write your logic //@var AccessToken $accessToken $accessToken= $this->accessTokenService->execute(new UserCredentialsRequest('YourEmail@example.com','YourPassword')) } }
Enjoy!!!
Implement User verification and active feature
It will never generate a access token is user need to be email verified or inactive.
Simply implement the AccessToken\Entity\TokenUserInterface on your User Entity like below
class User implements UserInterface, PasswordAuthenticatedUserInterface, TokenUserInterface { public function isVerified(): ?bool { // return null if you don't have this functionality return true; } public function isActive(): ?bool { // return null if you don't have this functionality return true; } public function getUserIdentifierValue(): string { return $this->email; } public function getPublicId(): string { // It safe to use a UID (symfony UID) for generating JWT token. Do not expose your internal primary key return (string)$this->id; } }
Revoke Access Token
If you want to revoke all of the access token for a particular user then fire AccessToken\Events\RevokeAccessTokensEvent
namespace App\Controller; use AccessToken\Events\RevokeAccessTokensEvent; use \Symfony\Component\EventDispatcher\EventDispatcherInterface; class SomeController { public function someAction(EventDispatcherInterface $dispatcher){ //Do something with the User. E.g block or inactive or subscription expired. $dispatcher->dispatch(new RevokeAccessTokensEvent(1),RevokeAccessTokensEvent::NAME) } }
digitaldream/symfony-access-token 适用场景与选型建议
digitaldream/symfony-access-token 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 digitaldream/symfony-access-token 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 digitaldream/symfony-access-token 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-01