title: Token was deauthenticated after trying to refresh it
body: <p>I updated my Symfony environment form 3.3 to 4.0. After the update I have problems with the login (user provided by database). When I submit the login form, I just got right back to the login form without any error message. When I use invalid credentials, I got the corresponding error message. Here is the log after trying to login. The login with the "in_memory" user provider is working. Do you need more information? </p>
  
  <pre><code>[2017-12-06 13:57:05] security.INFO: User has been authenticated successfully. {"username":"***"} []
  [2017-12-06 14:22:39] doctrine.DEBUG: "START TRANSACTION" [] []
  [2017-12-06 13:57:05] security.DEBUG: Read existing security token from the session. {"key":"_security_secured_area","token_class":"Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken"} []
  [2017-12-06 13:57:05] doctrine.DEBUG: SELECT t0.username AS username_1, t0.password AS password_2, t0.email AS email_3, t0.email_new AS email_new_4, t0.first_name AS first_name_5, t0.last_name AS last_name_6, t0.is_active AS is_active_7, t0.email_confirmed AS email_confirmed_8, t0.shibboleth_state AS shibboleth_state_9, t0.shibboleth_hash AS shibboleth_hash_10, t0.shibboleth_persistent_id AS shibboleth_persistent_id_11, t0.confirmation_email_send AS confirmation_email_send_12, t0.last_login AS last_login_13, t0.expires AS expires_14, t0.session_id AS session_id_15, t0.id AS id_16, t0.hidden AS hidden_17, t0.deleted AS deleted_18, t0.created AS created_19, t0.modified AS modified_20, t0.sorting AS sorting_21, t0.salutation_id AS salutation_id_22, t0.creator_id AS creator_id_23, t0.modifier_id AS modifier_id_24 FROM User t0 WHERE t0.id = ? AND ((t0.deleted = 0)) [2] []
  [2017-12-06 13:57:05] security.DEBUG: Token was deauthenticated after trying to refresh it. {"username":"user","provider":"Symfony\\Component\\Security\\Core\\User\\ChainUserProvider"} []
  [2017-12-06 13:57:05] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
  [2017-12-06 13:57:05] 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 /vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php:68)"} []
  [2017-12-06 13:57:05] security.DEBUG: Calling Authentication entry point. [] []
  </code></pre>
  
  <p>Entity\User:</p>
  
  <pre><code>class User extends EntitySuperclass implements AdvancedUserInterface, \Serializable
  {
      /**
       * @ORM\Column(type="string")
       */
      private $username;
  
      /**
       *
       * @Assert\Length(max=4096,groups={"account_complete","account_password","user"})
       * @Assert\Length(min = 8,groups={"account_complete","account_password","user"}, minMessage="user.password_length")
       */
      private $plainPassword;
  
      /**
       * The below length depends on the "algorithm" you use for encoding
       * the password, but this works well with bcrypt.
       *
       * @ORM\Column(type="string", length=64)
       */
      private $password;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank(groups={"account_register","user"})
       * @Assert\Email(
       *      groups = {"account_register", "account","user"},
       *      strict = true,
       *      checkMX = true
       * )
       */
      private $email;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $emailNew = '';
  
      /**
       * @ORM\ManyToOne(targetEntity="Salutation")
       * 
       */
      private $salutation;
  
      /**
       * @ORM\Column(type="string")
       * @Assert\NotBlank(groups={"account_complete","user"})
       * @Assert\Regex(pattern = "/^[a-zA-ZäöüÄÖÜß0-9 ]+$/",groups={"account_complete","user"}, message="user.first_name.regex")
       */
      private $firstName;
  
      /**
       * @ORM\Column(type="string")
       * @Assert\NotBlank(groups={"account_complete","user"})
       * @Assert\Regex(pattern = "/^[a-zA-ZäöüÄÖÜß0-9 ]+$/",groups={"account_complete","user"}, message="user.last_name.regex")
       */
      private $lastName;
  
      /**
       * @ORM\Column(name="is_active", type="boolean")
       */
      private $isActive = false;
  
      /**
       * @ORM\Column(name="email_confirmed", type="boolean")
       */
      private $emailConfirmed = false;
  
      /**
       * @ORM\Column(type="integer")
       */
      private $shibbolethState = 0;
  
      /**
       * @ORM\Column(type="string")
       */
      private $shibbolethHash = '';
  
      /**
       * @ORM\Column(type="string")
       */
      private $shibbolethPersistentId = '';
  
      /**
       * @ORM\ManyToMany(targetEntity="UserGroup")
       * @ORM\JoinTable(name="User_UserGroup",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
       *      )
       */
      private $userGroups;
  
      /**
       * @ORM\Column(type="integer")
       */
      private $confirmationEmailSend;
  
      /**
       * @ORM\Column(type="integer")
       */
      private $lastLogin = 0;
  
      /**
       * @ORM\Column(type="integer")
       */
      protected $expires = 0;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $sessionId = '';
  
      /**
       * @ORM\ManyToMany(targetEntity="BankDetails", cascade={"persist"})
       * @ORM\JoinTable(name="User_BankDetails",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="bank_details_id", referencedColumnName="id")}
       * )
       * @Assert\Valid
       */
      private $bankDetails;
  
      /**
       * @ORM\ManyToMany(targetEntity="Address", cascade={"persist"})
       * @ORM\JoinTable(name="User_BillingAddress",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="billing_address_id", referencedColumnName="id")}
       * )
       * @Assert\Count(
       *      min = 1,
       *      minMessage = "user.billing_addresses.min",
       * )
       * @Assert\Valid
       */
      private $billingAddresses;
  
      public function __construct()
      {
          parent::__construct();
          $this-&gt;isActive = true;
          $this-&gt;confirmationEmailSend = 0;
          $this-&gt;userGroups = new ArrayCollection();
          $this-&gt;bankDetails = new ArrayCollection();
          $this-&gt;billingAddresses = new ArrayCollection();
          // may not be needed, see section on salt below
          // $this-&gt;salt = md5(uniqid(null, true));
      }
  
      /**
       * @ORM\PrePersist
       */
      public function prePersist()
      {
          $currentTimestamp = time();
  
          if($this-&gt;getConfirmationEmailSend() == NULL)
              $this-&gt;setConfirmationEmailSend(0);
  
     }
  
      public function getUsername()
      {
          //return $this-&gt;username;
          return $this-&gt;email;
      }
  
      public function getSalt()
      {
          // The bcrypt algorithm doesn't require a separate salt.
          return null;
      }
  
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      public function getRoles()
      {
          $roles = array();
          $userGroups = $this-&gt;getUserGroups();
          if(!empty($userGroups)) {
              foreach($userGroups as $userGroup) {
                  $role = $userGroup-&gt;getRole();
                  $roles[] = 'ROLE_'.strtoupper($role);
              }
          }
          return $roles;
      }
  
      public function isGranted($role)
      {
          return in_array($role, $this-&gt;getRoles());
      }
  
      public function eraseCredentials()
      {
      }
  
      public function isAccountNonExpired()
      {
          return true;
      }
  
      public function isAccountNonLocked()
      {
          return true;
      }
  
      public function isCredentialsNonExpired()
      {
          return true;
      }
  
      public function isEnabled()
      {
          return $this-&gt;isActive;
      }
  
      /** @see \Serializable::serialize() */
      public function serialize()
      {
          return serialize(array(
              $this-&gt;id,
              $this-&gt;username,
              $this-&gt;password,
              $this-&gt;isActive,
              // see section on salt below
              // $this-&gt;salt,
          ));
      }
  
      /** @see \Serializable::unserialize() */
      public function unserialize($serialized)
      {
          list (
              $this-&gt;id,
              $this-&gt;username,
              $this-&gt;password,
              $this-&gt;isActive,
              // see section on salt below
              // $this-&gt;salt
          ) = unserialize($serialized);
      }
  
      /**
       * Set username
       *
       * @param string $username
       *
       * @return User
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      public function getPlainPassword()
      {
          return $this-&gt;plainPassword;
      }
  
      public function setPlainPassword($password)
      {
          $this-&gt;plainPassword = $password;
      }
  
      /**
       * Set password
       *
       * @param string $password
       *
       * @return User
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      /**
       * Set email
       *
       * @param string $email
       *
       * @return User
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
          $this-&gt;setUsername($email);
  
          return $this;
      }
  
      /**
       * Get email
       *
       * @return string
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
      /**
       * Set isActive
       *
       * @param boolean $isActive
       *
       * @return User
       */
      public function setIsActive($isActive)
      {
          $this-&gt;isActive = $isActive;
  
          return $this;
      }
  
      /**
       * Get isActive
       *
       * @return boolean
       */
      public function getIsActive()
      {
          return $this-&gt;isActive;
      }
  
      /**
       * Add userGroup
       *
       * @param \AppBundle\Entity\UserGroup $userGroup
       *
       * @return User
       */
      public function addUserGroup(\AppBundle\Entity\UserGroup $userGroup)
      {
          $this-&gt;userGroups[] = $userGroup;
  
          return $this;
      }
  
      /**
       * Remove userGroup
       *
       * @param \AppBundle\Entity\UserGroup $userGroup
       */
      public function removeUserGroup(\AppBundle\Entity\UserGroup $userGroup)
      {
          $this-&gt;userGroups-&gt;removeElement($userGroup);
      }
  
      /**
       * Get userGroups
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getUserGroups()
      {
          return $this-&gt;userGroups;
      }
  
      /**
       * Set shibbolethPersistentId
       *
       * @param string $shibbolethPersistentId
       *
       * @return User
       */
      public function setShibbolethPersistentId($shibbolethPersistentId)
      {
          $this-&gt;shibbolethPersistentId = $shibbolethPersistentId;
  
          return $this;
      }
  
      /**
       * Get shibbolethPersistentId
       *
       * @return string
       */
      public function getShibbolethPersistentId()
      {
          return $this-&gt;shibbolethPersistentId;
      }
  
      /**
       * Set firstName
       *
       * @param string $firstName
       *
       * @return User
       */
      public function setFirstName($firstName)
      {
          $this-&gt;firstName = $firstName;
  
          return $this;
      }
  
      /**
       * Get firstName
       *
       * @return string
       */
      public function getFirstName()
      {
          return $this-&gt;firstName;
      }
  
      /**
       * Set lastName
       *
       * @param string $lastName
       *
       * @return User
       */
      public function setLastName($lastName)
      {
          $this-&gt;lastName = $lastName;
  
          return $this;
      }
  
      /**
       * Get lastName
       *
       * @return string
       */
      public function getLastName()
      {
          return $this-&gt;lastName;
      }
  
      /**
       * Set emailConfirmed
       *
       * @param boolean $emailConfirmed
       *
       * @return User
       */
      public function setEmailConfirmed($emailConfirmed)
      {
          $this-&gt;emailConfirmed = $emailConfirmed;
  
          return $this;
      }
  
      /**
       * Get emailConfirmed
       *
       * @return boolean
       */
      public function getEmailConfirmed()
      {
          return $this-&gt;emailConfirmed;
      }
  
      public function removeAllUserGroups() {
          $userGroups = $this-&gt;getUserGroups();
          foreach($userGroups as $userGroup) {
             $this-&gt;removeUserGroup($userGroup);
          }
      }
  
      public function hasUserGroup($userGroupId) {
          foreach($this-&gt;getUserGroups() as $userGroup) {
              if($userGroup-&gt;getId() == $userGroupId)
                  return true;
          }
          return false;
      }
  
      /**
       * Set lastLogin
       *
       * @param integer $lastLogin
       *
       * @return User
       */
      public function setLastLogin($lastLogin)
      {
          $this-&gt;lastLogin = $lastLogin;
  
          return $this;
      }
  
      /**
       * Get lastLogin
       *
       * @return integer
       */
      public function getLastLogin()
      {
          return $this-&gt;lastLogin;
      }
  
      /**
       * Set confirmationEmailSend
       *
       * @param integer $confirmationEmailSend
       *
       * @return User
       */
      public function setConfirmationEmailSend($confirmationEmailSend)
      {
          $this-&gt;confirmationEmailSend = $confirmationEmailSend;
  
          return $this;
      }
  
      /**
       * Get confirmationEmailSend
       *
       * @return integer
       */
      public function getConfirmationEmailSend()
      {
          return $this-&gt;confirmationEmailSend;
      }
  
      /**
       * Set validTill
       *
       * @param integer $validTill
       *
       * @return User
       */
      public function setValidTill($validTill)
      {
          $this-&gt;validTill = $validTill;
  
          return $this;
      }
  
      /**
       * Get validTill
       *
       * @return integer
       */
      public function getValidTill()
      {
          return $this-&gt;validTill;
      }
  
      /**
       * Set shibbolethValid
       *
       * @param integer $shibbolethValid
       *
       * @return User
       */
      public function setShibbolethValid($shibbolethValid)
      {
          $this-&gt;shibbolethValid = $shibbolethValid;
  
          return $this;
      }
  
      /**
       * Get shibbolethValid
       *
       * @return integer
       */
      public function getShibbolethValid()
      {
          return $this-&gt;shibbolethValid;
      }
  
      /**
       * Set shibbolethHash
       *
       * @param string $shibbolethHash
       *
       * @return User
       */
      public function setShibbolethHash($shibbolethHash)
      {
          $this-&gt;shibbolethHash = $shibbolethHash;
  
          return $this;
      }
  
      /**
       * Get shibbolethHash
       *
       * @return string
       */
      public function getShibbolethHash()
      {
          return $this-&gt;shibbolethHash;
      }
  
      /**
       * Set shibbolethState
       *
       * @param integer $shibbolethState
       *
       * @return User
       */
      public function setShibbolethState($shibbolethState)
      {
          $this-&gt;shibbolethState = $shibbolethState;
  
          return $this;
      }
  
      /**
       * Get shibbolethState
       *
       * @return integer
       */
      public function getShibbolethState()
      {
          return $this-&gt;shibbolethState;
      }
  
      /**
       * Set expires
       *
       * @param integer $expires
       *
       * @return User
       */
      public function setExpires($expires)
      {
          $this-&gt;expires = $expires;
  
          return $this;
      }
  
      /**
       * Get expires
       *
       * @return integer
       */
      public function getExpires()
      {
          return $this-&gt;expires;
      }
  
      /**
       * Set emailNew
       *
       * @param string $emailNew
       *
       * @return User
       */
      public function setEmailNew($emailNew)
      {
          $this-&gt;emailNew = $emailNew;
  
          return $this;
      }
  
      /**
       * Get emailNew
       *
       * @return string
       */
      public function getEmailNew()
      {
          return $this-&gt;emailNew;
      }
  
      /**
       * Set passwordHash
       *
       * @param string $passwordHash
       *
       * @return User
       */
      public function setPasswordHash($passwordHash)
      {
          $this-&gt;passwordHash = $passwordHash;
  
          return $this;
      }
  
      /**
       * Get passwordHash
       *
       * @return string
       */
      public function getPasswordHash()
      {
          return $this-&gt;passwordHash;
      }
  
      /**
       * Set sessionId
       *
       * @param string $sessionId
       *
       * @return User
       */
      public function setSessionId($sessionId)
      {
          $this-&gt;sessionId = $sessionId;
  
          return $this;
      }
  
      /**
       * Get sessionId
       *
       * @return string
       */
      public function getSessionId()
      {
          return $this-&gt;sessionId;
      }
  
      /**
       * Set salutation
       *
       * @param \AppBundle\Entity\Salutation $salutation
       *
       * @return User
       */
      public function setSalutation(\AppBundle\Entity\Salutation $salutation = null)
      {
          $this-&gt;salutation = $salutation;
  
          return $this;
      }
  
      /**
       * Get salutation
       *
       * @return \AppBundle\Entity\Salutation
       */
      public function getSalutation()
      {
          return $this-&gt;salutation;
      }
  
      /**
       * Add bankDetail
       *
       * @param \AppBundle\Entity\BankDetails $bankDetail
       *
       * @return User
       */
      public function addBankDetail(\AppBundle\Entity\BankDetails $bankDetail)
      {
          $this-&gt;bankDetails[] = $bankDetail;
  
          return $this;
      }
  
      /**
       * Remove bankDetail
       *
       * @param \AppBundle\Entity\BankDetails $bankDetail
       */
      public function removeBankDetail(\AppBundle\Entity\BankDetails $bankDetail)
      {
          $this-&gt;bankDetails-&gt;removeElement($bankDetail);
      }
  
      /**
       * Get bankDetails
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getBankDetails()
      {
          return $this-&gt;bankDetails;
      }
  
      /**
       * Add billingAddress
       *
       * @param \AppBundle\Entity\Address $billingAddress
       *
       * @return User
       */
      public function addBillingAddress(\AppBundle\Entity\Address $billingAddress)
      {
          $this-&gt;billingAddresses[] = $billingAddress;
  
          return $this;
      }
  
      /**
       * Remove billingAddress
       *
       * @param \AppBundle\Entity\Address $billingAddress
       */
      public function removeBillingAddress(\AppBundle\Entity\Address $billingAddress)
      {
          $this-&gt;billingAddresses-&gt;removeElement($billingAddress);
      }
  
      /**
       * Set billingAddresses
       *
       * @param \AppBundle\Entity\Address $billingAddress
       *
       * @return User
       * 
       */
      public function setBillingAddresses(\AppBundle\Entity\Address $billingAddress)
      {
          if($this-&gt;billingAddresses !== NULL and $this-&gt;billingAddresses-&gt;contains($billingAddress)){
              return false;
          }
          $this-&gt;addBillingAddress($billingAddress);
          return $this;
      }
  
      /**
       * Set one billingAddresses
       *
       * @param \AppBundle\Entity\Address $billingAddress
       *
       * @return User
       * 
       */
      public function setOneBillingAddresses(\AppBundle\Entity\Address $billingAddress)
      {
          $this-&gt;billingAddresses = $billingAddress;
  
          return $this;
      }
  
      /**
       * Set one billingAddresses
       *
       * @param \AppBundle\Entity\Address $billingAddress
       *
       * @return User
       * 
       */
      public function unsetBillingAddresses()
      {
          $this-&gt;billingAddresses = new ArrayCollection();
  
          return $this;
      }
  
      /**
       * Get billingAddresses
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getBillingAddresses()
      {
          return $this-&gt;billingAddresses;
      }
  }
  </code></pre>
  
  <p>config/security.yml</p>
  
  <pre><code>providers:
          chain_provider:
              chain:
                  providers: [in_memory, database_user]
          in_memory:
              memory:
                  users:
                      admin:
                          password: ***
                          roles: 'ROLE_ADMIN'
          database_user:
              entity:
                  class: AppBundle:User
  
      firewalls:
          # disables authentication for assets and the profiler, adapt it according to your needs
          dev:
              pattern: ^/(_(profiler|wdt)|css|images|js)/
              security: false
          secured_area:
              # pattern: match to pages
              anonymous: ~
              pattern:    ^/
              access_denied_handler: AppBundle\Security\AccessDeniedHandler
              provider: chain_provider
              form_login:
                  login_path: /login
                  check_path: /login_check
                  default_target_path: account
                  # Configuring CSRF protection
                  csrf_parameter: _csrf_security_token
                  csrf_token_id: a_private_string
                  success_handler: AppBundle\Handler\LoginSuccessHandler
              logout:
                  path: /logout
                  target: /login
  
      access_control:
          ...
  
      role_hierarchy:
          ...
  
      encoders:
          AppBundle\Entity\User:
              algorithm: bcrypt
          Symfony\Component\Security\Core\User\User:
              plaintext
  </code></pre>