我正在关注Symfony cookbook并且在"避免常见的陷阱"它说:
此外,如果防火墙不允许匿名用户(无
anonymous
密钥),则您需要创建一个允许匿名用户登录页面的特殊防火墙firewalls: # order matters! This must be before the ^/ firewall login_firewall: pattern: ^/login$ anonymous: ~ secured_area: pattern: ^/ form_login: ~
所以我实现了我的security.yml
文件,如下所示:
security:
providers:
in_memory:
memory:
users:
ryan:
password: pass1
roles: ROLE_USER
admin:
password: pass2
roles: ROLE_ADMIN
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
anonymous: ~
pattern: ^/login$
context: foo
main:
pattern: ^/
form_login:
login_path: /login
check_path: /login
default_target_path: /secret
logout:
path: /logout
target: homepage
context: foo
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secret, roles: ROLE_ADMIN }
问题在于,每当我尝试登录时,我都会被重定向到登录页面而我从未接受过身份验证。我还尝试在两个防火墙之间共享上下文(即它们使用相同的context
),但没有产生任何积极影响。
此配置有什么问题?
这是我的login.html.twig
模板:
<html>
<body>
{% if error %}
<div>{{ error.messageKey | trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit" >Login</button>
</form>
</body>
</html>
答案 0 :(得分:0)
我不知道为什么(如果有人这样做,欢迎他们添加其他答案),但如果我将form_login
元素同时放在login_firewall
和<{strong}在main
内,然后它允许用户进行身份验证。
换句话说,工作security.xml
配置是:
(修改:main -> form_login
也可以null
)
security:
providers:
in_memory:
memory:
users:
ryan:
password: pass1
roles: ROLE_USER
admin:
password: pass2
roles: ROLE_ADMIN
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
anonymous: ~
pattern: ^/(login|logout)?$
context: foo
form_login:
login_path: /login
check_path: /login
logout:
path: /logout
target: homepage
main:
pattern: ^/
form_login: ~
context: foo
access_control:
- { path: /(login|logout)?$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /secret, roles: ROLE_ADMIN }