2017-02-27 10 views
1

가드 (symfony 3.2)로 폼 로그인 인증을 시도하지만 작동하지 않습니다. 인증이 작동하지만 홈 페이지 (accueil)로 리디렉션 될 때 인증없이 로그인 페이지로 리디렉션됩니다.
내 홈 페이지symfony3 가드 로그인 폼이 인증되지 않습니다

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

내 사용자, 역할을 볼 수 있지만 자신이 인증되지 않은의 controler를 넣어합니다.

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

내가 빠뜨린 내용은 무엇입니까?

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() 
{ 
} 
} 

편집 :
감사합니다 당신의 도움이 있지만, 거기에서 오는 나던 문제에 대한 leo_ap .

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

내가 저장 경로 폴더에 체크하면 나는 세션 파일이 생성되지만 인증하지 않은 :
는 config 세션은 다음과 같이한다.

_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";} 

나는 ... 잘 작동하고
내가 성공하지 널 (null)에 handler_id 및 save_path으로 시도했습니다 security.yml와 일반 login_form을 시도하십시오.

EDIT2 :
내가 로그 아웃했기 때문에 왜 내가 항상 로그인 페이지로 리디렉션되었는지 알게되었습니다!

[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)"} 

및 이유 (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. 

GuardAuthenticationProvider.php 그러나에

???

답변

0

토큰을 지속하지 않는 세션 일 수 있습니다. 세션 구성 (내부 : config.yml)을 확인하십시오. framework 옵션에는 session이 있습니다. handler_idsave_path이 어떻게 구성되었는지 확인하십시오. PHP 설치가 구성된 경로에서 세션을 처리 할 수없는 것일 수 있습니다. handler_idsave_path에 null을 두어 세션을 처리하기 위해 PHP가 자체 빌드를 사용하도록합니다.

config.yml 파일 :

framework: 

    { .. Other configurations ..} 

    session: 
     handler_id: null 
     save_path: null 

    { .. More configurations ..} 
+0

안녕 leo_ap. 내 편집보기 ... – lemairep