da / api-server-bundle
DaApiServerBundle 是一个允许以简单和安全的方式提供 REST API 的 Symfony2 扩展包
v1.0.3
2020-05-19 15:25 UTC
Requires
- php: >=5.3.3
- da/auth-common-bundle: 1.0.*
- symfony/config: >=2.1
- symfony/dependency-injection: >=2.1
Requires (Dev)
- doctrine/doctrine-bundle: 1.2.*
- doctrine/mongodb-odm: dev-master
- doctrine/orm: >=2.2.3
- phpunit/phpunit: 3.7.*
Suggests
- da/api-client-bundle: Allow to connect to a remote SSO server
- doctrine/doctrine-bundle: Allow to use the lexer
- doctrine/mongodb-odm: Allow to use the lexer
- doctrine/orm: Allow to use the lexer
README
DaApiServerBundle 是一个允许以简单和安全的方式提供 REST API 的 Symfony2 扩展包。
安装
安装是一个快速的两步过程。
步骤 1: 在 composer 中添加
在 composer.json 文件中添加扩展包
// composer.json "require": { // ... "da/auth-common-bundle": "dev-master", "da/api-server-bundle": "dev-master" },
然后更新您的供应商
composer update # WIN composer.phar update # LINUX
步骤 2: 在内核中声明
在您的内核中声明扩展包
// app/AppKernel.php $bundles = array( // ... new Da\AuthCommonBundle\DaAuthCommonBundle(), new Da\ApiServerBundle\DaApiServerBundle(), );
检查客户端 API 密钥
如果您想检查 API 路径的客户端的 API 令牌,您必须在您的 security.yml 中指定它
# app/config/security.yml security: firewalls: #... api: pattern: ^/api da_api: true stateless: true
位于 /api
下的 URL 将使用与请求一起发送的 API 令牌对您的 API 客户端进行身份验证。目前,API 令牌必须通过 HTTP 头 "X-API-Security-Token" 发送。
检查特定的 OAuth 令牌
如果您想检查请求的 Authorization
头中提供的 OAuth 令牌(Bearer 令牌),您可以按照以下方式指定
# app/config/security.yml security: firewalls: #... api_user: pattern: ^/api/user da_oauth: true stateless: true
远程检查
如果您的 API 不与您的 SSO 服务器(使用 OAuth 等)在同一个地方,只需遵循以下步骤
在 composer.json 文件中添加扩展包
// composer.json "require": { // ... "da/api-client-bundle": "dev-master" },
然后更新您的供应商
composer update # WIN composer.phar update # LINUX
然后,设置配置
# app/config/config.yml # DaApiClient Configuration da_api_client: api: sso_user: endpoint_root: %api.sso.endpoint_root% security_token: %api.sso.security_token% client: service: da_api_server.user_manager.http sso_client: endpoint_root: %api.sso.endpoint_root% security_token: %api.sso.security_token% client: service: da_api_server.client_manager.http # DaApiServer Configuration da_api_server: user_manager: da_api_client.api.sso_user client_manager: da_api_client.api.sso_client
最后,设置相应的参数
# app/config/parameters.yml and app/config/parameters.yml.dist parameters: # ... api.sso.endpoint_root: 'http://my-domain.com/api' api.sso.security_token: 3jgwm1izbse884cwskk00c0o4ww8kg08gsgc4o808gsssw4
文档
此扩展包还有一些其他功能,可以帮助您开发文档化的 REST API,具体请参阅这里。