php - 无法在 symfony 中登录用户
问题描述
我对 Symfony 完全陌生,我正在尝试解决登录问题。我无法解决此问题,因为我可以获取登录用户信息,Bundle
但AppBundle
无法在其他捆绑包中获取此信息,例如WebsiteBundle
. 我尝试了几种来自互联网的方法,但没有任何效果。像这个
这是我的登录方法
public function login(Request $request)
{
$token = $request->get('token');
$user = $this->getDoctrine()->getRepository(User::class)->findOneByToken($token);
if (!($user instanceof User))
{
return new JsonResponse(['status' => 'error', 'message' => 'Incorrect username or password provided.']);
}
// Here, "website" is the name of the firewall in your security.yml
$userToken = new UsernamePasswordToken($user, $user->getPassword(), "website", $user->getRoles());
// For older versions of Symfony, use security.context here
$this->get("security.token_storage")->setToken($userToken);
$this->get('session')->set('_security_main', serialize($userToken));
// Fire the login event
// Logging the user in above the way we do it doesn't do this automatically
$event = new InteractiveLoginEvent($request, $userToken);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
$this->get('session')->save();
$this->addFlash('success', 'Login successful.');
return new JsonResponse(['status' => 'ok', 'message' => 'Login successful.']);
}
事件控制台说用户没有登录。请看截图。
更新
以下是我的security.yml
文件内容
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/security.html
security:
encoders:
AppBundle\Entity\User: sha512
AppBundle\Entity\Client: sha512
AppBundle\Entity\Admin: sha512
Symfony\Component\Security\Core\User\User: sha512
# http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
chain_provider:
chain:
providers: [api_user_provider, webservice, admin_provider]
api_user_provider:
entity:
class: AppBundle:User
property: token
webservice:
entity:
class: AppBundle:Client
property: email
admin_provider:
entity:
class: AppBundle:Admin
property: email
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/(crm/|ajax/)
anonymous: ~
logout:
path: /crm/logout
target: /crm/login/
stateless: false
# activate different ways to authenticate
# http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
#http_basic: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
form_login:
login_path: /crm/login/
check_path: /crm/login/
failure_path_parameter: /crm/login/
always_use_default_target_path: true
default_target_path: /crm/loginRedirect/
username_parameter: username
password_parameter: password
require_previous_session: false
logout: true
access_denied_url: access_denied
api:
pattern: ^/api/
anonymous: ~
guard:
authenticators:
- app.token_authenticator
stateless: false
解决方案
推荐阅读
- python - 禁用全屏 kivymd
- html - 高度为 100% 时如何保持纵横比?
- reactjs - 需要添加头像和用户名才能发帖
- bash - #!/bin/bash set -o pipefail if [[ $# -ne 1 ]] if [ $? -eq 0 ] 在 shell 脚本中的意思
- php - 如何提高 MySQL 数据库的性能?
- html - 我的 css flexbox 不能在任何浏览器上运行
- javascript - Reactjs 错误:未捕获(承诺中)错误:请求失败,状态码 401
- php - cURL 错误 60:SSL 证书问题:在 php.ini 文件中添加证书文件 xxx.pem 路径后无法获取本地颁发者证书
- java - 使用带有 WSDL 的 SOAP WebService - Spring Boot 2
- linux - 创建文件或将文件从 docker 容器复制到主机