使用costom用户提供程序在我的表单中循环登录

时间:2017-05-20 20:00:40

标签: symfony login

我是symfony的通知,我的身份验证有问题,我搜索每个解决方案,为我做任何工作

我被困在我的表单登录中: 这是我的security.yml

encoders:
    UserBundle\Security\User: bcrypt
role_hierarchy:
    ROLE_AGENT:       ROLE_USER
providers:
    user_provider:
        id: user.user_provider
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/agent, role: ROLE_AGENT }
firewalls:
    login_firewall:
        pattern:   ^/login$
        anonymous: ~
    agent_firewall:
        pattern: ^/agent
        provider: user_provider
        anonymous: ~
        form_login:
            login_path: login_agent
            check_path: login_agent
        logout:
            path:   /logout
            target: /login
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~

我的services.yml

user.user_provider:
    class: UserBundle\Security\UserProvider
    arguments: ['@doctrine.orm.entity_manager']

我的login.html.twig

<form action="{{ path('login_agent') }}" 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" />

    {#
        If you want to control the URL the user
        is redirected to on success (more details below)
        <input type="hidden" name="_target_path" value="/account" />
    #}

    <button type="submit">login</button>
</form>

我的路线登录:

/**
 * @Route("/login_agent", name="login_agent")
 */
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::login.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
    ));
}

我希望有人可以帮助我,我被封锁了,我不知道我能做些什么

1 个答案:

答案 0 :(得分:0)

login_path设置为/login或类似设置(而不是check_path)并相应地更改loginAction

当您将内容发布到check_path时Symfony会直接相交,如果身份验证不成功(然后您可以检查错误,则会重新链接到login_path

相关文档:http://symfony.com/doc/current/security/custom_password_authenticator.html。我还建议您查看FOSUserBundle,看看他们是如何解决这个问题的:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/SecurityController.php。相关代码如下:

class SecurityController extends Controller
{
    /**
     * the login path /login
     */
    public function loginAction(Request $request)
    {
        $session = $request->getSession();
        //checking errors & setting variables

        return $this->render('@FOSUser/Security/login.html.twig', array(
            'last_username' => $lastUsername,
            'error' => $error,
            'csrf_token' => $csrfToken,
        ));
    }

    /**
     * the check path /login_check
     */
    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.');
    }
}