# When I set 'deny\_by\_default' ('zf-mvc-auth' component) to true, I get 403 "Forbidden" on all pages of the site

**URL:** <https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145>\
**Category:** API Tools\
**Tags:** apigility\
**Created:** [August 8, 2019, 8:02pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145 "2019-08-08T20:02:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![testuser](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@testuser](https://discourse.laminas.dev/u/testuser)\
**Post date:** [August 8, 2019, 8:02pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/1 "2019-08-08T20:02:05Z")

</div>

I am trying to use oauth2 with apigility within zf3 project too. At current moment I have faced with next situation: when I am setting ‘deny\_by\_default’ to true, I get 403 “Forbiddeb” on all pages of the site even at non API urls. I mean, the home page of my site shows 403 too.

This is my config:

```
'zf-mvc-auth' => [
        'authentication' => [
            'map' => [
                '\\Admin\\Module\\User\\V1' => 'oauth2_pdo',
            ],
        ],
        'authorization' => [
            'deny_by_default' => true,
            '\\Admin\\Module\\User\\V1\\Rest\\Item\\Controller' => [
                'collection' => [
                    'GET' => true,
                    'POST' => true,
                    'PUT' => false,
                    'PATCH' => false,
                    'DELETE' => false,
                ],
                'entity' => [
                    'GET' => true,
                    'POST' => false,
                    'PUT' => true,
                    'PATCH' => true,
                    'DELETE' => true,
                ],
            ],
        ],
    ],

```

I have apigility admin at the another project and generate there what I need. After that I copy generated config to my project.  
It looks like I have missed something. When I remove deny\_by\_default or set it to false everything works as before but my API is allowed to see without authentication.

I have this json response on all pages (at home page too):

```
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Forbidden",
"status":403,
"detail":"Forbidden"
}

```

How to fix this?

---

<div class="post-metadata">

**Author:** ![TomHAnderson](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomhanderson/32/150_2.png) [@TomHAnderson](https://discourse.laminas.dev/u/TomHAnderson)\
**Post date:** [August 12, 2019, 8:52pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/2 "2019-08-12T20:52:24Z")

</div>

This article may help: [https://blog.tomhanderson.com/2017/10/using-zf-mvc-auth-for-custom.html](https://blog.tomhanderson.com/2017/10/using-zf-mvc-auth-for-custom.html)

It’s not as easy as just a config variable but once all your ACL is in place you’ll have better control of your permissions.

---

<div class="post-metadata">

**Author:** ![testuser](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@testuser](https://discourse.laminas.dev/u/testuser)\
**Post date:** [August 12, 2019, 9:09pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/3 "2019-08-12T21:09:36Z")

</div>

I followed by this tutorial [https://apigility.org/documentation/auth/user-differentiation](https://apigility.org/documentation/auth/user-differentiation) but your example contains doctrine which is exactly what I need. Thank you!

Also I have figured out why I had 403 everywhere. I had to add acl resources to the authorization listener.

---

<div class="post-metadata">

**Author:** ![testuser](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@testuser](https://discourse.laminas.dev/u/testuser)\
**Post date:** [August 16, 2019, 11:14am UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/4 "2019-08-16T11:14:25Z")

</div>

I have fixed 403 at nonAPI pages but still have 403 at all REST API links.

---

<div class="post-metadata">

**Author:** ![testuser](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@testuser](https://discourse.laminas.dev/u/testuser)\
**Post date:** [August 22, 2019, 10:44pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/5 "2019-08-22T22:44:44Z")

</div>

Tom, can you clear for me one moment from your blog post at [https://blog.tomhanderson.com/2017/10/using-zf-mvc-auth-for-custom.html](https://blog.tomhanderson.com/2017/10/using-zf-mvc-auth-for-custom.html)?  
At first you have wrote this:

```
'zf-mvc-auth' => array(
        'authentication' => array(
            'adapters' => array(
                'user' => [
                    'adapter' => 'User\\Authentication\\Adapter\\DoctrineAdapter',
                    'storage' => [
                        'Zend\Authentication\Storage\Session',
                    ],
                ],
            ),
        ),
    ),

```

where the name of adapter is “DoctrineAdapter”. Later at that post you have mentioned this:

```
final class AuthenticationAdapter implements
    AdapterInterface,
    ObjectManagerAwareInterface
{

```

Is the “AuthenticationAdapter” the same as “DoctrineAdapter” from config?

Also, at this page [https://apigility.org/documentation/auth/user-differentiation](https://apigility.org/documentation/auth/user-differentiation) have mentioned about “final class SessionAdapter” but later at that docs is this code:

```
// Add Authentication Adapter for session
        $defaultAuthenticationListener = $container->get(DefaultAuthenticationListener::class);
        $defaultAuthenticationListener->attach(new Authentication\AuthenticationAdapter());

```

Is “SessionAdapter” and “AuthenticationAdapter” the same thing? Because, I used it as same and rename second class to first class.

---

<div class="post-metadata">

**Author:** ![TomHAnderson](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomhanderson/32/150_2.png) [@TomHAnderson](https://discourse.laminas.dev/u/TomHAnderson)\
**Post date:** [February 6, 2020, 12:59pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/6 "2020-02-06T12:59:49Z")

</div>

@testuser I don’t get what you’re asking. If you’re still working on this add some links to the code with your question.

---

<div class="post-metadata">

**Author:** ![testuser](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@testuser](https://discourse.laminas.dev/u/testuser)\
**Post date:** [February 6, 2020, 1:25pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/7 "2020-02-06T13:25:30Z")

</div>

Hi, Tom! Thank you for replaying!  
I have figured out this issue.

---

<div class="post-metadata">

**Author:** ![xerkus](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xerkus/32/22_2.png) [@xerkus](https://discourse.laminas.dev/u/xerkus)\
**Post date:** [February 9, 2020, 5:27am UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/8 "2020-02-09T05:27:19Z")

</div>

@testuser it is customary to describe what the issue was and how you solved it for those who come here later looking for help

---

<div class="post-metadata">

**Author:** ![testuser](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@testuser](https://discourse.laminas.dev/u/testuser)\
**Post date:** [February 9, 2020, 10:35am UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/9 "2020-02-09T10:35:39Z")

</div>

@xerkus , I don’t remember exactly but If I am not mistaken the issue was related to that fact that ‘deny\_by\_default’ and ‘zf-mvc-auth’ was related only to guest users. And I was trying to config apigility for admin user.

I have created additional authorization listener and added something like this:

```
    $entityMehtods = [
        'GET',
        'PATCH',
        'PUT',
        'DELETE',
    ];
    $authorization->allow('admin', '\\Admin\\Module\\News\\V1\\Rest\\Item\\Controller' . '::entity', $entityMehtods);

```

This works for me.

---

<div class="post-metadata">

**Author:** ![api](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@api](https://discourse.laminas.dev/u/api)\
**Post date:** [March 10, 2020, 10:35am UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/10 "2020-03-10T10:35:51Z")

</div>

could you please share your solution.

i have followed both tutorials:

[https://api-tools.getlaminas.org/documentation/auth/user-differentiation](https://api-tools.getlaminas.org/documentation/auth/user-differentiation)

> **[Using zf-mvc-auth for custom authentication](https://blog.tomhanderson.com/2017/10/using-zf-mvc-auth-for-custom.html)**
>
> The zfcampus/zf-mvc-auth module is more than meets the eye.  Not only can this module use the built-in authentication types of Basic, Diges...

And could not make it work. thanks in advance

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [March 10, 2020, 2:01pm UTC](https://discourse.laminas.dev/t/when-i-set-deny-by-default-zf-mvc-auth-component-to-true-i-get-403-forbidden-on-all-pages-of-the-site/1145/11 "2020-03-10T14:01:53Z")

</div>

The `deny_by_default` flag, when set to boolean `true`, does exactly what it says: it denies access to API endpoints un-identified users. You MUST pass credentials with your request in order to access any API endpoints. (Non-API endpoints are not affected.)

Your ACL rules may specify further restrictions, but this is the first line in making your API private by default. If you don’t want that behavior, toggle the flag back to `false`, and then restrict only the endpoints that will require a credentialed user.
