简体   繁体   English

Symfony + FOSUserBundle无法登录

[英]Symfony + FOSUserBundle can't login

I'm using Symfony 3.1 with FOSUserBundle. 我正在将Symfony 3.1与FOSUserBundle一起使用。

I read the docs and integrated FOS in Symfony, here all right, the registration seems to work well and persists the data to the DB, BUT, if I try to login I get redirected again to the login page as an anonymous user. 我在Symfony中阅读了文档和集成的FOS,在这里,注册似乎运行良好,并将数据持久保存到DB,BUT中,如果尝试登录,我将以匿名用户的身份再次重定向到登录页面。

I checked the Symfony Profiler, and I can see that when it's called the path /login_check (in the profiler) the user result as authenticated, but then after the /login_check phase, when I get redirected, the user is as anonymous... 我检查了Symfony Profiler,可以看到当它被称为/ login_check路径(在Profiler中)时,用户结果为已认证,但是在/ login_check阶段之后,当我被重定向时,该用户为匿名用户...

-- -

/login_check from profiler: / login_check来自探查器:

Property Value 适当的价值

Roles [ROLE_USER] 角色[ROLE_USER]

Inherited Roles none 继承的角色无

Token class Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken 令牌类Symfony \\ Component \\ Security \\ Core \\ Authentication \\ Token \\ UsernamePasswordToken

-- -

The sessions seems to exist (dump(app.session)) is not empty, but no stores data of user. 会话似乎存在(dump(app.session))不为空,但没有存储用户数据。

That's my security.yml: 那就是我的security.yml:

security: 安全:

encoders:
    FOS\UserBundle\Model\UserInterface: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

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

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            login_path: /login
            check_path: /login_check
            default_target_path: /redirLogIn
#                csrf_token_generator: security.csrf.token_manager

        logout:       true
        anonymous:    true

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
#        - { path: ^/admin.*, role: ROLE_ADMIN}
#        - { path: ^/, role: ROLE_USER}

(I disabled the csrf token due to problems on registration) (由于注册问题,我禁用了csrf令牌)

(This is an intranet, there is no "homepage", as you open the site you are requested to login) (这是一个Intranet,没有“主页”,因为您打开需要登录的网站)

Why this? 为什么这个?

Thanks 谢谢

You need to improve the indentation in the file 您需要改善文件中的缩进

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN
    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory:
            memory: ~
        fos_userbundle:
            id: fos_user.user_provider.username_email
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            # activate different ways to authenticate

            # http_basic: ~
            # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                # if you are using Symfony < 2.8, use the following config instead:
                # csrf_provider: form.csrf_provider

            logout:       true
            anonymous:    true

    access_control:
            - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/admin/, role: ROLE_ADMIN }

I'm not sure I understand the question you are asking, but maybe it's why you are being directed to login when you open the site? 我不确定我是否理解您所提出的问题,但是也许这就是为什么您打开网站时会被定向登录?

If so, this is why: 如果是这样,这就是原因:

default_target_path: /redirLogIn

Change the above to the path (route) you need. 将以上内容更改为所需的路径(路线)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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