Authentication does not work in Symfony2

I am having authentication issues, but this only happens in special circumstances. Authentication is done through a third-party API, so I wrote my own class of service provider, and inside this class there is code that synchronizes data between the API and Symfony, as part of the synchronization process that determines which roles a user should have. After that, he establishes the relationship between the roles and the user through the ManyToMany relationship.

The getRoles () method in my User object gets role objects from the database and turns them into an array of strings, role names come from my database, and it all starts with ROLE _.

If I log into the system with an account that should not have additional roles, it works fine, but if I log in to the account, which should have roles, I will simply send it back to the login screen without an error message.

I checked the log and saw these entries:

security.INFO: User " test105@example.com " has been authenticated successfully [] [] event.DEBUG: Notified event "security.interactive_login" to listener "Pogo\MyBundle\Listener\LoginListener::onSecurityInteractivelogin". [] [] event.DEBUG: Listener "Symfony\Component\Security\Http\Firewall::onKernelRequest" stopped propagation of the event "kernel.request". [] [] event.DEBUG: Listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener" was not called for event "kernel.request". [] [] event.DEBUG: Listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener" was not called for event "kernel.request". [] [] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse". [] [] security.DEBUG: Write SecurityContext in the session [] [] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] [] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\SecurityBundle\EventListener\ResponseListener::onKernelResponse". [] [] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] [] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] [] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] [] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] [] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onEarlyKernelRequest". [] [] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] [] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] [] security.INFO: Populated SecurityContext with an anonymous Token [] [] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException". [] [] security.DEBUG: Access denied (user is not fully authenticated); redirecting to authentication entry point [] [] security.DEBUG: Calling Authentication entry point [] [] 

I don’t understand how it can be authenticated from above, and then when it checks the firewall, it detects an anonymous token, so it probably sends me back to the login screen.

Settings for my / access _control firewall:

 firewalls: public: pattern: /.* anonymous: true tessitura_login: login_path: /account/login check_path: /secure/login_check logout: path: /secure/logout target: / access_control: - { path: ^/secure/.*, role: ROLE_USER } - { path: ^/admin.*, role: ROLE_ADMIN } - { path: ^/account/login/?, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY } 

Any help with this would be appreciated commendably, I spent several hours on it now and completely lost consciousness.

+7
source share
6 answers

Got this problem with a silent crash when using the phone number as the username and did not specify the username property in the refreshUser() method, which should be:

 public function refreshUser(UserInterface $customer) { $class = get_class($customer); if( !$this->supportsClass($class) ) { throw new UnsupportedUserException("Instances of \"{$class}\" are not supported"); } return $this->loadUserByUsername($customer->getPhoneNumber()); // <-- This is it! } 

I think that I'm not the only one who missed it, can help.

+2
source

The corrupted session store caused this for me. I used PdoSessionHandler and unfortunately did not give clear error messages or logs.

+1
source

I experienced the same thing. When my users log in, I check what role it performs with a few statements like this:

 if(true === $this->get('security.context')->isGranted('ROLE_MANAGER')){ //return redirect } if(true === $this->get('security.context')->isGranted('ROLE_USER')){ //return redirect } //throw error 

From time to time, some users receive an error message. I believe this is for the same reason. The user is somehow authenticated, but has not received his role.

I can not reproduce the problem myself. I just heard error messages from my users.

0
source

I experienced the same thing. And for me it was because the / tmp section was full, so the session can be stored on the server side and redirect avter to nex

0
source

I had the same issue when logging into my system where sessions are configured to be stored in memcache but memcached was not started. As stated above, unfortunately, this did not give a better error message.

Hope this helps someone save some time; -)

0
source

I had the same problem with user login. I used sonata administration pack and I also used database session with PdoSessionHandler

 session.handler.pdo: class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler arguments: ["@pdo", %pdo.db_options%] 

The first release that I received when I create a group with many roles / permissions, the data is truncated in the field, so I change the field of my roles using longtext and changed ROW_FORMAT=COMPRESSED

 ALTER TABLE `fos_group` CHANGE `roles` `roles` LONGTEXT NOT NULL COMMENT '(DC2Type:array)'; ALTER TABLE `fos_group` ENGINE=INNODB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8; 

It does the job and saves all roles / permissions in the field as a full serialized string. But the user was unable to log in without an error message, then I look at the logs created by symfony in app/logs dir, it has

user authenticated successfully

and then redirect to the control panel, but from the dashboard the logs created as

access denied (user is not fully authenticated)

the reason was that session data is truncated in the session table, so I also modify the session table, and this does the job

 ALTER TABLE `session` CHANGE `session_value` `session_value` LONGTEXT NOT NULL; ALTER TABLE `session` ENGINE=INNODB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8; 

I also updated my.ini file and changed the file format to Barracuda by default, antelop file format

 [mysqld] innodb_file_per_table innodb_file_format = Barracuda 
0
source

All Articles