我想使用 OAuth2 和 Symfony2(实际上是 Symfony3)为我的 API 进行服务器到服务器身份验证。我正在使用 FOSOAuthServerBundle
。
远程服务器不会代表任何用户发出请求,所以我相信 client_credentials
是合适的授权类型。
我已经创建了一个客户端并且能够获得访问 token 。但是,我在使用 client_credentials
token 保护端点时遇到了问题。这是我的 security.yml 中的内容
firewalls:
api:
pattern: ^/api/v1
fos_oauth: true
stateless: true
anonymous: false # can be omitted as its default value
access_control:
- { path: ^/api/v1, roles: [ IS_AUTHENTICATED_FULLY ] }
当我尝试使用 Bearer Authorization header 访问
/api/v1
中的某些内容时,出现错误Full authentication is required to access this resource
这是一个示例请求:
GET /app_dev.php/api/v1/user HTTP/1.1
Host: local.dev
Authorization: Bearer NDFhMzViZjQ2YjMyYjFlNzBjZTZiMTU2ZjdhY2I4ZmZhZjY2MmVkMjU3NzNjNDE2NGI2YzEzMWFjZGQ5MzE4NA
我认为问题在于,由于我使用的是 client_credentials,因此没有用户,因此 IS_FULLY_AUTHENTICATED 不正确。如果是这种情况,我如何在没有用户的情况下对客户端进行身份验证?
注意: 我也试过删除这些行
access_control:
- { path: ^/api/v1, roles: [ IS_AUTHENTICATED_FULLY ] }
然后尝试使用以下命令访问 Controller 中的客户端详细信息:
$tokenManager = $this->get('fos_oauth_server.access_token_manager.default');
$accessToken = $tokenManager->findTokenByToken(
$this->get('security.token_storage')->getToken()->getToken()
);
$client = $accessToken->getClient();
但后来我得到了错误:
Attempted to call an undefined method named "getToken" of class "Symfony\Component\Security\Core\Authentication\Token\AnonymousToken"
最佳答案
我花了一天时间在同样的情况下挣扎(还有 Symfony 3)。最后,解决方案对我来说非常简单。
在我的 security.yml 中,我定义了以下内容:
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
# form_login: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
# Add your favorite authentication process here
form_login:
provider: userprovider
check_path: /oauth/v2/auth_login_check
login_path: /oauth/v2/auth_login
#anonymous: true #allow all requests
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false
access_control:
- { path: ^/oauth/v2/token, roles: [ IS_AUTHENTICATED_ANONYMOUSLY, IS_AUTHENTICATED_FULLY ] }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
那里有一个小错误。我将防火墙中的主要内容保留为所有网址:
main:
anonymous: ~
# activate different ways to authenticate
通过删除 main 及其 childeren 或定义不覆盖 api 防火墙模式的正确“模式”,我的 OAuth2 服务器正常工作。
后来我发现防火墙的顺序很重要。在主(默认)防火墙规则之前简单地定义 API 防火墙规则就像一个魅力。所以以下是正确的:
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
# Add your favorite authentication process here
form_login:
provider: userprovider
check_path: /oauth/v2/auth_login_check
login_path: /oauth/v2/auth_login
#anonymous: true #allow all requests
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false
main:
anonymous: ~
# activate different ways to authenticate
# http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
# form_login: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
access_control:
- { path: ^/oauth/v2/token, roles: [ IS_AUTHENTICATED_ANONYMOUSLY, IS_AUTHENTICATED_FULLY ] }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
关于symfony - 在 Symfony2 中使用 OAuth2 client_credentials 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330864/