希望您能帮助我。 我在尝试使用symfony2登录时遇到了一些问题。基本上,我在login_check中遇到302错误。
我来自这个网址。
Symfony2 Authentication "login_check" path not found
我认为这是我的解决方案,但事实并非如此。配置看起来不错。 尽管我可以访问/ panel,但我试图删除所有access_control和仍然相同的问题。 任何人都可以提出有关正在发生的事情的更多想法?
谢谢
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
always_use_default_target_path: true
default_target_path: /user
# login_path: /register
logout: true
anonymous: true
remember_me:
key: "%secret%"
lifetime: 31536000 # 365 days in seconds
always_remember_me: true
path: /
domain: ~ # Defaults to the current domain from $_SERVER
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/panel/, role: ROLE_USER }
- { path: ^/admin/, role: ROLE_ADMIN }
其他信息
调试:路线
fos_user_security_login GET | POST任何/登录
fos_user_security_check POST任何/ login_check
loginAction
SecurityController类扩展了Controller { 公共功能loginAction(Request $ request) {
/** @var $session \Symfony\Component\HttpFoundation\Session\Session */
$session = $request->getSession();
if (class_exists('\Symfony\Component\Security\Core\Security')) {
$authErrorKey = Security::AUTHENTICATION_ERROR;
$lastUsernameKey = Security::LAST_USERNAME;
} else {
// BC for SF < 2.6
$authErrorKey = SecurityContextInterface::AUTHENTICATION_ERROR;
$lastUsernameKey = SecurityContextInterface::LAST_USERNAME;
}
// get the error if any (works with forward and redirect -- see below)
if ($request->attributes->has($authErrorKey)) {
$error = $request->attributes->get($authErrorKey);
} elseif (null !== $session && $session->has($authErrorKey)) {
$error = $session->get($authErrorKey);
$session->remove($authErrorKey);
} else {
$error = null;
}
if (!$error instanceof AuthenticationException) {
$error = null; // The value does not come from the security component.
}
// last username entered by the user
$lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
if ($this->has('security.csrf.token_manager')) {
$csrfToken = $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
} else {
// BC for SF < 2.4
$csrfToken = $this->has('form.csrf_provider')
? $this->get('form.csrf_provider')->generateCsrfToken('authenticate')
: null;
}
return $this->renderLogin(array(
'last_username' => $lastUsername,
'error' => $error,
'csrf_token' => $csrfToken,
));
}
/**
* Renders the login template with the given parameters. Overwrite this function in
* an extended controller to provide additional data for the login template.
*
* @param array $data
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderLogin(array $data)
{
return $this->render('FOSUserBundle:Security:login.html.twig', $data);
}
public function checkAction()
{
throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}
public function logoutAction()
{
throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
}
}
表格
<form action="{{ path("fos_user_security_check") }}" method="post">
{% if error %}
<div class="text-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}"/>
<h3 style="margin-bottom: 70px;"><strong>Sign in</strong></h3>
<h4>Email<input placeholder="email or username" name="_username"
value="{{ last_username }}"
required="required" type="text" id="username"></h4>
<h4>Password<input id="password" name="_password" type="password" required="required"></h4>
<a href=""><button class="btn-aurovine" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}">Sign in</button></a>
<p style="color:#4d4d4d; font-size: 15px;margin-top: 10px;">forgot password? <a href="{{ path('fos_user_resetting_request') }}">Click here.</a></p>
<div style="width: 70%; margin: 0 auto;">
<div style="min-width:100%;height: 20px; border-bottom: 1px solid #595959;text-align: center;">
<span style="font-size: 20px; background-color:white;margin-top:10px;padding: 0px 20px;">
<strong>OR</strong></span>
</div>
</div>
<a href="{{ path('fos_user_registration_register') }}"><button type="button" class="btn btn-add btn-full btn-sm btn-responsive btn-aurovine" style="margin-bottom: 20px;">SIGN UP</button></a>
</form>
18/11/19注意
虽然是4.6版,但我遇到了此链接,但我不知道如何检查这是否是我的问题
https://symfony.com/doc/current/security/user_provider.html
选中“了解如何从会话中刷新用户”