简体   繁体   English

Symfony2 SessionUnavailable Exception

[英]Symfony2 SessionUnavailable Exception

Is there someone who knows the Symfony authentication very well? 是否有人非常了解Symfony身份验证?

Because every time I try to login with a new browser after startup, I get the SessionUnavailable Exception with the text "No session available, it either timed out or cookies are not enabled." 因为每次我尝试在启动后使用新浏览器登录时,都会收到SessionUnavailable Exception,其中包含“没有可用会话,超时或未启用cookie”的文本。 Why does it not make a new session when I'm using a new browser after startup? 为什么我在启动后使用新浏览器时没有进行新的会话?

I dug a little deeper and found one option "require_previous_session" that is set to true in: vendor/symfony/symfony/src/Symfony/Component/Security/HTTP/Firewall/AbstractAuthenticationListener.php, but I don't what to set it to false without knowing what it actually does. 我挖得更深一点,发现一个选项“require_previous_session”设置为true:vendor / symfony / symfony / src / Symfony / Component / Security / HTTP / Firewall / AbstractAuthenticationListener.php,但我不知道如何设置它在不知道它实际做了什么的情况下做错。

Any tips would be great. 任何提示都会很棒。

The Security.yml file is quite big because of the role system, but take a look here: Security.yml 由于角色系统,Security.yml文件非常大,但请看一下: Security.yml

The require_previous_session setting is a bit oblique but can (hopefully) be explained with a bit of code. require_previous_session设置有点倾斜但可以(希望)用一些代码来解释。

So ordinarilly, when you set up a standard form login (like the docs ), in your security.yml file you set up your firewall with a pattern (say /user ) and also set the anonymous option. 因此,当您在security.yml文件中设置标准表单登录(如文档 )时,您可以使用模式(例如/user )设置防火墙,并设置anonymous选项。 Now down in your access control you set the login page (say /user/login ) to have a role of IS_AUTHENTICATED_ANONYMOUSLY , like so: 现在,在您的访问控制中,您将登录页面(例如/user/login )设置为IS_AUTHENTICATED_ANONYMOUSLY角色,如下所示:

firewalls:
    default:
        pattern: ^/user
        anonymous: ~
        form_login:
            login_path: /user/login
            check_path: /user/login_check

access_control:
    - { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/user, roles: ROLE_USER }

Now, what happens when someone goes to /user is they get forwarded to /user/login ; 现在,当有人去/user时会发生什么,他们会被转发到/user/login ; but when they do, they will have a session created for them (if they didn't already) and their assigned role will be anon (you can check this in the Symfony toolbar when on /user/login ) as allowed by the access_control section above. 但是当他们这样做时,他们会为他们创建一个会话(如果他们还没有)并且他们分配的角色将是anon (你可以在/user/login时在Symfony工具栏中检查这个),如access_control部分所允许的那样以上。

This means whenever someone logs in (ie sends credentials to /user/login_check ) they will already have a session created for them and require_previous_session will be true. 这意味着只要有人登录( /user/login_check凭证发送到/user/login_check ),他们就已经为他们创建了会话,并且require_previous_session将为true。

For most people, this is fine and you won't have to worry about this setting. 对于大多数人来说,这很好,你不必担心这个设置。 However, if you start touching the edges of the security component, for instance, creating your own authentication provider, or disabling security ( security: false for a specific pattern, see the default dev firewall for an example of this) then you can come up against this problem. 但是,如果您开始触摸安全组件的边缘,例如,创建自己的身份验证提供程序,或禁用安全性(特定模式的security: false ,请参阅默认的dev防火墙以获取此示例),那么您可以反对这个问题。

As far as I know, there is no security penalty for not having a session before you log in - I have production sites going where this is the case. 据我所知,在您登录之前没有会话没有安全惩罚 - 我有生产网站在这种情况下。 However, there is a benefit in that you can then use CSRF tokens ( cookbook entry ) on the login form for extra security, meaning that attacks on user accounts are a lot harder. 但是,有一个好处是,您可以在登录表单上使用CSRF令牌( cookbook条目 )以获得额外的安全性,这意味着对用户帐户的攻击要困难得多。

Short version: I wouldn't worry about setting that option if it solves your problem. 简短版本:如果能解决您的问题,我不会担心设置该选项。 Depending on your site size there can be a performance gain for doing so (if you can log into your entire site but unauthenticated users don't need a session) but security wise, you should be good. 这取决于您的站点大小,这样做可以获得性能提升(如果您可以登录到整个站点,但未经身份验证的用户不需要会话)但是安全性方面,您应该做得很好。

Edit, example from above with require_previous_session set to false: 编辑,示例从上面将require_previous_session设置为false:

firewalls:
    default:
        pattern: ^/user
        anonymous: ~
        form_login:
            login_path: /user/login
            check_path: /user/login_check
            require_previous_session: false

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

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