单点登录

在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
可以用cross-storage把登录信息放在服务器上
需要在服务器上放一个文件,项目登录后,调用服务器上的程序,把登录信息放在服务器上,其他项目登录时调用服务器上的程序,获取登录信息。
storage.js

import CrossStorage from 'cross-storage'
var storage = new CrossStorage.CrossStorageClient('http://localhost:7000/crossstorage/example/hub.html');//我的地址
/**添加Storage**/
export const addStorage = function(name, value) {
    storage.onConnect().then(function() {
        return storage.set(name, value);
    }).catch(function(err) {
        console.log(err)
    }).then(function() {
        storage.close();
    });
};

/**获取Storage**/
export const getStorage = function(name,callback) {
    storage.onConnect()
        .then(function() {
            return storage.get(name);
        }).then(function(res) {
            callback(res);
        })['catch'](function(err) {
                        console.log(err)
        });
};

/**删除Storage**/
export const delStorage = function(name) {
    storage.onConnect().then(function() {
        return storage.del(name);
    });
};

调用

        addStorage('token', token);
        window.location.reload();
        that.$router.replace('/main');

添加后需要需要刷新


web1
web2

web2可以获取localhost:7000上的token,不用再次登录

可以看看简单的例子

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。