hallboav/silex-jwt-security
最新稳定版本:v1.1
Composer 安装命令:
composer require hallboav/silex-jwt-security
包简介
JSON web token security provider for Silex 2.
README 文档
README
Install
composer require hallboav/silex-jwt-security
Usage
Set up JSON web token application (which extends Application class from Silex)
This is required if you want to use the JsonWebTokenTrait trait.
use Hallboav\JsonWebTokenApplication; $app = new JsonWebTokenApplication();
Register the service provider
use Hallboav\Provider\JsonWebTokenSecurityServiceProvider; $app->register(new JsonWebTokenSecurityServiceProvider());
Set up your Symfony's firewalls
use Silex\Provider\SecurityServiceProvider; $providerKey = 'jwt0'; $app['security.user_provider.' . $providerKey] = function ($app) { return $app['security.jwt.user_provider']; }; $app->register(new SecurityServiceProvider(), [ 'security.firewalls' => [ $providerKey => [ 'pattern' => '^/admin', // any url that matches this pattern 'stateless' => true, 'guard' => [ 'authenticators' => [ 'security.jwt.guard_authenticator' ] ] ] ] ]);
Examples of how to generate and retrieve your json web token (thanks to Luís Cobucci)
use Symfony\Component\Security\Core\User\User; $app->get('/get-token', function () use ($app) { $user = new User('hall', 'KIPP', ['ROLE_ADMIN']); $token = $app['security.jwt.builder'] ->setExpiration(strtotime('+15 minutes')) ->set('username', $user->getUsername()) ->set('roles', $user->getRoles()) ->sign($app['security.jwt.default_signer'], $app['security.jwt.secret']) ->getToken(); return $app->json(['token' => (string) $token]); }); $app->get('/admin', function () use ($app) { $user = $app['user']; return $app->json([ 'user' => [ 'username' => $user->getUsername(), 'roles' => $user->getRoles(), 'token' => (string) $app->getToken() ] ]); });
That's it!
$app->run();
统计信息
- 总下载量: 6.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-18