rch / jwt-user-bundle
包含 FOSUserBundle 集成的 Json Web Token 身份验证包
此包的官方仓库似乎已丢失,因此该包已被冻结。
Requires
- php: >=5.5
- doctrine/orm: ^2.5
- fabpot/goutte: ~1.0|~2.0|~3.0
- friendsofsymfony/user-bundle: ~2.0-dev
- gesdinet/jwt-refresh-token-bundle: ~0.1
- lexik/jwt-authentication-bundle: ~1.0
- symfony/symfony: ~3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.11
- symfony/swiftmailer-bundle: ~2.3
This package is not auto-updated.
Last update: 2019-05-29 02:10:00 UTC
README
RCH/JWTUserBundle (v2.x)
通过 JSON Web Token 在您的 REST API 中管理用户。
包含内容
要求
- PHP 5.5+
- Symfony 3.0+
注意 此分支需要 friendsofsymfony/user-bundle
版本 ~2.0
。
对于使用 friendsofsymfony/user-bundle
版本 ~1.3
(>= 1.3, < 2.0
),请使用此包的 1.x
分支。
安装
1) 下载包
打开命令行,进入您的项目目录并执行以下命令以下载此包的最新稳定版本
$ composer require rch/jwt-user-bundle:2.0.x-dev
此命令要求您全局安装 Composer,如 Composer 文档的 安装章节 所述。
2) 启用包
注意: 此包需要注册第三方包。
然后,通过将它们添加到项目的 app/AppKernel.php
文件中注册的包列表中,来启用这些包
// app/AppKernel.php <?php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new RCH\JWTUserBundle\RCHJWTUserBundle(), new FOS\UserBundle\FOSUserBundle(), new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(), new Gesdinet\JWTRefreshTokenBundle\GesdinetJWTRefreshTokenBundle(), ); // ... } // ... }
配置
1) 创建您的用户类
您的用户类需要扩展 RCH\JWTUserBundle\Entity\User
映射超类。
示例
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use RCH\JWTUserBundle\Entity\User as BaseUser; /** * @ORM\Entity * @ORM\Table(name="fos_user") */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function __construct() { parent::__construct(); // your own logic } }
2) 设置包配置
配置它
# app/config/config.yml rch_jwt_user: user_class: AppBundle\Entity\User # your user class (required) user_identity_field: email # the property used as authentication credential (tied to password) passphrase: foobar # the passphrase of your RSA private key
加载路由
# app/config/routing.yml rch_jwt_user: resource: "@RCHJWTUserBundle/Resources/config/routing.yml" # Set a prefix if you want i.e. prefix: /api
相应地设置安全配置,例如
security: providers: fos_userbundle: id: fos_user.user_provider.username_email encoders: FOS\UserBundle\Model\UserInterface: bcrypt role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false # Signin login: pattern: ^/login stateless: true anonymous: true form_login: provider: fos_userbundle check_path: /login require_previous_session: false username_parameter: email password_parameter: password success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure # Signup register: pattern: ^/register anonymous: true stateless: true # Refresh token refresh: pattern: ^/refresh_token stateless: true anonymous: true # API (secured via JWT) api: pattern: ^/ stateless: true lexik_jwt: ~ access_control: - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/refresh_token, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/, role: IS_AUTHENTICATED_FULLY }
3) 生成用于签名/验证 JWT 令牌的 RSA 密钥
$ php bin/console rch:jwt:generate-keys
注意 此命令使用配置的密码短语生成密钥。
用法
通过 /register
路由注册用户
$ curl -X POST https://:8000/register -d username=johndoe -d password=test
通过 /login
路由获取 JWT 令牌
$ curl -X POST https://:8000/login -d username=johndoe -d password=test
通过 /refresh_token
路由刷新令牌(一旦它过期)
$ curl -X POST https://:8000/refresh_token -d token=[the expired token] -d refresh_token=[the refresh_token]
使用令牌访问受保护资源
$ curl -H "Authorization: Bearer [token here]" https://:8000
注意 在前面的示例中每次使用 username
时,都必须用安全防火墙映射中设置的 username_parameter
值替换。
注意2 对于密码字段,必须用 password_parameter
值替换。
注意3 对于密码字段,必须用 password_parameter
值替换。
贡献
请参阅《CONTRIBUTING.md》中的贡献指南,该文件已分发。
故障排除
该捆绑包包含一系列第三方依赖项,其稳定性主要取决于这些依赖项。
如果您在安装/使用过程中遇到问题,请在此存储库上打开一个问题此存储库。
许可证
代码在GPL-3.0许可证下发布。
有关完整版权信息,请参阅LICENSE文件。