安装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>
退出登录
路由配置