2

次のTWIGテンプレートコンテンツを含むSymfony2セキュリティドキュメントを介して、このログインフォームを持っています:

<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") }}ですか、それともコントローラーに何かを追加する必要がありますか?

4

2 に答える 2

2

@Chris McKinnel からの回答は正しくないことがわかりました。現在、Symfony2 のチュートリアル ページには次のセクションがあります。

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

私は私の中に行を追加する必要がありますsecurity.yml:

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

これで、認証フォーム si CSRF が保護されたと確信しました。

于 2014-02-23T19:16:36.623 に答える