Symfony 2 Guard 组件和一个普通的登录表单 [英] Symfony 2 Guard component and a normal login form

查看:21
本文介绍了Symfony 2 Guard 组件和一个普通的登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 Guard 组件.我找到的有关它的唯一信息是官方文档.

I'm trying to use the new Guard component. The only information I found about it are the official docs.

文档中的示例使用 Guard 来验证 API 的请求.

The example from the docs uses Guard to authenticate API's requests.

根据文档,它也可以与普通的登录表单一起使用,这是我想做的.

According to the docs it can be also used with a normal login form, what I want to do.

我复制了那个例子并替换了两个函数:

I copied that example and replaced two functions:

public function getCredentials(Request $request)
{
    return array(
        'username' => $request->request->get('_username'),
        'password' => $request->request->get('_password'),
    );
}

public function getUser($credentials, UserProviderInterface $userProvider)
{
    $email = $credentials['username'];

    $user = $this->em->getRepository('Entity\User')
        ->findOneBy(array('email' => $email));

    return $user;
}

security.yml:

security.yml:

providers:
    user_provider:
        entity:
            class: Entity\User
            property: email

firewalls:
    user:
        switch_user: { role: ROLE_ADMIN }
        pattern:  ^/
        anonymous: ~
        provider: user_provider
        form_login:
            check_path: login_check
            login_path: login
        logout:
            path:   logout
            target: login
        guard:
            authenticators:
                - app.ldap.authenticator
        stateless: false

问题是 Symfony 不会持久化我的会话.

The problem is that Symfony doesn't persist my session.

仅当我为每个请求发送数据表单时我才登录.是否可以像普通登录表单一样发送一次这些数据?

I'm logged in only if I send data form for every request. Is it possible to send this data once just like a normal login form?

推荐答案

我找到了.我在 security.yml

I found it. I had in security.yml

always_authenticate_before_granting:  true

从另一个项目复制:).我删除了它,现在它可以工作了

copied from another project :). I deleted it and now it works

这篇关于Symfony 2 Guard 组件和一个普通的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆