IdentityServer--vue

本文介绍如何使用oidc-client库在项目中实现身份验证流程,包括安装库、配置认证参数、处理登录与登出操作及获取用户信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装oidc-client库

npm install oidc-client

项目开启页

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

<script>
import Oidc from 'oidc-client'
var config = {
  authority: 'http://localhost:9000',   
  client_id: 'adminweb',     
  redirect_uri: 'http://localhost:8090/#/callback',
  response_type: 'code',
  scope: 'openid profile offline_access aspid',
  post_logout_redirect_uri: 'http://localhost:8090'
}
var mgr = new Oidc.UserManager(config)
export default {
  beforeCreate () {
    mgr.signinRedirect()
  },
  data () {
    return {
    }
  },
  methods: {
  }
}
</script>

<style>

</style>

登录后 跳转页`

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

<script>
import Oidc from 'oidc-client'
new Oidc.UserManager({response_mode: 'query'})
  .signinRedirectCallback()
  .then(function () {
    // debugger
    window.location.replace('/#/home/index')
  })
  .catch(function (e) {

  })
export default {

}
</script>

<style>

</style>

项目主页

<template>
  <div>
    <button @click="api">查看token</button>
    <pre>{{res}}</pre>
  </div>
</template>

<script>
import Oidc from 'oidc-client'

var config = {
  authority: 'http://localhost:9000',
  client_id: 'adminweb',
  redirect_uri: 'http://localhost:8090/#/callback',
  response_type: 'code',
  scope: 'openid profile offline_access aspid',
  post_logout_redirect_uri: 'http://localhost:8090'
}
var mgr = new Oidc.UserManager(config)
export default {
  data () {
    return {
      res: 'My Home'
    }
  },

  methods: {
    api () {
      mgr.getUser().then(user => {
        console.log(user)
        if (user) {
        // this.res = ("User logged in", user.profile);注意闭包
          this.res = ('User logged in', user)
        } else {
          this.res = ('User not logged in')
        }
      })
    },
    refresh () {

    }
  },
  mounted () {
  }
}
</script>

<style scoped>
</style>

退出登录

路由配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值