shoprenter/sr-oauth-jwt-security
Composer 安装命令:
composer require shoprenter/sr-oauth-jwt-security
包简介
This package provides the ability to secure endpoints with shoprenter oauth token
README 文档
README
This Symfony bundle provides JWT-based OAuth token verification and user authentication for securing your API endpoints.
Installation
-
Install the bundle using Composer:
composer require shoprenter/sr-oauth-jwt-security
-
Configure the bundle in
config/packages/shoprenter.yaml:shoprenter_oauth_jwt_security: oauth_jwt_security: public_key_path: '%kernel.project_dir%/config/jwt/jwtRS256.key.pub'
-
Configure security in
config/packages/security.yaml:security: providers: jwt_users: id: Shoprenter\OauthJWTSecurity\User\OAuthAccessTokenUserProvider firewalls: jwt_bearer: pattern: ^/api stateless: true access_token: provider: jwt_users token_handler: Shoprenter\OauthJWTSecurity\AccessToken\OAuthAccessTokenHandler access_control: - { path: ^/api, roles: ROLE_JWT_AUTHENTICATED_USER }
Usage
Securing Endpoints with Scopes
Use voter attributes to check for specific OAuth scopes:
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\HttpFoundation\Response; class ProductController extends AbstractController { public function getProducts(AuthorizationCheckerInterface $authChecker): Response { // Check if the user has the 'read_products' scope if (!$authChecker->isGranted('product.product:read')) { throw $this->createAccessDeniedException('Missing required scope'); } // Your protected code here... } }
Using Annotations/Attributes
With Symfony 6.4, you can use PHP attributes to secure controllers:
use Symfony\Component\Security\Http\Attribute\IsGranted; class ProductController extends AbstractController { #[IsGranted('product.product:read')] public function getProducts(): Response { // This endpoint requires the 'product.product:write' scope // ... } #[IsGranted('product.product:write')] public function createProduct(): Response { // This endpoint requires the 'product.product:write' scope // ... } }
Client Authentication
Clients must include a Bearer token in the Authorization header:
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5...
Error Handling
The authenticator will return a JSON response with a 401 status code if authentication fails.
It will return 403 status if a required scope is missing.
Technical Implementation Details
Service Configuration
This bundle follows Symfony's best practices for service configuration:
- Services are defined in
src/Resources/config/services.yaml - The service configuration is loaded by the bundle's extension class (
ShoprenterOauthJWTSecurityExtension) - When the bundle is enabled in your application, all services are automatically registered with the Symfony container
This approach ensures that services are properly loaded and configured without requiring manual setup in your application.
Development
```shell
docker build -t sr-oauth-jwt-security .
```
Run the container:
```shell
docker run -d --name sr-oauth-jwt-security-container -v $(pwd):/var/www sr-oauth-jwt-security
```
This command:
-d: Runs the container in detached mode (background) --name: Assigns a name to the container -v $(pwd):/var/www: Mounts your current directory to /var/www in the container
Enter the running container:
```shell
docker exec -it sr-oauth-jwt-security-container bash
```
Running Tests
The bundle includes comprehensive unit tests for all core components. To run the tests:
-
Install the development dependencies:
composer install --dev
-
Run the PHPUnit tests:
./vendor/bin/phpunit
shoprenter/sr-oauth-jwt-security 适用场景与选型建议
shoprenter/sr-oauth-jwt-security 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 shoprenter/sr-oauth-jwt-security 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shoprenter/sr-oauth-jwt-security 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-27