使用Symfony Security创建简单的登录表单

4

我尝试按照这个教程做登录表单: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 }

安全控制器:

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,
            )
        );
    }

}

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


如果您正在尝试使用ROLE_ADMIN登录,则可能会遇到问题。您还没有配置角色层次结构,告诉Symfony哪个角色继承其他角色。在access_controls中,您只允许ROLE_USER进入。也许这就是问题所在?此外,您应该在防火墙中提供default_target_path... - Chris P. Bacon
1个回答

3

check_path更改为其他内容,例如login_check,并在控制器中添加login_checklogout操作:

/**
 * @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_check 执行提交(post)操作:

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

谢谢,但这并没有帮上忙。看起来安全包无法处理POST请求。 - doubleB
你确定表单提交到了 check_path 吗?此外,你的 yml 文件中的缩进似乎有问题。是四个空格吗? - Rvanlaak
我忘记更改操作属性,但现在我收到了“登录已过期”的警告。 - doubleB
这意味着安全层没有使用 login_check 作为 check_path,否则请求将被拦截。您确定您的配置有正确的路径吗? - Rvanlaak
你是指这个链接吗:http://symfony.com/doc/current/cookbook/security/form_login_setup.html#be-sure-the-login-page-isn-t-secure-redirect-loop - Rvanlaak
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接