Symfony 4 аутентифицированный токен пользователя потерян - PullRequest
0 голосов
/ 13 октября 2018

У меня проблема с логином.Когда я ввожу учетные данные пользователя и нажимаю кнопку входа, он создает токен пользователя и перенаправляет его на главную страницу, но когда запрос получает главную страницу, токен пользователя не существует ... Вот данные из файла dev.log

request.INFO: Matched route "login_check". {"route":"login_check","route_parameters":{"_route":"login_check","_controller":"App\\Controller\\Auth\\LoginController::login"},"request_uri":"http://localhost:8082/login","method":"POST"} []
security.INFO: User has been authenticated successfully. {"username":"jac@jack.com"} []
security.DEBUG: Fallback to the default authentication success handler. [] []
security.DEBUG: Clearing remember-me cookie. {"name":"REMEMBERME"} []
security.DEBUG: Remember-me was requested; setting cookie. [] []
security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []
request.INFO: Matched route "main". {"route":"main","route_parameters":{"_route":"main","_controller":"App\\Controller\\MainController::index"},"request_uri":"http://localhost:8082/","method":"GET"} []
security.DEBUG: Read existing security token from the session. {"key":"_security_main","token_class":"Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken"} []
security.WARNING: Username could not be found in the selected user provider. {"username":null,"provider":"App\\Security\\UserProvider"} []
security.DEBUG: Remember-me cookie detected. [] []
security.INFO: User for remember-me cookie not found. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException(code: 0): Username \"jac@jack.com\" does not exist. at /opt/aicty/src/Security/UserProvider.php:52)"} []
security.DEBUG: Clearing remember-me cookie. {"name":"REMEMBERME"} []
security.INFO: Populated the TokenStorage with an anonymous Token. [] []
security.DEBUG: Access denied, the user is not fully authenticated; redirecting to authentication entry point. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException(code: 403): Access Denied. at /opt/aicty/vendor/symfony/security/Http/Firewall/AccessListener.php:68)"} []
security.DEBUG: Calling Authentication entry point. [] []
request.INFO: Matched route "show_login". {"route":"show_login","route_parameters":{"_route":"show_login","_controller":"App\\Controller\\Auth\\LoginController::showLogin"},"request_uri":"http://localhost:8082/login","method":"GET"} []
security.INFO: Populated the TokenStorage with an anonymous Token. [] []
request.INFO: Matched route "_wdt". {"route":"_wdt","route_parameters":{"_route":"_wdt","_controller":"web_profiler.controller.profiler::toolbarAction","token":"e96cb7"},"request_uri":"http://localhost:8082/_wdt/e96cb7","method":"GET"} []

А вот и моя служба безопасности.Я взял класс User и класс UserProvider из документации Symfony.

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt
            cost: 12
    providers:
        japi:
            id: App\Security\UserProvider
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: ~
            provider: japi
            simple_form:
                login_path: show_login
                check_path: login_check
                username_parameter: email
                password_parameter: password
                authenticator: App\Security\Authenticator
                remember_me: true
                provider: japi

Ответы [ 2 ]

0 голосов
/ 31 августа 2019

В моем случае я изменяю сущность пользователя и забыл сгенерировать или обновить таблицу в sql, вы можете обновить или сгенерировать таблицу, используя следующую команду.

php bin/console doctrine:schema:update --force

Или

 php bin/console make:migration
 php bin/console doctrine:migrations:migrate
0 голосов
/ 14 октября 2018

Используете ли вы https://github.com/lexik/LexikJWTAuthenticationBundle?

Конфигурация проста:

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
      webservice:
        id: App\Security\UserProvider

    encoders:
      App\Entity\User:
        algorithm: bcrypt
        cost: 12

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
          pattern:  ^/login
          stateless: true
          anonymous: true
          json_login:
            check_path:               /login_check
            success_handler:          lexik_jwt_authentication.handler.authentication_success
            failure_handler:          lexik_jwt_authentication.handler.authentication_failure
...