简体   繁体   中英

Symfony 4: cannot access as ROLE_ADMIN

My Roles column value: ["ROLE_ADMIN"]

My Entity/User:

/**
 * @ORM\Column(type="json", nullable=true)
 */
private $roles = [];

public function getRoles() : ?array { 
    $roles = $this->roles; 
    $roles[] = 'ROLE_USER';
    return array_unique($roles); 
}

The $currentUser->getRoles(); gives me this in var_dump :

array(2) { [0]=> string(10) "ROLE_ADMIN" [1]=> string(9) "ROLE_USER" }

My security.yaml:

access_control:
- { path: ^/admin, roles: ROLE_ADMIN }

The Profiler Toolbar tells me that I'm logged in.

But still I see the message:

Symfony\Component\HttpKernel\Exception\
AccessDeniedHttpException

But when I change my security.yaml to:

access_control:
- { path: ^/admin, roles: ROLE_USER }

Then the Exception disappears.

Did I miss something?

Try to logout and login, symfony keeps roles in remember me token, you can check your currently assigned roles in profiler, security tab. Most probably role is not in remember me cookie, that's why it doesnt let you access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM