【问题标题】:Magento 2 session data gets deleted in google chromeMagento 2 会话数据在谷歌浏览器中被删除
【发布时间】:2020-08-28 14:34:48
【问题描述】:

问题:
当我的 magento2.3 应用程序将用户重定向到支付网关时,我可以访问所有会话数据。但是当它从那里返回时,它没有结帐会话数据或任何会话数据。这只发生在谷歌浏览器上

我已经探索过的东西
从 google chrome 发行说明 (https://www.chromium.org/updates/same-site) 我可以看到他们已将 samesite 默认值更改为“Lax”,并且禁用此功能。

正在寻找解决方案
我想为我对任何第三方服务的所有传出请求赋予 samesite=None 值。任何帮助或领导将不胜感激。

【问题讨论】:

    标签: php magento magento2


    【解决方案1】:

    您可以尝试按照以下步骤设置 samesite=None..

    文件:etc/frontend/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Framework\View\Element\Js\Cookie">
            <plugin name="afterGetPath" type="namespace\module\Plugin\View\Element\Js\ManagePath" sortOrder="10"/>
        </type>
    </config>
    

    文件:插件/视图/元素/Js/ManagePath.php

    namespace namespace\module\Plugin\View\Element\Js;
    
    use Magento\Framework\View\Element\Js\Cookie;
    
    class ManagePath
    {
        public function afterGetPath(\Magento\Framework\View\Element\Js\Cookie $subject, $path)
        {
            
            if (preg_match('/SameSite/', $path)) {
                 $path_array = explode(';', $path);
                 $path = $path_array[0];
            }
            
            return $path;
        }
    }
    

    文件:etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
        <preference for="Magento\Framework\Session\Config\ConfigInterface" type="namespace\module\Session\CustomConfig"/>
    </config>
    
    

    文件:Session/CustomConfig.php

    
    namespace namespace\module\Session;
    
    use Magento\Framework\Session\Config as DefaultConfig;
    
    class CustomConfig extends DefaultConfig
    {
        public function setCookiePath($path, $default = null)
        {   
            parent::setCookiePath($path, $default);
    
            $path = $this->getCookiePath();
    
            //check and update path of cookie
            if (!preg_match('/SameSite/', $path)) {
                $path .= '; SameSite=None';
                $this->setOption('session.cookie_path', $path);
            }
    
            return $this;
        }
    }
    
    

    注意:用您的命名空间模块替换命名空间和模块。

    【讨论】:

    • 像魅力一样工作。
    【解决方案2】:

    由于我没有足够的声誉来评论已接受的答案,我必须指出,对我来说它不起作用,因为 Chrome 要求将 SameSite 设置为“无”的所有 cookie 标记为安全。 修复正在添加:

    $path .= '; SameSite=None ; secure';
    

    如果不将它们标记为安全,我将无法将商品添加到购物车。

    为我工作,希望它可以帮助遇到同样问题的其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2013-11-10
      相关资源
      最近更新 更多