Symfony Security FOSOAuthServerBundle public and private routes

谁说胖子不能爱 提交于 2019-12-12 04:23:11

问题


I am using the FOSOAuthBundle for my REST application

I would like most of my routes to require authorization however there are a few that should have public access

I have the following in my security.yml:

security:
encoders:
    FOS\UserBundle\Model\UserInterface: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    oauth_token:
        pattern:    ^/login
        security:   false

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

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: [ IS_AUTHENTICATED_FULLY ] }

For example:

I have a Products Entity and Controller

I would like the CRUD operations to be private except for Read

So: POST, PUT, DELETE on /products(/:id) should be private while GET should be public.

I have tried adding the following to the access_control:

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/products$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: [ IS_AUTHENTICATED_FULLY ] }

I thought this would open up all methods on /products but I get the error:

{
  "error": "access_denied",
  "error_description": "OAuth2 authentication required"
} 

I have many entities and controllers I am trying to apply this to.

How would I go about opening up specific routes (including the method requirements)?


回答1:


You can make new firewall with regex and set it like this. You have to put it in front of your api firewall in order to match this regex first.

api_anonym_area:
      pattern: (^/api/products/.*)
      methods: [GET]
      security: false

Or you can make it

api_anonym_area:
      pattern: (^/api/products/.*)
      methods: [GET]
      anonymous: true

with

access_control:
- { path: ^/api/products/.*, role: IS_AUTHENTICATED_ANONYMOUSLY}

In first case you wont have token, in in second case you will have token (its good when you expect authenticated or anonymous users to come).




回答2:


To achieve this, best way would be to code the permissions in the controllers, I don't think this is possible via the security.yml configuration.

You should remove:

 - { path: ^/, roles: [ IS_AUTHENTICATED_FULLY ] }

And manage permissions inside the controller actions, for example (taken from symfony documentation at http://symfony.com/doc/current/security.html)

public function updateProduct($id)
{
    // The second parameter is used to specify on what object the role is tested.
    $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');

    // Old way :
    // if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
    //     throw $this->createAccessDeniedException('Unable to access this page!');
    // }

    // ...
}


来源:https://stackoverflow.com/questions/41257798/symfony-security-fosoauthserverbundle-public-and-private-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!