我正在学习Symfony 4,并尝试按照Symfony 4文档创建一个应用程序。
我遵循此article来制作管理员角色。
但是当我访问路径/admin
时,它总是"Access Denied."
我在Symfony Documentation页面和StackOverflow中阅读了很多文章,但找不到解决方法。这是我的设置
//config/packges/security.yaml
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: App\Entity\User
in_memory:
memory:
users:
admin:
password: admin123
roles: 'ROLE_ADMIN'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
http_basic: ~
provider: our_db_provider
form_login:
login_path: login
check_path: login
default_target_path: index
always_use_default_target_path: true
logout:
path: /logout
target: /index
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
这是Symfony Debug日志:
未捕获的PHP异常Symfony \ Component \ Security \ Core \ Exception \ AccessDeniedException:" Access Denied。"在C:\ xampp \ htdocs \ aimer-mvc \ vendor \ symfony \ security \ Http \ Firewall \ AccessListener.php第68行
请帮帮我。非常感谢你!
答案 0 :(得分:0)
如果您想使用 admin / admin123 测试您的应用,则必须在防火墙中允许in_memory
提供商:
仅适用于http_basic:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: App\Entity\User
in_memory:
memory:
users:
admin:
password: admin123
roles: 'ROLE_ADMIN'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
http_basic:
provider: in_memory
provider: our_db_provider
form_login:
login_path: login
check_path: login
default_target_path: index
always_use_default_target_path: true
logout:
path: /logout
target: /index
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
for form_login:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
both_providers:
chain:
providers: [in_memory, our_db_provider]
our_db_provider:
entity:
class: App\Entity\User
in_memory:
memory:
users:
admin:
password: admin123
roles: 'ROLE_ADMIN'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
http_basic: ~
provider: both_providers
form_login:
login_path: login
check_path: login
default_target_path: index
always_use_default_target_path: true
logout:
path: /logout
target: /index
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
有关示例,请参阅https://symfony.com/doc/current/security/multiple_user_providers.html