新增SSO前后端分离集成示例 vue2 版本

This commit is contained in:
click33 2022-10-27 20:51:36 +08:00
parent 7ef1305527
commit bb3d9c6210
14 changed files with 27724 additions and 0 deletions

View 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?

View 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
```

View File

@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}

File diff suppressed because it is too large Load Diff

View 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"
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -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>

View File

@ -0,0 +1,15 @@
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

View 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')

View File

@ -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

View File

@ -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);
}

View File

@ -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>&nbsp;&nbsp;
<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>

View File

@ -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>