首页 > 解决方案 > 无法在 symfony 中登录用户

问题描述

我对 Symfony 完全陌生,我正在尝试解决登录问题。我无法解决此问题,因为我可以获取登录用户信息,BundleAppBundle无法在其他捆绑包中获取此信息,例如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

标签: phpsymfonyauthentication

解决方案


推荐阅读