mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-05-02 20:02:45 +08:00
新增SSO前后端分离集成示例 vue2 版本
This commit is contained in:
parent
7ef1305527
commit
bb3d9c6210
22
sa-token-demo/sa-token-demo-sso-client-vue2/.gitignore
vendored
Normal file
22
sa-token-demo/sa-token-demo-sso-client-vue2/.gitignore
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
.DS_Store
|
||||
node_modules
|
||||
/dist
|
||||
|
||||
# local env files
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Log files
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
|
||||
# Editor directories and files
|
||||
.idea
|
||||
.vscode
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
20
sa-token-demo/sa-token-demo-sso-client-vue2/README.md
Normal file
20
sa-token-demo/sa-token-demo-sso-client-vue2/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# sa-token-demo-sso-client-vue2
|
||||
Sa-Token SSO-Client 应用端(前后端分离版-Vue2)
|
||||
|
||||
在线文档:[https://sa-token.cc/](https://sa-token.cc/)
|
||||
|
||||
## 运行
|
||||
先安装依赖
|
||||
``` bat
|
||||
npm install --registry=https://registry.npm.taobao.org
|
||||
```
|
||||
|
||||
运行
|
||||
``` bat
|
||||
npm run serve
|
||||
```
|
||||
|
||||
打包
|
||||
``` bat
|
||||
npm run build
|
||||
```
|
@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
presets: [
|
||||
'@vue/cli-plugin-babel/preset'
|
||||
]
|
||||
}
|
27430
sa-token-demo/sa-token-demo-sso-client-vue2/package-lock.json
generated
Normal file
27430
sa-token-demo/sa-token-demo-sso-client-vue2/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
44
sa-token-demo/sa-token-demo-sso-client-vue2/package.json
Normal file
44
sa-token-demo/sa-token-demo-sso-client-vue2/package.json
Normal file
@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "hello-world",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"serve": "vue-cli-service serve",
|
||||
"build": "vue-cli-service build",
|
||||
"lint": "vue-cli-service lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.1.3",
|
||||
"core-js": "^3.6.5",
|
||||
"vue": "^2.6.11",
|
||||
"vue-router": "^3.6.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vue/cli-plugin-babel": "~4.4.6",
|
||||
"@vue/cli-plugin-eslint": "~4.4.6",
|
||||
"@vue/cli-service": "~4.4.6",
|
||||
"babel-eslint": "^10.1.0",
|
||||
"eslint": "^6.7.2",
|
||||
"eslint-plugin-vue": "^6.2.2",
|
||||
"vue-template-compiler": "^2.6.11"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"extends": [
|
||||
"plugin:vue/essential",
|
||||
"eslint:recommended"
|
||||
],
|
||||
"parserOptions": {
|
||||
"parser": "babel-eslint"
|
||||
},
|
||||
"rules": {}
|
||||
},
|
||||
"browserslist": [
|
||||
"> 1%",
|
||||
"last 2 versions",
|
||||
"not dead"
|
||||
]
|
||||
}
|
BIN
sa-token-demo/sa-token-demo-sso-client-vue2/public/favicon.ico
Normal file
BIN
sa-token-demo/sa-token-demo-sso-client-vue2/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<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>Sa-Token SSO-Client 应用端(前后端分离版-Vue2)</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>
|
||||
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
|
||||
</noscript>
|
||||
<div id="app"></div>
|
||||
<!-- built files will be auto injected -->
|
||||
</body>
|
||||
</html>
|
15
sa-token-demo/sa-token-demo-sso-client-vue2/src/App.vue
Normal file
15
sa-token-demo/sa-token-demo-sso-client-vue2/src/App.vue
Normal file
@ -0,0 +1,15 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<router-view />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'App'
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
</style>
|
BIN
sa-token-demo/sa-token-demo-sso-client-vue2/src/assets/logo.png
Normal file
BIN
sa-token-demo/sa-token-demo-sso-client-vue2/src/assets/logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.7 KiB |
11
sa-token-demo/sa-token-demo-sso-client-vue2/src/main.js
Normal file
11
sa-token-demo/sa-token-demo-sso-client-vue2/src/main.js
Normal file
@ -0,0 +1,11 @@
|
||||
import Vue from 'vue'
|
||||
import App from './App.vue'
|
||||
|
||||
Vue.config.productionTip = false
|
||||
|
||||
import router from './router'
|
||||
|
||||
new Vue({
|
||||
router,
|
||||
render: h => h(App)
|
||||
}).$mount('#app')
|
@ -0,0 +1,34 @@
|
||||
import Vue from 'vue'
|
||||
import Router from 'vue-router'
|
||||
|
||||
Vue.use(Router)
|
||||
|
||||
/**
|
||||
* 路由表
|
||||
*/
|
||||
export const routes = [
|
||||
// 首页
|
||||
{
|
||||
name: 'index',
|
||||
path: "/index",
|
||||
component: () => import('../views/sso-index.vue')
|
||||
},
|
||||
// SSO-登录页
|
||||
{
|
||||
name: 'sso-login',
|
||||
path: '/sso-login',
|
||||
component: () => import('../views/sso-login.vue')
|
||||
},
|
||||
|
||||
// 访问 / 时自动重定向到 /index
|
||||
{
|
||||
path: '/',
|
||||
redirect: '/index'
|
||||
}
|
||||
]
|
||||
|
||||
const router = new Router({
|
||||
routes: routes
|
||||
})
|
||||
|
||||
export default router
|
@ -0,0 +1,36 @@
|
||||
import axios from 'axios'
|
||||
|
||||
// sso-client 的后端服务地址
|
||||
export const baseUrl = "http://sa-sso-client1.com:9001";
|
||||
|
||||
// 封装一下 Ajax 方法
|
||||
export const ajax = function(path, data, successFn) {
|
||||
axios({
|
||||
url: baseUrl + path,
|
||||
method: 'post',
|
||||
data: data,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"satoken": localStorage.getItem("satoken")
|
||||
}
|
||||
}).
|
||||
then(function (response) { // 成功时执行
|
||||
const res = response.data;
|
||||
successFn(res);
|
||||
}).
|
||||
catch(function (error) {
|
||||
return alert("异常:" + JSON.stringify(error));
|
||||
})
|
||||
}
|
||||
|
||||
// 从url中查询到指定名称的参数值
|
||||
export const getParam = function(name, defaultValue){
|
||||
var query = window.location.search.substring(1);
|
||||
var vars = query.split("&");
|
||||
for (var i=0;i<vars.length;i++) {
|
||||
var pair = vars[i].split("=");
|
||||
if(pair[0] == name){return pair[1];}
|
||||
}
|
||||
return(defaultValue == undefined ? null : defaultValue);
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
<!-- 项目首页 -->
|
||||
<template>
|
||||
<div>
|
||||
<h2> Sa-Token SSO-Client 应用端(前后端分离版-Vue2) </h2>
|
||||
<p>当前是否登录:<b>{{isLogin}}</b></p>
|
||||
<p>
|
||||
<router-link :to="loginUrl">登录</router-link>
|
||||
<a :href="logoutUrl">注销</a>
|
||||
</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {baseUrl, ajax} from './method-util.js'
|
||||
|
||||
export default {
|
||||
name: 'App',
|
||||
data() {
|
||||
return {
|
||||
// 单点登录地址
|
||||
loginUrl: '/sso-login?back=' + encodeURIComponent(location.href),
|
||||
// 单点注销地址
|
||||
logoutUrl: baseUrl + '/sso/logout?satoken=' + localStorage.satoken + '&back=' + encodeURIComponent(location.href),
|
||||
// 是否登录
|
||||
isLogin: false
|
||||
}
|
||||
},
|
||||
created() {
|
||||
// 查询当前会话是否登录
|
||||
ajax('/sso/isLogin', {}, function (res) {
|
||||
console.log('/isLogin 返回数据:', res);
|
||||
this.isLogin = res.data;
|
||||
}.bind(this))
|
||||
}
|
||||
}
|
||||
</script>
|
@ -0,0 +1,54 @@
|
||||
<!-- Sa-Token-SSO-Client端-登录页 -->
|
||||
<template>
|
||||
<div></div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {ajax, getParam} from './method-util.js';
|
||||
import router from '../router';
|
||||
|
||||
|
||||
export default {
|
||||
name: 'App',
|
||||
data() {
|
||||
return {
|
||||
back: getParam('back') || router.currentRoute.query.back,
|
||||
ticket: getParam('ticket') || router.currentRoute.query.ticket
|
||||
}
|
||||
},
|
||||
// 页面加载后触发
|
||||
created() {
|
||||
console.log('获取 back 参数:', this.back)
|
||||
console.log('获取 ticket 参数:', this.ticket)
|
||||
|
||||
if(this.ticket) {
|
||||
this.doLoginByTicket(this.ticket);
|
||||
} else {
|
||||
this.goSsoAuthUrl();
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 重定向至认证中心
|
||||
goSsoAuthUrl: function() {
|
||||
ajax('/sso/getSsoAuthUrl', {clientLoginUrl: location.href}, function(res) {
|
||||
console.log('/sso/getSsoAuthUrl 返回数据', res);
|
||||
location.href = res.data;
|
||||
})
|
||||
},
|
||||
// 根据ticket值登录
|
||||
doLoginByTicket: function(ticket) {
|
||||
ajax('/sso/doLoginByTicket', {ticket: ticket}, function(res) {
|
||||
console.log('/sso/doLoginByTicket 返回数据', res);
|
||||
if(res.code === 200) {
|
||||
localStorage.setItem('satoken', res.data);
|
||||
location.href = decodeURIComponent(this.back);
|
||||
} else {
|
||||
alert(res.msg);
|
||||
}
|
||||
}.bind(this))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user