单点登录token存本地

单点登录token存本地localstorage

1、认证中心获取token存储在本地 -> 要进入子系统时,使用postmessage向子系统发送token,并存储在子系统的localstorage中

// 创建一个iframe,在iframe中加载一个跨域的网页
const iframe = document.createElement('iframe')
iframe.style.height = '0'
iframe.style.display = 'none'
iframe.src = `${url}/settoken.html`
document.body.append(iframe)
// 使用postMessage()方法将token传递给iframe
iframe.onload = () => {
  iframe.contentWindow.postMessage(`info|${token}`, url)
  // 监听 子系统 的回调
  this.listener = window.onmessage = (event) => {
    if (event.data === 'jump') {
    // 跳转到子系统
      window.open(url)
      this.listener = null
    }
  }
}

2、子系统的public文件夹下创建settoken.html,用来存储token

<!DOCTYPE html>
<html lang="zh">
  <head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width,initial-scale=1.0">
	<link rel="icon" href="<%= BASE_URL %>favicon.ico">
	<title></title>
  </head>
  <body>
    <noscript>
  	  <strong>抱歉,javascript被禁用,请开启后重试。</strong>
    </noscript>
    <div >1111111</div>
  </body>
 
  <script>
    // 监听跨域跳转获取新token存入localStorage实现获取访问令牌
    let listener = window.onmessage = (event) => {
      if (Object.prototype.toString.call(event.data).slice(8, -1) === 'String') {
        const [key, value] = event.data.split('|')
        if (key === 'info') {
          localStorage.setItem('Admin-Token',value)
          // 告诉上层 window,已保存token,可以开始跳转了
          window.parent.postMessage('jump', '*')
          listener = null
        }
      }
    }
  </script>
</html>

注意:这里使用window.onmessage,用window.addEventListener会导致重复触发

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解如何在Vue3中实现登录并将token储在本地吗? 首先,在登录成功后,将token储在本地可以使用浏览器提供的localStorage或sessionStorage。在Vue3中,可以在Vue实例的创建过程中,通过创建一个全局的$store对象来实现储和管理状态数据。 以下是一些示例代码,可以帮助您更好地了解如何在Vue3中实现登录并将token储在本地: 1. 在登录页面中,您可以通过表单提交来处理用户输入的登录信息,如下所示: ``` <template> <form @submit.prevent="login"> <label> Email: <input type="email" v-model="email" required> </label> <label> Password: <input type="password" v-model="password" required> </label> <button type="submit">Login</button> </form> </template> <script> export default { data() { return { email: '', password: '' } }, methods: { async login() { // 发送登录请求并获取token const { token } = await api.login(this.email, this.password) // 将token储到本地 localStorage.setItem('token', token) // 跳转到首页或其他需要登录的页面 this.$router.push('/') } } } </script> ``` 2. 在Vue实例的创建过程中,可以创建一个全局的$store对象来储和管理状态数据,如下所示: ``` import { createStore } from 'vuex' const store = createStore({ state() { return { token: localStorage.getItem('token') || null } }, mutations: { setToken(state, token) { state.token = token } } }) export default store ``` 在上面的代码中,我们通过使用localStorage.getItem('token')来获取本地储的token值,并将其储在Vuex的状态管理中。 3. 在需要使用token的地方,可以通过Vuex的getters来获取储在状态管理中的token值,如下所示: ``` <template> <div v-if="$store.getters.isAuthenticated"> <!-- 显示需要登录后才能访问的内容 --> </div> <div v-else> <p>Please login to access this content.</p> </div> </template> <script> export default { computed: { isAuthenticated() { return !!this.$store.state.token } } } </script> ``` 在上面的代码中,我们使用了Vuex的getters来获取储在状态管理中的token值,并将其用于判断用户是否已经登录。 希望这些示例代码能够帮助您更好地了解如何在Vue3中实现登录并将token储在本地。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值