2017-02-22 5 views
1

나는 security.yml 대신 로그인 양식을 만들기 위해 Guard를 사용하려고합니다.symfony3 가드 및 로그인 양식

Getuser 및 checkcredential은 정상입니다.
onAuthenticationSuccess가 okAuthenticationSuccess에 dump($token); die;을 넣었 으면 내 사용자를 토큰에서 볼 수 있습니다.)/accueil로 리디렉션합니다.
하지만/accueil에 도착하면 사용자 인증이 항상 anon에 있기 때문에/login으로 다시 전송됩니다!

불가능 해결책을 찾기 위해 : C/security.yml에서

방화벽 :

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

    login_firewall: 
     pattern: ^/login$ 
     anonymous: true 

    main: 
     pattern: ^/ 
     anonymous: ~ 
     logout: ~ 
     switch_user: true 
     guard: 
      provider: database 
      authenticators: 
       - ent.login_authenticator 

access_control: 
    - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/admin/, roles: ROLE_ADMIN } 
    - { path: ^/, roles: ROLE_USER } 

securityController

/** 
* @Route("/login", name="login") 
* 
*/ 
public function loginAction(Request $request) 
{ 

    if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
    return $this->redirectToRoute('accueil'); 
    } 

    $authenticationUtils = $this->get('security.authentication_utils'); 

    $exception = $authenticationUtils->getLastAuthenticationError(); 

    $lastUsername = $authenticationUtils->getLastUsername(); 

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

} 

/** 
* @Route("/login_check", name="login_check") 
*/ 
public function loginCheckAction() 
{ 
    // this controller will not be executed, 
    // as the route is handled by the Security system 
} 

loginAuthenticator : 위도에 대한

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

public function getCredentials(Request $request) 
{ 
    if ($request->getPathInfo() != '/login_check') { 
     return null; 
    } 

    $request->getSession()->set(Security::LAST_USERNAME, $request->request->get('_username')); 

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

public function getUser($credentials, UserProviderInterface $userProvider) 
{ 
    try { 
     return $this->em->getRepository('EntBundle:User\User')->findOneBy(array('username' => $credentials)); 
    } 
    catch (UsernameNotFoundException $e) { 
     throw new CustomUserMessageAuthenticationException($this->failMessage); 
    } 
} 

public function checkCredentials($credentials, UserInterface $user) { 

    $plainPassword = $credentials['password']; 
    if ($this->passwordEncoder->isPasswordValid($user, $plainPassword)) { 
     return true; 
    } 

    throw new CustomUserMessageAuthenticationException($this->failMessage); 
} 

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

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 start(Request $request, AuthenticationException $authException = null) 
{ 
    $url = $this->router->generate('login'); 
    return new RedirectResponse($url); 
} 
+0

당신은 사용자 토큰을 설정하는 당신을 도움 바랍니다

use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; and the actual setting of the token part will be like: $token = new UsernamePasswordToken($user, $user->getPassword(), "firewall goes here for example: main", $user->getRoles()); $this->get("security.token_storage")->setToken($token); 

: 전자 응답 코드의 조각은 이런 일이 symfony3 응용 프로그램에서 토큰을 설정 보일 것이다? –

+0

안녕 Giovnni 답변 주셔서 감사합니다. 내가 놓친 뭔가가있는 것처럼 보입니다. 이 토큰을 관리하는 방법을 설명하는 링크가 있습니까? – lemairep

답변

0

죄송합니다 나는이 :)