我试着做使用symfony的内存用户提供程序使用官方文档登录。
所以我让我的用户使用symfony console make:user,我允许默认设置,并选择"no“来使用数据库。
我在security.yaml和一些配置中添加了我的管理员:
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: "auto"
providers:
app_user_provider:
id: App\Security\UserProvider
backend_users:
memory:
users:
my:
{
password: "$2y$…B3", // i put my true bcrypt generated password
roles: ["ROLE_ADMIN", "ROLE_SUPER_ADMIN"],
}
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/profile, roles: ROLE_USER }
when@test:
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
algorithm: bcrypt
cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon我进行登录操作:
#[Route('/{_locale<%app.supported_locales%>}/login', name: 'my_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('@MyCore/Core/login.html.twig', [
'controller_name' => 'CoreController',
'last_username' => $lastUsername,
'error' => $error,
]);
}与相关表单一起使用,如文档中所示:
{% extends '@MyCore/layout-default.html.twig' %}
{% block body %}
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('my_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"/>
{# If you want to control the URL the user is redirected to on success
<input type="hidden" name="_target_path" value="/account"/> #}
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
<button type="submit">login</button>
</form>
{% endblock %}和…什么都没发生。当我尝试登录时,使用真正的用户或随机的假条目,没有错误,没有日志记录,只是我回到"/login“,好像什么都没发生一样。
我是不是忘了什么?
编辑
是的,我只是忘了一些东西:在security.yaml中定义security.yaml(以我为耻,我可以删除,但是,也许有人会做同样的错误,然后搜索…):
firewalls:
main:
form_login:
login_path: my_login
check_path: my_login
logout:
path: my_logout发布于 2022-09-23 16:56:29
没有配置用于使用backend_users用户提供程序的防火墙。
如果要将内存中的用户用于main防火墙,请按以下方式配置它:
security:
# [..]
providers:
backend_users:
memory:
users: [] # <- add your users here
firewalls:
main:
lazy: true
provider: backend_users # <- use the provider here发布于 2022-09-25 13:11:38
您正在使用app_user_provider,但您需要同时使用两个提供程序,app_user_provider和backend_users提供程序。要构建它,您需要使用提供者链(塞福尼医生):
# config/packages/security.yaml
security:
# ...
providers:
backend_users:
# ...
app_user_provider:
# ...
all_users:
chain:
providers: ['backend_users', 'app_user_provider']
firewalls:
main:
lazy: true
provider: all_users # all_users => provider chain发布于 2022-09-26 12:53:53
你的问题来自于你的安全问题。如果您需要添加两个用户,那么您应该使用一个链,看看Symfony文档。
https://stackoverflow.com/questions/73819417
复制相似问题