Symfony Security: Auth with session or oauth

拜拜、爱过 提交于 2019-11-29 12:01:56

I solved by duplicating the routes of the api controllers, so that I have a route /api/method which relies on OAuth2, and a /webapi/method route which relies on the standard (main) firewall:

In security.yml:

firewalls:
    api:
        pattern:    ^/api
        fos_oauth:  true
        stateless:  true

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

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: /login
            check_path: /login_check
        logout:       true
        anonymous:    true

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

In routing.yml:

acme_api:
    type: rest 
    prefix: /
    resource: "@AcmeBundle/Resources/config/routing_api.yml"

In routing_api.yml:

# REST API - OAUTH Access
acme_api_users:
    resource: AcmeBundle\Controller\UsersController
    type:     rest
    defaults: {_format: json}
    prefix:   /api
    name_prefix:  api_

# REST API - Frontend Client Access 
acme_webapi_users:
    resource: AcmeBundle\Controller\UsersController
    type:     rest
    defaults: {_format: json}
    prefix:   /web-api
    name_prefix:  webapi_
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!