[英]Symfony 4 access_control not working by roles
我嘗試在路線上設置訪問級別,並編寫了這個簡單的security.yaml
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
App\Entity\User: bcrypt
providers:
db_provider:
entity:
class: App\Entity\User
property: mobile
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
form_login:
login_path: verify_token_page
check_path: verify_token_page
default_target_path: panel_index
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/panel, roles: ROLE_SERVICE_MAN }
問題是,當我嘗試訪問localhost:8000 / panel時 ,我得到AccessDeniedHttpException
這是我的用戶角色轉儲
array:1 [▼
0 => "ROLE_SERVICE_MAN"
]
而且我還嘗試不使用角色,而是使用角色或將ROLE_SERVICE_MAN用括號括起來
這是我的PanelController
<?php
namespace App\Controller;
use App\Entity\Car;
use App\Entity\User;
use App\Form\CarType;
use App\Repository\CarRepository;
use App\Repository\RequestRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* @Route("/panel")
*/
class PanelController extends Controller
{
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/",name="panel_index")
*/
public function indexAction()
{
return $this->render('panel/index.html.twig');
}
}
好的,要點1:我發現問題永遠不會嘗試直接從數據庫中更改用戶角色,因為在您登錄緩存或cookie或其他內容時設置了一次(我不知道確切的位置),當您更改時應該重新登錄或添加另一個用戶以設置新角色。
要點2:檢查實體中的關鍵角色(不要添加Role [我的錯誤])。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.