Customizing the Form Login Authenticator Responses

The form login authenticator creates a login form where users authenticate using an identifier (e.g. email address or username) and a password. In Security the usage of this authenticator is explained.

フォーム ログイン認証は、ユーザーが識別子 (電子メール アドレスやユーザー名など) とパスワードを使用して認証するログイン フォームを作成します。 InSecurity では、このオーセンティケーターの使用法について説明します。

Redirecting after Success

By default, the form will redirect to the URL the user requested (i.e. the URL which triggered the login form being shown). For example, if the user requested http://www.example.com/admin/post/18/edit, then after they have successfully logged in, they will be sent back to http://www.example.com/admin/post/18/edit.

デフォルトでは、フォームはユーザーが要求した URL (つまり、表示されるログイン フォームをトリガーした URL) にリダイレクトされます。たとえば、ユーザーが http://www.example.com/admin/post/18/edit をリクエストした場合、ログインに成功すると、http://www.example.com/admin/post に返信されます。 /18/編集。

This is done by storing the requested URL in the session. If no URL is present in the session (perhaps the user went directly to the login page), then the user is redirected to / (i.e. the homepage). You can change this behavior in several ways.

これは、要求された URL をセッションに保存することによって行われます。セッションに URL が存在しない場合 (ユーザーがログイン ページに直接アクセスした可能性があります)、ユーザーは / (つまり、ホームページ) にリダイレクトされます。この動作はいくつかの方法で変更できます。

Changing the default Page

Define the default_target_path option to change the page where the user is redirected to if no previous page was stored in the session. The value can be a relative/absolute URL or a Symfony route name:

前のページがセッションに保存されていない場合にユーザーがリダイレクトされるページを変更するには、default_target_path オプションを定義します。値は、相対/絶対 URL または Symfony ルート名にすることができます:
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
# config/packages/security.yaml
security:
    # ...

    firewalls:
        main:
            form_login:
                # ...
                default_target_path: after_login_route_name

Always Redirect to the default Page

Define the always_use_default_target_path boolean option to ignore the previously requested URL and always redirect to the default page:

always_use_default_target_path ブール値オプションを定義して、以前に要求された URL を無視し、常にデフォルト ページにリダイレクトします。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
# config/packages/security.yaml
security:
    # ...

    firewalls:
        main:
            form_login:
                # ...
                always_use_default_target_path: true

Control the Redirect Using Request Parameters

The URL to redirect to after the login can be dynamically defined using the _target_path parameter of the GET or POST request. Its value must be a relative or absolute URL, not a Symfony route name.

ログイン後にリダイレクトする URL は、GET または POST 要求の _target_path パラメータを使用して動的に定義できます。その値は、Symfony のルート名ではなく、相対 URL または絶対 URL でなければなりません。

For GET, use a query string parameter:

GET の場合、クエリ文字列パラメーターを使用します。
1
http://example.com/some/path?_target_path=/dashboard

For POST, use a hidden form field:

POST の場合、非表示のフォーム フィールドを使用します。
1
2
3
4
5
6
7
{# templates/login/index.html.twig #}
<form action="{{ path('app_login') }}" method="post">
    {# ... #}

    <input type="hidden" name="_target_path" value="{{ path('account') }}">
    <input type="submit" name="login">
</form>

Using the Referring URL

In case no previous URL was stored in the session and no _target_path parameter is included in the request, you may use the value of the HTTP_REFERER header instead, as this will often be the same. Define the use_referer boolean option to enable this behavior:

以前の URL がセッションに保存されておらず、リクエストに _target_path パラメータが含まれていない場合は、多くの場合同じであるため、代わりに HTTP_REFERER ヘッダーの値を使用できます。この動作を有効にするには、use_referer ブール値オプションを定義します。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
# config/packages/security.yaml
security:
    # ...

    firewalls:
        main:
            # ...
            form_login:
                # ...
                use_referer: true

Note

ノート

The referrer URL is only used when it is different from the URL generated by the login_path route to avoid a redirection loop.

リファラー URL は、リダイレクト ループを回避するために login_path ルートによって生成された URL とは異なる場合にのみ使用されます。

Redirecting after Failure

After a failed login (e.g. an invalid username or password was submitted), the user is redirected back to the login form itself. Use the failure_path option to define a new target via a relative/absolute URL or a Symfony route name:

ログインに失敗した後 (たとえば、無効なユーザー名またはパスワードが送信された場合)、ユーザーはログイン フォーム自体にリダイレクトされます。相対/絶対 URL または Symfony ルート名を介して新しいターゲットを定義するには、failure_path オプションを使用します。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
# config/packages/security.yaml
security:
    # ...

    firewalls:
        main:
            # ...
            form_login:
                # ...
                failure_path: login_failure_route_name

This option can also be set via the _failure_path request parameter:

このオプションは、_failure_path リクエスト パラメータを介して設定することもできます。
1
http://example.com/some/path?_failure_path=/forgot-password
1
2
3
4
5
6
7
{# templates/security/login.html.twig #}
<form action="{{ path('login') }}" method="post">
    {# ... #}

    <input type="hidden" name="_failure_path" value="{{ path('forgot_password') }}">
    <input type="submit" name="login">
</form>

Customizing the Target and Failure Request Parameters

The name of the request attributes used to define the success and failure login redirects can be customized using the target_path_parameter and failure_path_parameter options of the firewall that defines the login form.

成功および失敗のログイン リダイレクトを定義するために使用される要求属性の名前は、ログイン フォームを定義するファイアウォールの target_path_parameter およびfailure_path_parameter オプションを使用してカスタマイズできます。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
# config/packages/security.yaml
security:
    # ...

    firewalls:
        main:
            # ...
            form_login:
                target_path_parameter: go_to
                failure_path_parameter: back_to

Using the above configuration, the query string parameters and hidden form fields are now fully customized:

上記の構成を使用すると、クエリ文字列パラメーターと非表示のフォーム フィールドが完全にカスタマイズされます。
1
http://example.com/some/path?go_to=/dashboard&back_to=/forgot-password
1
2
3
4
5
6
7
8
{# templates/security/login.html.twig #}
<form action="{{ path('login') }}" method="post">
    {# ... #}

    <input type="hidden" name="go_to" value="{{ path('dashboard') }}">
    <input type="hidden" name="back_to" value="{{ path('forgot_password') }}">
    <input type="submit" name="login">
</form>