File tree Expand file tree Collapse file tree 1 file changed +47
-1
lines changed Expand file tree Collapse file tree 1 file changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -465,14 +465,60 @@ security:
465
465
pattern : ^/api
466
466
fos_oauth : true
467
467
stateless : true
468
+ anonymous : false # can be omitted as its default value
468
469
469
470
access_control :
470
- # You can omit this if /api can be accessed both authenticated and anonymously
471
471
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
472
472
` ` `
473
473
474
474
The URLs under ` /api` will use OAuth2 to authenticate users.
475
475
476
+ # ### Anonymous access
477
+
478
+ Sometimes you need to allow your api to be accessed without authorization. In order to do that lets adjust
479
+ above-mentioned example configuration.
480
+
481
+ ` ` ` yaml
482
+ # app/config/security.yml
483
+ security:
484
+ firewalls:
485
+ oauth_token:
486
+ pattern: ^/oauth/v2/token
487
+ security: false
488
+
489
+ oauth_authorize:
490
+ pattern: ^/oauth/v2/auth
491
+ # Add your favorite authentication process here
492
+
493
+ api:
494
+ pattern: ^/api
495
+ fos_oauth: true
496
+ stateless: true
497
+ anonymous: true # note that anonymous access is now enabled
498
+
499
+ # also note absence of "access_control" section
500
+ ` ` `
501
+
502
+ From now on all of your api resources can be accessed without authorization. But what if one or more of them should be
503
+ secured anyway or/and require presence of authenticated user? It's easy! You can do that manually by adding few lines of
504
+ code at the beginning of all of your secured actions like in the example below :
505
+
506
+ ` ` ` php
507
+ // [...]
508
+ use Symfony\C omponent\S ecurity\C ore\E xception\A ccessDeniedException;
509
+
510
+ class YourApiController extends Controller
511
+ {
512
+ public function getSecureResourceAction()
513
+ {
514
+ # this is it
515
+ if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
516
+ throw new AccessDeniedException();
517
+ }
518
+
519
+ // [...]
520
+ }
521
+ ` ` `
476
522
477
523
# ## Step 5: Configure FOSOAuthServerBundle
478
524
You can’t perform that action at this time.
0 commit comments