简体   繁体   中英

Symfony 3 Form Login

I've been trying for a couple of days now ... and still can't make this work I've read the documentation page over an over, I'm going crazy and I can't understand what is wrong.

It's very important for me to know and learn the way Symfony works because my new job requires me to work with it.

So I went to the documentation page at : http://symfony.com/doc/current/cookbook/security/entity_provider.html#security-config-entity-provider

security.yml

encoders:
    Paul\FrontBundle\Entity\User:
        algorithm: bcrypt

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    db_users:
        entity:
            class: Paul\FrontBundle\Entity\User
            property: username

firewalls:
    admin:
        pattern: ^/admin
        provider: db_users
        form_login:
            check_path: admin_login_check
            login_path: admin_login

    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~

My user entity implements the UserInterface What is wrong ? Can anyone please explain me what I'm doing wrong ?

Thanks !

OK, so the problem was the security context , for those who are interested this link will explain more.

Now what I've done is the following:

In security.yml

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    protected_area:
        pattern: ^/protected
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
            default_target_path: /protected
        provider: database_users
        logout:
            path: logout
            target: /
            success_handler: ~
            invalidate_session: true

    main:
        anonymous: ~
    access_control:
        - { path: ^/protected/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/protected, roles: ROLE_USER }

In routing.yml

login:
   path:     /protected/login
   defaults: { _controller: PaulDemoBundle:Security:login }

login_check:
   path:     /login_check

logout:
   path:     /protected/logout

So to explain it more, The login form is now in the context of protected_area firewall before /login , after /protected/login .

Also pay attention to the access_control node.

I hope this will help others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM