2013-10-14 2 views
2

나는 다음과 같은 나뭇 가지 템플릿 콘텐츠로 Symfony2 security documentation를 통해이 로그인 양식을 가지고 :Symfony2, 로그인 폼 - CSRF 보호

<form action="{{ path('login_check') }}" method="post"> 
    <div class="input form"> 
     <label for="username">Account name or E-mail:</label> 
     <input type="text" id="username" name="_username" value="{{ last_username }}" required="required" /> 
    </div> 
    <div class="input form"> 
     <label for="password">Password:</label> 
     <input type="password" id="password" name="_password" required="required" /> 
    </div> 
    <input type="hidden" name="_token" value="{{ csrf_token("intention") }}"> 
    <button type="submit">Log In</button> 
</form> 

그리고이 양식에 CSRF 보호를 추가 할. 보시다시피,이 라인을 <input type="hidden" name="_token" value="{{ csrf_token("intention") }}">에 추가했는데,이 보호 기능을 활성화하기에 충분하다면 정말로 확신 할 수 없습니다.

내 컨트롤러는 문서에 같은 형태를 가지고, 그래서 다음과 같습니다

<?php 
// src/Acme/SecurityBundle/Controller/SecurityController.php; 
namespace Acme\SecurityBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Core\SecurityContext; 

class SecurityController extends Controller 
{ 
    public function loginAction() 
    { 
     $request = $this->getRequest(); 
     $session = $request->getSession(); 

     // get the login error if there is one 
     if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $request->attributes->get(
       SecurityContext::AUTHENTICATION_ERROR 
      ); 
     } else { 
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
      $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
     } 

     return $this->render(
      'AcmeSecurityBundle:Security:login.html.twig', 
      array(
       // last username entered by the user 
       'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
       'error'   => $error, 
      ) 
     ); 
    } 
} 

그래서 단지 충분히 가치 {{ csrf_token("intention") }} 하나 개의 숨겨진 입력을 붙여 넣거나 내가 컨트롤러에 무언가를 추가해야입니까?

답변

2

@Chris McKinnel의 답변이 사실이 아닙니다.

http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html

내가 내 security.yml에 줄을 추가해야합니다 : 자, Symfony2 튜토리얼 페이지에서이 부분이

form_login: 
     # ... 
     csrf_provider: form.csrf_provider 

변경을이

<input type="hidden" name="_token" value="{{ csrf_token("intention") }}"> 

~

<input type="hidden" name="_csrf_token" value="{{ csrf_token("authenticate") }}"> 

이제 인증 양식이 CSRF로 보호됩니다.

+0

양식 "form_login"을 호출해야합니까? – Sekai

+0

@Sekai 식별 로그인 양식의 구성 항목입니다. 이 항목에서 로그인 작업을위한 경로 또는이 대답에서 볼 수있는 것처럼 csrf 공급자에 대한 옵션을 설정합니다. –

+0

그래, 동의하지만 양식 (임의의 문자를 추가)에서 토큰을 조작하고 양식을 제출하면 성공했다. 보호 기능은 어떻게 작동합니까? – Sekai

1

CSRF 보호는 기본적으로 활성화되어 있으므로 HTML에 CSRF 토큰을 렌더링하면됩니다. 컨트롤러에서 아무 것도 할 필요가 없습니다.

당신은 몇 가지 옵션이 있습니다 :

  1. 이 그것을 당신이
  2. 사용 {{ form_rest(form) }} 위를했던 것처럼를,이 같은 숨겨진 필드를 포함 아직 렌더링되지 않은 모든 필드를 렌더링 CSRF 토큰

어느 쪽이든 제대로 작동합니다.

+1

그러나이 로그인에는 생성 된 양식이 없으므로이 기능을 사용할 수 없습니다. –