使用映射跨网络上的多个域登录Cookie

时间:2017-12-01 作者:David Sword

我有一个子目录Wordpress网络,有超过50个站点,它位于wordpress.example.com.

我的网络中的客户johndoe将(有意)拥有一个包含主站点的后端(出于品牌和所有权原因):

wordpress.example.com/johndoe/wp-admin/

然后通过域映射WordPress MU Domain Mapping By Donncha O Caoimh, 前端:

johndoe.com/


我的问题是登录cookie只与wordpress.example.com 而且完全不相关也不知道johndoe.com, 因此前端无法识别用户是否实际登录。这将产生:

前端无用户工具栏“预览更改”按钮不起作用is_user_logged_in() 前端不工作通过禁用“远程登录”,前端页面生成器插件将不工作我知道我可以使网站后端johndoe.com/wp-admin/ 这将解决上述所有问题。然而,为后端保留主域是至关重要的。在我所有的阅读资料中,我都没有找到一个解决方案,我已经把这个问题搁置多年了。

我知道Wordpress。com(本身由Wordpress网络提供支持)似乎已经解决了这个问题。登录wordpress时。com冒险使用随机wordpress。com博客类https://longitudes.ups.com 我能看到我的。com登录,并且工具栏似乎不是iframe或任何愚蠢的操作。

所以,我的问题是,如果Wordpress站点的前端域与后端域不同,那么是否有必要将登录cookie与两者绑定?如果答案是“你不能”(正如我所有的研究结果所显示的那样),那么我的后续问题是,Automatic的员工是如何做到这一点的?

2 个回复
最合适的回答,由SO网友:kierzniak 整理而成

WordPress通过检查来决定您是否登录AUTH_COOKIELOGGED_IN_COOKIE. 正如您所注意到的,这些cookies设置在相同的位置,比如说,A 您的站点所在的域。将相同的Cookie添加到第二个B 域将使您的用户在两个AB 域。当然,从域设置cookieA 第二次B 域将是一个巨大的安全漏洞,所以您必须从域发送cookie值A 到域B 并在域中设置这些cookieB.

这就是我们要做的:

读取CookieAUTH_COOKIELOGGED_IN_COOKIE 在域上AAUTH_COOKIE 和LOGGED_IN_COOKIE 来自域A 到域BAUTH_COOKIE 和LOGGED_IN_COOKIE 在域上Bset_auth_cookie 和set_logged_in_cookie. 在域上设置CookieB 用户浏览器必须位于站点上B 所以我们需要从域重定向用户A 到域B 具有cookie值。使用重定向GET 参数不是选项,Cookie是安全敏感的,我们必须使用POST 要求为了重定向用户并通过POST发送Cookie数据,我们可以创建简单的html表单,url指向域B. 重定向用户后,我们可以在域上设置cookieB 并将用户返回到域A.

我为我的实现创建了工作代码。

/**
 * DOMAIN A PART PLUGIN
 */

class WPSE_287556_Send_Cookies {

    /**
     * Domain which user have to be redirected
     *
     * @var array
     */
    private $domainB = \'example.com\';

    /**
     * Array of cookies to send
     *
     * @var array
     */
    private $cookies = array();

    /**
     * WPSE_287556_Send_Cookies constructor.
     */
    public function __construct()
    {
        /**
         * Define plugin related hooks
         */
        $this->define_hooks();
    }

    /**
     * Save auth and logged in cookies to array
     */
    public function save_cookie( $cookie, $expire, $expiration, $user_id, $scheme, $token ) {

        $this->cookies[] = $data = array(
            \'cookie\' => $cookie,
            \'expire\' => $expire,
            \'scheme\' => $scheme,
        );
    }

    /**
     * Display redirect post form
     *
     * We should not redirect user with cookies in get parameters because this is
     * no safe. We also can not redirect user with post parameters. We can create
     * html post form and submit it with js.
     */
    public function display_redirect_form( $redirect_to, $requested_redirect_to, $user ) {

        if( is_array( $this->cookies ) && !empty( $this->cookies ) ):

            $url = ( is_ssl() ) ? \'https://\' : \'http://\' . $this->domainB . \'/\';
            ?>

            <form action="<?php echo esc_url( $url ); ?>" method="post" style="display: none;" id="post_redirect_form">

                <input type="hidden" name="action" value="set_cookies" >

                <?php foreach($this->cookies as $index => $cookie): ?>
                    <input type="hidden" name="cookies[<?php esc_attr_e( $index ); ?>][cookie]" value="<?php esc_attr_e( $cookie[\'cookie\'] ); ?>" >
                    <input type="hidden" name="cookies[<?php esc_attr_e( $index ); ?>][expire]" value="<?php esc_attr_e( $cookie[\'expire\'] ); ?>" >
                    <input type="hidden" name="cookies[<?php esc_attr_e( $index ); ?>][scheme]" value="<?php esc_attr_e( $cookie[\'scheme\'] ); ?>" >
                <?php endforeach; ?>

                <input type="hidden" name="redirect_to" value="<?php esc_attr_e( $redirect_to ); ?>" >
            </form>
            <script> document.getElementById(\'post_redirect_form\').submit(); </script>

            <?php exit; ?>

        <?php endif;

        return $redirect_to;
    }

    /**
     * Define plugin related hooks
     */
    private function define_hooks() {

        /**
         * Save cookies hook
         */
        add_action( \'set_auth_cookie\', array($this, \'save_cookie\'), 10, 6 );
        add_action( \'set_logged_in_cookie\', array($this, \'save_cookie\'), 10, 6 );

        /**
         * Display redirect post form
         *
         * This filter is used to modify redirect url after login. There is no
         * better place to modify page content after user login. Additionally
         * we have access to $redirect_to url which we can use later.
         */
        add_filter(\'login_redirect\', array( $this, \'display_redirect_form\' ), 10, 3);
    }
}

new WPSE_287556_Send_Cookies();

/**
 * END OF DOMAIN A PART PLUGIN
 */

/**
 * DOMAIN B PART PLUGIN
 */

class WPSE_287556_Set_Cookies {

    /**
     * WPSE_287556_Set_Cookies constructor.
     */
    public function __construct()
    {
        /**
         * Define plugin related hooks
         */
        $this->define_hooks();
    }

    /**
     * Set auth and logged in cookies
     */
    public function set_cookies() {

        // Check if request is "set auth cookie" request
        if( $_SERVER[\'REQUEST_METHOD\'] === \'POST\' && isset( $_POST[\'action\'] ) && $_POST[\'action\'] === \'set_cookies\' ) {

            $args = array(
                \'redirect_to\'   => FILTER_SANITIZE_URL,
                \'cookies\'    => array(
                    \'filter\' => FILTER_SANITIZE_STRING,
                    \'flags\'  => FILTER_REQUIRE_ARRAY,
                ),
            );

            // Read and filter all post params
            $post = filter_input_array(INPUT_POST, $args);

            $redirect_to = $post[\'redirect_to\'];
            $cookies     = $post[\'cookies\'];

            foreach( $cookies as $cookie_params ){

                $scheme = $cookie_params[\'scheme\'];
                $cookie = $cookie_params[\'cookie\'];
                $expire = (int) $cookie_params[\'expire\'];

                // Decide which cookie to set
                switch( $scheme ) {

                    case \'logged_in\':

                        // Set logged in cookie, most of the code is from wp_set_auth_cookie function
                        setcookie( LOGGED_IN_COOKIE, $cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);

                        if ( COOKIEPATH != SITECOOKIEPATH )
                            setcookie(LOGGED_IN_COOKIE, $cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);

                        break;

                    case \'secure_auth\':
                    case \'auth\':

                        // Set auth cookie, most of the code is from wp_set_auth_cookie function
                        if ( $scheme === \'secure_auth\' ) {
                            $auth_cookie_name = SECURE_AUTH_COOKIE;
                        } else {
                            $auth_cookie_name = AUTH_COOKIE;
                        }

                        setcookie($auth_cookie_name, $cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, is_ssl(), true);
                        setcookie($auth_cookie_name, $cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl(), true);

                        break;
                }
            }

            // Redirect user to previous site
            header( \'Location: \' . esc_url( $redirect_to ) );
            exit;
        }
    }

    /**
     * Define plugin related hooks
     */
    private function define_hooks() {

        /**
         * Set cookies from request
         */
        add_action( \'init\', array($this, \'set_cookies\'));
    }
}

new WPSE_287556_Set_Cookies();

/**
 * END OF DOMAIN B PART PLUGIN
 */

SO网友:Serkan Algur

你能在你的wp-config.php 文件

define(\'ADMIN_COOKIE_PATH\', \'/\');
define(\'COOKIE_DOMAIN\', \'\');
define(\'COOKIEPATH\', \'\');
define(\'SITECOOKIEPATH\', \'\'); 
此外,请检查您的多站点定义。

define(\'WP_ALLOW_MULTISITE\', true);
define(\'MULTISITE\', true);
define(\'SUBDOMAIN_INSTALL\', false);
define(\'DOMAIN_CURRENT_SITE\', \'your-domain.com\');
define(\'PATH_CURRENT_SITE\', \'/\');
define(\'SITE_ID_CURRENT_SITE\', 1);
define(\'BLOG_ID_CURRENT_SITE\', 1);
define(\'SUNRISE\', \'on\');

结束

相关推荐

是否使用wp_LOGIN_FORM函数使用自定义用户表登录?

是否可以使用WP内置功能wp_login_form 在自定义非wp用户表上??如果我可以在非wp用户表中作为参数,那就太棒了$args = array( \'redirect\' => home_url(), \'id_username\' => \'user\', \'id_password\' => \'pass\', \'table_user\' =>mycustom_table )