发布于 2015-08-27 16:39:58 | 210 次阅读 | 评论: 0 | 来源: 网络整理
Using a form login for authentication is a common, and flexible, method for handling authentication in Symfony. Pretty much every aspect of the form login can be customized. The full, default configuration is shown in the next section.
To see the full form login configuration reference, see SecurityBundle Configuration (“security”). Some of the more interesting options are explained below.
You can change where the login form redirects after a successful login using
the various config options. 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 successfully log in, they will eventually be sent back to
http://www.example.com/admin/post/18/edit
.
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 the default page,
which is /
(i.e. the homepage) by default. You can change this behavior
in several ways.
注解
As mentioned, by default the user is redirected back to the page originally requested. Sometimes, this can cause problems, like if a background Ajax request “appears” to be the last visited URL, causing the user to be redirected there. For information on controlling this behavior, see How to Change the default Target Path Behavior.
First, the default page can be set (i.e. the page the user is redirected to
if no previous page was stored in the session). To set it to the
default_security_target
route use the following config:
# app/config/security.yml
security:
firewalls:
main:
form_login:
# ...
default_target_path: default_security_target
<!-- app/config/security.xml -->
<config>
<firewall>
<form-login
default_target_path="default_security_target"
/>
</firewall>
</config>
// app/config/security.php
$container->loadFromExtension('security', array(
'firewalls' => array(
'main' => array(
// ...
'form_login' => array(
// ...
'default_target_path' => 'default_security_target',
),
),
),
));
Now, when no URL is set in the session, users will be sent to the
default_security_target
route.
You can make it so that users are always redirected to the default page regardless
of what URL they had requested previously by setting the
always_use_default_target_path
option to true:
# app/config/security.yml
security:
firewalls:
main:
form_login:
# ...
always_use_default_target_path: true
<!-- app/config/security.xml -->
<config>
<firewall>
<form-login
always_use_default_target_path="true"
/>
</firewall>
</config>
// app/config/security.php
$container->loadFromExtension('security', array(
'firewalls' => array(
'main' => array(
// ...
'form_login' => array(
// ...
'always_use_default_target_path' => true,
),
),
),
));
In case no previous URL was stored in the session, you may wish to try using
the HTTP_REFERER
instead, as this will often be the same. You can do
this by setting use_referer
to true (it defaults to false):
# app/config/security.yml
security:
firewalls:
main:
form_login:
# ...
use_referer: true
<!-- app/config/security.xml -->
<config>
<firewall>
<form-login
use_referer="true"
/>
</firewall>
</config>
// app/config/security.php
$container->loadFromExtension('security', array(
'firewalls' => array(
'main' => array(
// ...
'form_login' => array(
// ...
'use_referer' => true,
),
),
),
));
You can also override where the user is redirected to via the form itself by
including a hidden field with the name _target_path
. For example, to
redirect to the URL defined by some account
route, use the following:
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<input type="hidden" name="_target_path" value="account" />
<input type="submit" name="login" />
</form>
<!-- src/Acme/SecurityBundle/Resources/views/Security/login.html.php -->
<?php if ($error): ?>
<div><?php echo $error->getMessage() ?></div>
<?php endif ?>
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<input type="hidden" name="_target_path" value="account" />
<input type="submit" name="login" />
</form>
Now, the user will be redirected to the value of the hidden form field. The
value attribute can be a relative path, absolute URL, or a route name. You
can even change the name of the hidden form field by changing the target_path_parameter
option to another value.
# app/config/security.yml
security:
firewalls:
main:
form_login:
target_path_parameter: redirect_url
<!-- app/config/security.xml -->
<config>
<firewall>
<form-login
target_path_parameter="redirect_url"
/>
</firewall>
</config>
// app/config/security.php
$container->loadFromExtension('security', array(
'firewalls' => array(
'main' => array(
'form_login' => array(
'target_path_parameter' => redirect_url,
),
),
),
));
In addition to redirecting the user after a successful login, you can also set
the URL that the user should be redirected to after a failed login (e.g. an
invalid username or password was submitted). By default, the user is redirected
back to the login form itself. You can set this to a different route (e.g.
login_failure
) with the following config:
# app/config/security.yml
security:
firewalls:
main:
form_login:
# ...
failure_path: login_failure
<!-- app/config/security.xml -->
<config>
<firewall>
<form-login
failure_path="login_failure"
/>
</firewall>
</config>
// app/config/security.php
$container->loadFromExtension('security', array(
'firewalls' => array(
'main' => array(
// ...
'form_login' => array(
// ...
'failure_path' => 'login_failure',
),
),
),
));