gpt4 book ai didi

php - 使用 Symfony Security 的简单登录表单

转载 作者:可可西里 更新时间:2023-10-31 23:28:50 26 4
gpt4 key购买 nike

我试着用这个教程做登录表单:http://symfony.com/doc/current/cookbook/security/form_login_setup.html

所以,现在我的 security.yml 文件如下所示:

security:
providers:
in_memory:
memory:
users:
ryan:
password: ryanpass
roles: 'ROLE_USER'
admin:
password: kitten
roles: 'ROLE_ADMIN'

encoders:
Symfony\Component\Security\Core\User\User: plaintext

firewalls:
login_firewall:
pattern: ^/login$
anonymous: ~
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: login
check_path: login
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false

access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }

安全 Controller :

class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');

// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();

// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();

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

}

所以基本上,它不能正常工作。发送登录表单后没有任何反应,我不知道为什么。我认为安全配置是错误的。有人可以帮帮我吗?我不知道出了什么问题。

最佳答案

check_path 更改为其他内容,例如 login_check 并将 login_checklogout 操作添加到您的 Controller :

/**
* @Route("/login_check", name="login_check")
*/
public function loginAction()
{
// The security layer will intercept this request, else redirect to login page
$this->addFlash('warning', $this->get('translator')->trans('login_expired'));
return $this->redirect($this->generateUrl('login'));
}

/**
* @Route("/logout", name="logout")
*/
public function logoutAction()
{
// The security layer will intercept this request, else redirect to login page
$this->addFlash('warning', $this->get('translator')->trans('login_expired'));
return $this->redirect($this->generateUrl('login'));
}

还要确保 login_form 向 login_check 发送一个帖子:

<form id="loginForm" action="{{ path('login_check') }}" method="post">

关于php - 使用 Symfony Security 的简单登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35888653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com