🎉 增加多终端token认证

This commit is contained in:
smallchill 2019-03-31 22:32:30 +08:00
parent f843875db4
commit b860051ce7
6 changed files with 414 additions and 85 deletions

View File

@ -1,48 +1,49 @@
{ {
"name": "saber-admin", "name": "saber-admin",
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build": "vue-cli-service build", "build": "vue-cli-service build",
"lint": "vue-cli-service lint", "lint": "vue-cli-service lint",
"analyz": "npm_config_report=true npm run build", "analyz": "npm_config_report=true npm run build",
"test:unit": "vue-cli-service test:unit", "test:unit": "vue-cli-service test:unit",
"test:e2e": "vue-cli-service test:e2e" "test:e2e": "vue-cli-service test:e2e"
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",
"babel-polyfill": "^6.26.0", "babel-polyfill": "^6.26.0",
"classlist-polyfill": "^1.2.0", "classlist-polyfill": "^1.2.0",
"element-ui": "^2.4.5", "element-ui": "^2.4.5",
"js-cookie": "^2.2.0", "js-cookie": "^2.2.0",
"mockjs": "^1.0.1-beta3", "mockjs": "^1.0.1-beta3",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"script-loader": "^0.7.2", "script-loader": "^0.7.2",
"vue": "^2.5.16", "vue": "^2.5.16",
"vue-axios": "^2.1.2", "vue-axios": "^2.1.2",
"vue-i18n": "^8.7.0", "vue-i18n": "^8.7.0",
"vue-router": "^3.0.1", "vue-router": "^3.0.1",
"vuex": "^3.0.1" "vuex": "^3.0.1",
}, "js-base64": "^2.5.1"
"devDependencies": { },
"@vue/cli-plugin-babel": "^3.1.1", "devDependencies": {
"@vue/cli-plugin-eslint": "^3.1.5", "@vue/cli-plugin-babel": "^3.1.1",
"@vue/cli-service": "^3.1.4", "@vue/cli-plugin-eslint": "^3.1.5",
"chai": "^4.1.2", "@vue/cli-service": "^3.1.4",
"node-sass": "^4.9.0", "chai": "^4.1.2",
"sass-loader": "^7.0.1", "node-sass": "^4.9.0",
"vue-template-compiler": "^2.5.17", "sass-loader": "^7.0.1",
"webpack-bundle-analyzer": "^3.0.3" "vue-template-compiler": "^2.5.17",
}, "webpack-bundle-analyzer": "^3.0.3"
"lint-staged": { },
"*.js": [ "lint-staged": {
"vue-cli-service lint", "*.js": [
"git add" "vue-cli-service lint",
], "git add"
"*.vue": [ ],
"vue-cli-service lint", "*.vue": [
"git add" "vue-cli-service lint",
] "git add"
} ]
} }
}

50
src/api/system/client.js Normal file
View File

@ -0,0 +1,50 @@
import request from '@/router/axios';
export const getList = (current, size, params) => {
return request({
url: '/api/blade-system/client/list',
method: 'get',
params: {
...params,
current,
size,
}
})
}
export const getDetail = (id) => {
return request({
url: '/api/blade-system/client/detail',
method: 'get',
params: {
id
}
})
}
export const remove = (ids) => {
return request({
url: '/api/blade-system/client/remove',
method: 'post',
params: {
ids,
}
})
}
export const add = (row) => {
return request({
url: '/api/blade-system/client/submit',
method: 'post',
data: row
})
}
export const update = (row) => {
return request({
url: '/api/blade-system/client/submit',
method: 'post',
data: row
})
}

View File

@ -4,6 +4,8 @@
export default { export default {
title: "saber", title: "saber",
indexTitle: 'Saber Admin', indexTitle: 'Saber Admin',
clientId: 'saber', // 客户端id
clientSecret: 'saber_secret', // 客户端密钥
tenantMode: true, // 是否开启租户模式 tenantMode: true, // 是否开启租户模式
logo: "S", logo: "S",
key: 'saber',//配置主键,目前用于存储 key: 'saber',//配置主键,目前用于存储

View File

@ -8,61 +8,64 @@
import axios from 'axios' import axios from 'axios'
import store from '@/store/'; import store from '@/store/';
import router from '@/router/router' import router from '@/router/router'
import { serialize } from '@/util/util' import {serialize} from '@/util/util'
import { getToken } from '@/util/auth' import {getToken} from '@/util/auth'
import { Message } from 'element-ui' import {Message} from 'element-ui'
import website from '@/config/website'; import website from '@/config/website';
import NProgress from 'nprogress' // progress bar import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style import 'nprogress/nprogress.css' // progress bar style
import {Base64} from 'js-base64';
axios.defaults.timeout = 10000; axios.defaults.timeout = 10000;
//返回其他状态吗 //返回其他状态吗
axios.defaults.validateStatus = function (status) { axios.defaults.validateStatus = function (status) {
return status >= 200 && status <= 500; // 默认的 return status >= 200 && status <= 500; // 默认的
}; };
//跨域请求允许保存cookie //跨域请求允许保存cookie
axios.defaults.withCredentials = true; axios.defaults.withCredentials = true;
// NProgress Configuration // NProgress Configuration
NProgress.configure({ NProgress.configure({
showSpinner: false showSpinner: false
}); });
//HTTPrequest拦截 //HTTPrequest拦截
axios.interceptors.request.use(config => { axios.interceptors.request.use(config => {
NProgress.start() // start progress bar NProgress.start() // start progress bar
const meta = (config.meta || {}); const meta = (config.meta || {});
const isToken = meta.isToken === false; const isToken = meta.isToken === false;
if (getToken() && !isToken) { config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`;
config.headers['blade-auth'] = 'Bearer ' + getToken() // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改 if (getToken() && !isToken) {
} config.headers['Blade-Auth'] = 'bearer ' + getToken() // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
//headers中配置serialize为true开启序列化 }
if (config.method === 'post' && meta.isSerialize === true) { //headers中配置serialize为true开启序列化
config.data = serialize(config.data); if (config.method === 'post' && meta.isSerialize === true) {
} config.data = serialize(config.data);
return config }
return config
}, error => { }, error => {
return Promise.reject(error) return Promise.reject(error)
}); });
//HTTPresponse拦截 //HTTPresponse拦截
axios.interceptors.response.use(res => { axios.interceptors.response.use(res => {
NProgress.done(); NProgress.done();
const status = res.data.code || 200 const status = res.data.code || 200
const statusWhiteList = website.statusWhiteList || []; const statusWhiteList = website.statusWhiteList || [];
const message = res.data.msg || '未知错误'; const message = res.data.msg || '未知错误';
//如果在白名单里则自行catch逻辑处理 //如果在白名单里则自行catch逻辑处理
if (statusWhiteList.includes(status)) return Promise.reject(res); if (statusWhiteList.includes(status)) return Promise.reject(res);
//如果是401则跳转到登录页面 //如果是401则跳转到登录页面
if (status === 401) store.dispatch('FedLogOut').then(() => router.push({ path: '/login' })); if (status === 401) store.dispatch('FedLogOut').then(() => router.push({path: '/login'}));
// 如果请求为非200否者默认统一处理 // 如果请求为非200否者默认统一处理
if (status !== 200) { if (status !== 200) {
Message({ Message({
message: message, message: message,
type: 'error' type: 'error'
}) })
return Promise.reject(new Error(message)) return Promise.reject(new Error(message))
} }
return res; return res;
}, error => { }, error => {
NProgress.done(); NProgress.done();
return Promise.reject(new Error(error)); return Promise.reject(new Error(error));
}) })
export default axios; export default axios;

273
src/views/system/client.vue Normal file
View File

@ -0,0 +1,273 @@
<template>
<basic-container>
<avue-crud :option="option"
:data="data"
:page="page"
@row-del="rowDel"
v-model="form"
:permission="permissionList"
@row-update="rowUpdate"
@row-save="rowSave"
:before-open="beforeOpen"
@search-change="searchChange"
@search-reset="searchReset"
@selection-change="selectionChange"
@on-load="onLoad">
<template slot="menuLeft">
<el-button type="danger"
size="small"
icon="el-icon-delete"
plain
v-if="permission.client_delete"
@click="handleDelete">
</el-button>
</template>
</avue-crud>
</basic-container>
</template>
<script>
import {getList, getDetail, add, update, remove} from "@/api/system/client";
import {mapGetters} from "vuex";
export default {
data() {
return {
form: {},
page: {
pageSize: 10,
currentPage: 1,
total: 0
},
selectionList: [],
option: {
tip: false,
border: true,
index: true,
viewBtn: true,
selection: true,
column: [
{
label: "应用id",
prop: "clientId",
search: true,
rules: [{
required: true,
message: "请输入客户端id",
trigger: "blur"
}]
},
{
label: "应用密钥",
prop: "clientSecret",
search: true,
rules: [{
required: true,
message: "请输入客户端密钥",
trigger: "blur"
}]
},
{
label: "授权类型",
prop: "authorizedGrantTypes",
valueDefault: "refresh_token,password,authorization_code",
rules: [{
required: true,
message: "请输入授权类型",
trigger: "blur"
}]
},
{
label: "授权范围",
prop: "scope",
valueDefault: "all",
rules: [{
required: true,
message: "请输入授权范围",
trigger: "blur"
}]
},
{
label: "令牌秒数",
prop: "accessTokenValidity",
type: "number",
valueDefault: 3600,
rules: [{
required: true,
message: "请输入令牌过期秒数",
trigger: "blur"
}]
},
{
label: "刷新秒数",
prop: "refreshTokenValidity",
type: "number",
valueDefault: 604800,
hide: true,
rules: [{
required: true,
message: "请输入刷新令牌过期秒数",
trigger: "blur"
}]
},
{
label: "回调地址",
prop: "webServerRedirectUri",
hide: true,
rules: [{
required: true,
message: "请输入回调地址",
trigger: "blur"
}]
},
{
label: "资源集合",
prop: "resourceIds",
hide: true,
rules: [{
message: "请输入资源集合",
trigger: "blur"
}]
},
{
label: "权限",
prop: "authorities",
hide: true,
rules: [{
message: "请输入权限",
trigger: "blur"
}]
},
{
label: "自动授权",
prop: "autoapprove",
hide: true,
rules: [{
message: "请输入自动授权",
trigger: "blur"
}]
},
{
label: "附加说明",
hide: true,
prop: "additionalInformation",
span: 24,
rules: [{
message: "请输入附加说明",
trigger: "blur"
}]
},
]
},
data: []
};
},
computed: {
...mapGetters(["permission"]),
permissionList() {
return {
addBtn: this.vaildData(this.permission.client_add),
viewBtn: this.vaildData(this.permission.client_view),
delBtn: this.vaildData(this.permission.client_delete),
editBtn: this.vaildData(this.permission.client_edit)
};
},
ids() {
let ids = [];
this.selectionList.forEach(ele => {
ids.push(ele.id);
});
return ids.join(",");
}
},
methods: {
rowSave(row, loading) {
add(row).then(() => {
loading();
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
});
},
rowUpdate(row, index, loading) {
update(row).then(() => {
loading();
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
});
},
rowDel(row) {
this.$confirm("确定将选择数据删除?", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
return remove(row.id);
})
.then(() => {
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
});
},
searchReset() {
this.onLoad(this.page);
},
searchChange(params) {
this.onLoad(this.page, params);
},
selectionChange(list) {
this.selectionList = list;
},
handleDelete() {
if (this.selectionList.length === 0) {
this.$message.warning("请选择至少一条数据");
return;
}
this.$confirm("确定将选择数据删除?", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
return remove(this.ids);
})
.then(() => {
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
this.$refs.crud.toggleSelection();
});
},
beforeOpen(done, type) {
if (["edit", "view"].includes(type)) {
getDetail(this.form.id).then(res => {
this.form = res.data.data;
});
}
done();
},
onLoad(page, params = {}) {
getList(page.currentPage, page.pageSize, params).then(res => {
const data = res.data.data;
this.page.total = data.total;
this.data = data.records;
});
}
}
};
</script>
<style>
</style>

View File

@ -4490,9 +4490,9 @@ joi@^14.3.0:
isemail "3.x.x" isemail "3.x.x"
topo "3.x.x" topo "3.x.x"
js-base64@^2.1.8: js-base64@^2.1.8, js-base64@^2.5.1:
version "2.5.1" version "2.5.1"
resolved "http://registry.npm.taobao.org/js-base64/download/js-base64-2.5.1.tgz#1efa39ef2c5f7980bb1784ade4a8af2de3291121" resolved "http://registry.npm.taobao.org/js-base64/download/js-base64-2.5.1.tgz?cache=0&other_urls=http%3A%2F%2Fregistry.npm.taobao.org%2Fjs-base64%2Fdownload%2Fjs-base64-2.5.1.tgz#1efa39ef2c5f7980bb1784ade4a8af2de3291121"
integrity sha1-Hvo57yxfeYC7F4St5KivLeMpESE= integrity sha1-Hvo57yxfeYC7F4St5KivLeMpESE=
js-cookie@^2.2.0: js-cookie@^2.2.0: