🎉 增加多终端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

@ -23,7 +23,8 @@
"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": { "devDependencies": {
"@vue/cli-plugin-babel": "^3.1.1", "@vue/cli-plugin-babel": "^3.1.1",

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,12 +8,14 @@
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) {
@ -30,8 +32,9 @@ 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;
config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`;
if (getToken() && !isToken) { if (getToken() && !isToken) {
config.headers['blade-auth'] = 'Bearer ' + getToken() // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改 config.headers['Blade-Auth'] = 'bearer ' + getToken() // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
} }
//headers中配置serialize为true开启序列化 //headers中配置serialize为true开启序列化
if (config.method === 'post' && meta.isSerialize === true) { if (config.method === 'post' && meta.isSerialize === true) {
@ -50,7 +53,7 @@ axios.interceptors.response.use(res => {
//如果在白名单里则自行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({

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: