vue项目实现单点登陆

vue项目使用oidc-client实现单点登陆

redirect

首先我们需要在路由钩子页面增加判断,如果没有token,则重定向到服务器进行单点登录

oidc.js 单点登录所需配置项

export const identityServerBase = 'http://baidu.com';//目标服务器登录地址
export const vueBase = 'http://localhost:8080'

// 参考文档 https://github.com/IdentityModel/oidc-client-js/wiki
export const openIdConnectSettings = {
    authority: `${identityServerBase}`,
    client_id: `vue-client`,
    redirect_uri: `${vueBase}/signin-oidc`,//这里是跳转回来的路由
    post_logout_redirect_uri: `${vueBase}`,
    silent_redirect_uri: `${vueBase}/redirect-silentrenew`,
    scope: 'openid profile api1',
    response_type: `id_token token`,
    automaticSilentRenew: true,
};

permission.js

import router from "./router";
import Oidc from "oidc-client";
import { openIdConnectSettings } from "./oidc";

router.beforeEach((to,from,next)=>{
   if(localStorage.getItem('token')){
      next()
   }else{
	  let mgr = new Oidc.UserManager(openIdConnectSettings);
	  mgr.signinRedirect(); //执行重定向 
   }

})

这个是我自己的项目这样用router钩子来判断权限,如果你们没有用到也没关系,忽略掉router,直接执行else中的内容,如果没有token,页面会跳转到服务器进行登录

下面是登录完成后会回到我们系统中的操作

odic.vue

<template>
    <div></div>
</template>

<script>
import Oidc from "oidc-client";

export default {
    props: {},
    data() {
        return {};
    },

    components: {},

    methods: {},
    created() {
        new Oidc.UserManager()
            .signinRedirectCallback()
            .then(() => {
                this.$router.push("/save-auth");
            })
            .catch(function(e) {});
    }
};
</script>

执行回调后,跳转到save-auth页面

save-auth.vue

<script>
import Oidc from "oidc-client";
import { openIdConnectSettings } from "./oidc";
export default {
  name: 'SaveAuth',
  created() {
    this.mgr =  new Oidc.UserManager(openIdConnectSettings);
    this.mgr.getUser().then((user) => {
      localStorage.setItem('userInfo',JSON.stringify(user))
      this.$router.push('/')
    })
  },
  render: function(h) {
    return h() // avoid warning message
  }
}
</script>

保存我们所需要的用户信息后跳转到首页,至此整体流程结束。

原文地址:vue项目实现单点登陆

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值