2017-02-27 11 views
1

Ich versuche, ein Formular Login-Authentifizierung mit Schutz (Symfony 3.2), aber es funktioniert nicht. Die Authentifizierung funktioniert, aber wenn ich zur Startseite (accueil) weitergeleitet werde, werde ich ohne Authentifizierung auf die Anmeldeseite umgeleitet.
Wenn ich in dem Controler meiner Homepage setzensymfony3 guard Anmeldeformular authentifiziert nicht

$user = $this->get('security.token_storage')->getToken(); 
dump($user); die; 

Ich kann meinen Benutzer, die Rolle sehen, aber er ist nicht authentifiziert.

DashboardController.php on line 23: 
PostAuthenticationGuardToken {#133 ▼ 
-providerKey: "main" 
-user: User {#457 ▶} 
-roles: array:1 [▼ 
    0 => Role {#120 ▼ 
    -role: "ROLE_SUPERADMIN" 
    } 
    ]  
-authenticated: false 
-attributes: [] 
} 

Was habe ich verpasst?

Security.ym

security: 
encoders: 
    EntBundle\Entity\User\User: 
    algorithm: bcrypt 

providers: 
    database: 
     entity: 
      class: EntBundle:User\User 
      property: username 

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

    main: 
     pattern: ^/ 
     anonymous: ~ 
     logout: ~ 
     guard: 
      authenticators: 
      - ent.login_authenticator 

TestAuthenticator.php

namespace EntBundle\Security; 

use Doctrine\ORM\EntityManager; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\RouterInterface; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; 
use Symfony\Component\Security\Core\Security; 
use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\User\UserProviderInterface; 
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; 

class TestAuthenticator extends AbstractGuardAuthenticator 
{ 
private $em; 
private $router; 

public function __construct(EntityManager $em, RouterInterface $router) 
{ 
    $this->em = $em; 
    $this->router = $router; 
} 

public function getCredentials(Request $request) 
{ 
    if ($request->getPathInfo() != '/login' || !$request->isMethod('POST'))  { 
     return; 
    } 

    return [ 
     'username' => $request->request->get('_username'), 
     'password' => $request->request->get('_password'), 
    ]; 
} 

public function getUser($credentials, UserProviderInterface $userProvider) 
{ 
    $username = $credentials['username']; 
    return $this->em->getRepository('EntBundle:User\User')->findOneBy(['username' => $username]); 
} 

public function checkCredentials($credentials, UserInterface $user) 
{ 
    // this is just for test 
    return true; 
} 

public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
{ 
    $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception); 
    $url = $this->router->generate('login'); 
    return new RedirectResponse($url); 
} 

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) 
{ 
    $url = $this->router->generate('accueil'); 
    return new RedirectResponse($url); 
} 

public function start(Request $request, AuthenticationException $authException = null) 
{ 
    $url = $this->router->generate('login'); 
    return new RedirectResponse($url); 
} 

public function supportsRememberMe() 
{ 
    return false; 
} 
} 

DashboardController.php

namespace EntBundle\Controller\Dashboard; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 


class DashboardController extends Controller 
{ 
/** 
* @Route("/accueil", name="accueil") 
*/ 
public function indexAction() 
{ 
    $user = $this->get('security.token_storage')->getToken(); 
    dump($user); die; 
    return $this->render('EntBundle:dashboard:dashboard_structure.html.twig'); 
} 


/** 
* @Route("/login", name="login") 
*/ 
public function loginAction() 
{ 
    $authenticationUtils = $this->get('security.authentication_utils'); 
    $error = $authenticationUtils->getLastAuthenticationError(); 
    $lastUsername = $authenticationUtils->getLastUsername(); 

    return $this->render('EntBundle::login.html.twig', [ 
     'last_username' => $lastUsername, 
     'error' => $error, 
    ]); 
} 

/** 
* @Route("/logout", name="logout") 
*/ 
public function logoutAction() 
{ 
} 
} 

EDIT:
Dank leo_ap für Ihre Hilfe, aber das Problem von dort kommt nicht .
Die config-Sitzung ist wie folgt:

session: 
    handler_id: session.handler.native_file 
    save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%" 

und wenn ich im Pfad Ordner speichern überprüfen Ich habe Sitzungsdatei erstellt, aber nicht authentifiziert.

_sf2_attributes|a:1:{s:26:"_security.main.target_path";s:29:"http://localhost:8000/accueil";}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:"u";i:1488245179;s:1:"c";i:1488244922;s:1:"l";s:1:"0";} 

Wenn ich versuche, den normalen login_form mit security.yml es funktioniert gut ...
Ich habe versucht mit handler_id und save_path auf null ohne Erfolg.

EDIT2:
Ich habe gefunden, warum ich immer auf die Anmeldeseite umgeleitet werde, weil ich ausgeloggt bin!

[2017-02-28 09:16:34] security.INFO: The security token was removed due to an AccountStatusException. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationExpiredException(code: 0): at /home/philippe/Documents/symfony/vendor/symfony/symfony/src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php:86)"} 

und in GuardAuthenticationProvider.php (86)

The listener *only* passes PreAuthenticationGuardToken instances. 
    This means that an authenticated token (e.g.PostAuthenticationGuardToken) 
    is being passed here, which happens if that token becomes "not authenticated" (e.g. happens if the user changes between requests). 
    In this case, the user should be logged out, so we will return an AnonymousToken to accomplish that. 

Aber warum ???

Antwort

0

Möglicherweise ist Ihre Sitzung das Token nicht persistent. Überprüfen Sie Ihre Sitzungskonfiguration innerhalb: config.yml. In der framework Option gibt es session. Sehen Sie, wie die handler_id und save_path konfiguriert sind. Es kann sein, dass Ihre PHP-Installation die Sitzungen im konfigurierten Pfad nicht verarbeiten kann. Versuchen Sie null auf handler_id und save_path zu setzen, um zu erzwingen, dass php seine eigenen eingebauten Konfigurationen verwendet, um Sitzungen zu behandeln.

config.yml Datei:

framework: 

    { .. Other configurations ..} 

    session: 
     handler_id: null 
     save_path: null 

    { .. More configurations ..} 
+0

Hallo leo_ap. Siehe meine Bearbeitung ... – lemairep

Verwandte Themen