1

我创建了一个 REST API,我实际上是在尝试使用 FOSOAuthServerBundle 来保护它。

我创建了生成新客户端的路由newclient,现在我想只允许“管理员”访问这个 url。

我想有可能做到这一点,scopes但我不知道怎么做。

这是我的security.yml:

security:

    providers:
        user_provider:
            id: user_provider

    firewalls:
        doc:
            pattern:    ^/doc
            security:   false

        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        oauth_authorize:
            pattern:    ^/oauth/v2/auth
            provider: user_provider
            anonymous:  true

        api:
            pattern:    ^/
            fos_oauth:  true
            stateless:  true
            anonymous:  false

    access_control:
        - { path: ^/, roles: ROLE_CLIENT }
        - { path: ^/newclient, roles: ROLE_ADMIN }

我的 config.yml

fos_oauth_server:

    db_driver: orm 
    client_class: WS\RESTBundle\Entity\Client
    access_token_class: WS\RESTBundle\Entity\AccessToken
    refresh_token_class: WS\RESTBundle\Entity\RefreshToken
    auth_code_class: WS\RESTBundle\Entity\AuthCode

有小费吗 ?

4

1 回答 1

0

实际上使用范围进行设计并不是最好的方法,而且捆绑包不支持它:https ://github.com/FriendsOfSymfony/FOSOAuthServerBundle/issues/231

我使用了用户模型,添加了一个“角色”方法并检查当前用户的角色是否足以访问路由。

这是一段代码

//Get the current user, check if it's an admin
$token = $this->container->get('security.context')->getToken()->getToken();

$accessToken = $this->container
->get('fos_oauth_server.access_token_manager.default')
->findTokenBy(array('token' => $token));

$client = $accessToken->getClient();


if ($client->getRole() == 'admin') {

...
}

不知道这是否是最好的方法,欢迎任何建议!

于 2014-05-14T08:18:03.083 回答