数据源初始化优化,权限优化

This commit is contained in:
暮光:城中城
2019-08-24 10:01:13 +08:00
parent f5e45b776b
commit 6c2141e8b8
7 changed files with 57 additions and 41 deletions

View File

@@ -87,7 +87,7 @@ public class DatabaseDocController {
@PostMapping(value = "/getDatabaseList") @PostMapping(value = "/getDatabaseList")
public ResponseJson getDatabaseList(Long sourceId) { public ResponseJson getDatabaseList(Long sourceId) {
BaseMapper baseMapper = this.getBaseMapper(sourceId); BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
List<DatabaseInfoDto> dbNameDtoList = baseMapper.getDatabaseList(); List<DatabaseInfoDto> dbNameDtoList = baseMapper.getDatabaseList();
return DocDbResponseJson.ok(dbNameDtoList); return DocDbResponseJson.ok(dbNameDtoList);
} }
@@ -101,9 +101,7 @@ public class DatabaseDocController {
@PostMapping(value = "/getTableColumnList") @PostMapping(value = "/getTableColumnList")
public ResponseJson getTableColumnList(Long sourceId, String dbName, String tableName) { public ResponseJson getTableColumnList(Long sourceId, String dbName, String tableName) {
if (!DocUserUtil.haveAuth(DocAuthConst.DB_DATASOURCE_MANAGE) && !DocUserUtil.haveCustomAuth(DbAuthType.VIEW.getName(), DocAuthConst.DB + sourceId)) { this.judgeAuth(sourceId, DbAuthType.VIEW.getName(), "没有查看该库表信息的权限");
return DocDbResponseJson.warn("没有查看该库表信息的权限");
}
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getFactoryById(sourceId); DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getFactoryById(sourceId);
if (databaseFactoryBean == null) { if (databaseFactoryBean == null) {
return DocDbResponseJson.warn("未找到对应的数据库连接"); return DocDbResponseJson.warn("未找到对应的数据库连接");
@@ -114,14 +112,14 @@ public class DatabaseDocController {
@PostMapping(value = "/getTableColumnDescList") @PostMapping(value = "/getTableColumnDescList")
public ResponseJson getTableColumnDescList(Long sourceId, String tableName) { public ResponseJson getTableColumnDescList(Long sourceId, String tableName) {
BaseMapper baseMapper = this.getBaseMapper(sourceId); BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
List<TableColumnDescDto> columnDescDto = baseMapper.getTableColumnDescList(tableName); List<TableColumnDescDto> columnDescDto = baseMapper.getTableColumnDescList(tableName);
return DocDbResponseJson.ok(columnDescDto); return DocDbResponseJson.ok(columnDescDto);
} }
@PostMapping(value = "/getTableAndColumnBySearch") @PostMapping(value = "/getTableAndColumnBySearch")
public ResponseJson getTableAndColumnBySearch(Long sourceId, String dbName, String searchText) { public ResponseJson getTableAndColumnBySearch(Long sourceId, String dbName, String searchText) {
BaseMapper baseMapper = this.getBaseMapper(sourceId); BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
if (StringUtils.isBlank(searchText)) { if (StringUtils.isBlank(searchText)) {
return DocDbResponseJson.ok(); return DocDbResponseJson.ok();
} }
@@ -132,13 +130,14 @@ public class DatabaseDocController {
@PostMapping(value = "/getTableDescList") @PostMapping(value = "/getTableDescList")
public ResponseJson getTableDescList(Long sourceId, String tableName) { public ResponseJson getTableDescList(Long sourceId, String tableName) {
BaseMapper baseMapper = this.getBaseMapper(sourceId); BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
List<TableDescDto> columnDescDto = baseMapper.getTableDescList(tableName); List<TableDescDto> columnDescDto = baseMapper.getTableDescList(tableName);
return DocDbResponseJson.ok(columnDescDto); return DocDbResponseJson.ok(columnDescDto);
} }
@PostMapping(value = "/updateTableDesc") @PostMapping(value = "/updateTableDesc")
public ResponseJson updateTableDesc(Long sourceId, String dbName, String tableName, String newDesc) { public ResponseJson updateTableDesc(Long sourceId, String dbName, String tableName, String newDesc) {
this.judgeAuth(sourceId, DbAuthType.DESC_EDIT.getName(), "没有修改该表注释的权限");
BaseMapper baseMapper = this.getBaseMapper(sourceId); BaseMapper baseMapper = this.getBaseMapper(sourceId);
baseMapper.updateTableDesc(dbName, tableName, newDesc); baseMapper.updateTableDesc(dbName, tableName, newDesc);
return DocDbResponseJson.ok(); return DocDbResponseJson.ok();
@@ -146,13 +145,8 @@ public class DatabaseDocController {
@PostMapping(value = "/updateTableColumnDesc") @PostMapping(value = "/updateTableColumnDesc")
public ResponseJson updateTableColumnDesc(Long sourceId, String dbName, String tableName, String columnName, String newDesc) { public ResponseJson updateTableColumnDesc(Long sourceId, String dbName, String tableName, String columnName, String newDesc) {
if (!DocUserUtil.haveCustomAuth(DbAuthType.DESC_EDIT.getName(), DocAuthConst.DB + sourceId)) { this.judgeAuth(sourceId, DbAuthType.DESC_EDIT.getName(), "没有修改该表字段注释的权限");
return DocDbResponseJson.warn("没有修改该表字段注释的权限"); BaseMapper baseMapper = this.getBaseMapper(sourceId);
}
BaseMapper baseMapper = databaseRegistrationBean.getBaseMapper(sourceId);
if (baseMapper == null) {
return DocDbResponseJson.warn("未找到对应的数据库连接");
}
ColumnInfoDto columnInfo = null; ColumnInfoDto columnInfo = null;
// mysql要同时修改类型默认值等所以先查出来 // mysql要同时修改类型默认值等所以先查出来
MysqlMapper mysqlMapper = databaseRegistrationBean.getBaseMapper(sourceId, MysqlMapper.class); MysqlMapper mysqlMapper = databaseRegistrationBean.getBaseMapper(sourceId, MysqlMapper.class);
@@ -175,9 +169,7 @@ public class DatabaseDocController {
@GetMapping(value = "/exportDatabase") @GetMapping(value = "/exportDatabase")
public ResponseJson exportDatabase(HttpServletResponse response, Long sourceId, String dbName, String tableNames) { public ResponseJson exportDatabase(HttpServletResponse response, Long sourceId, String dbName, String tableNames) {
if (!DocUserUtil.haveCustomAuth(DbAuthType.VIEW.getName(), DocAuthConst.DB + sourceId)) { this.judgeAuth(sourceId, DbAuthType.VIEW.getName(), "没有查看该库表信息的权限");
return DocDbResponseJson.warn("没有查看该库表信息的权限");
}
if (StringUtils.isBlank(tableNames)) { if (StringUtils.isBlank(tableNames)) {
return DocDbResponseJson.warn("请选择需要导出的表"); return DocDbResponseJson.warn("请选择需要导出的表");
} }
@@ -247,11 +239,22 @@ public class DatabaseDocController {
return tableColumnVo; return tableColumnVo;
} }
private BaseMapper getBaseMapper(Long sourceId) { /**
* 权限判断
* @author 暮光:城中城
*/
private void judgeAuth(Long sourceId, String authName, String noAuthInfo) {
if (!DocUserUtil.haveAuth(DocAuthConst.DB_DATASOURCE_MANAGE) if (!DocUserUtil.haveAuth(DocAuthConst.DB_DATASOURCE_MANAGE)
&& !DocUserUtil.haveCustomAuth(DbAuthType.VIEW.getName(), DocAuthConst.DB + sourceId)) { && !DocUserUtil.haveCustomAuth(authName, DocAuthConst.DB + sourceId)) {
throw new ConfirmException("没有查看该库表信息的权限"); throw new ConfirmException(noAuthInfo);
} }
}
/**
* 获取BaseMapper
* @author 暮光:城中城
*/
private BaseMapper getBaseMapper(Long sourceId) {
BaseMapper baseMapper = databaseRegistrationBean.getBaseMapperById(sourceId); BaseMapper baseMapper = databaseRegistrationBean.getBaseMapperById(sourceId);
if (baseMapper == null) { if (baseMapper == null) {
throw new ConfirmException("未找到对应的数据库连接"); throw new ConfirmException("未找到对应的数据库连接");
@@ -259,6 +262,15 @@ public class DatabaseDocController {
return baseMapper; return baseMapper;
} }
/**
* 判断查看权和获取BaseMapper
* @author 暮光:城中城
*/
private BaseMapper getViewAuthBaseMapper(Long sourceId) {
this.judgeAuth(sourceId, DbAuthType.VIEW.getName(), "没有查看该库表信息的权限");
return this.getBaseMapper(sourceId);
}
public static void main(String[] args) { public static void main(String[] args) {
//File zipFile = ZipUtil.zip("d:/aaa"); //File zipFile = ZipUtil.zip("d:/aaa");
File zipFile = new File("d:/111.zip"); File zipFile = new File("d:/111.zip");

View File

@@ -21,15 +21,8 @@ public class ApplicationListenerBean implements ApplicationListener<ContextRefre
@Resource @Resource
DbDatasourceService dbDatasourceService; DbDatasourceService dbDatasourceService;
private volatile static boolean IS_INIT = false;
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(ContextRefreshedEvent event) {
if (databaseRegistrationBean == null || IS_INIT) {
return;
}
// 会被调用两次
IS_INIT = true;
List<DatabaseFactoryBean> databaseFactoryBeanList = new LinkedList<>(); List<DatabaseFactoryBean> databaseFactoryBeanList = new LinkedList<>();
QueryWrapper<DbDatasource> wrapper = new QueryWrapper<>(); QueryWrapper<DbDatasource> wrapper = new QueryWrapper<>();

View File

@@ -1,6 +1,8 @@
package com.zyplayer.doc.db.framework.configuration; package com.zyplayer.doc.db.framework.configuration;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.zyplayer.doc.core.exception.ConfirmException;
import com.zyplayer.doc.data.repository.manage.entity.DbDatasource; import com.zyplayer.doc.data.repository.manage.entity.DbDatasource;
import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean; import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean;
import com.zyplayer.doc.db.framework.db.interceptor.SqlLogInterceptor; import com.zyplayer.doc.db.framework.db.interceptor.SqlLogInterceptor;
@@ -33,7 +35,16 @@ public class DatasourceUtil {
dataSource.setMaxWait(3000); dataSource.setMaxWait(3000);
dataSource.setTimeBetweenEvictionRunsMillis(60000); dataSource.setTimeBetweenEvictionRunsMillis(60000);
dataSource.setMinEvictableIdleTimeMillis(3600000); dataSource.setMinEvictableIdleTimeMillis(3600000);
// 重试3次失败退出源码里是errorCount > connectionErrorRetryAttempts所以写成2就是3次、、、
// CreateConnectionThread 源码在这个类里面
dataSource.setConnectionErrorRetryAttempts(2);
dataSource.setBreakAfterAcquireFailure(true);
dataSource.setName("zyplayer-doc-db" + dbDatasource.getId()); dataSource.setName("zyplayer-doc-db" + dbDatasource.getId());
DruidPooledConnection tryConnection = dataSource.getConnection(3000);
if (tryConnection == null) {
throw new ConfirmException("尝试获取该数据源连接失败:" + dbDatasource.getSourceUrl());
}
tryConnection.recycle();
// 描述连接信息的对象 // 描述连接信息的对象
DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean(); DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean();
DatabaseMetaData metaData = dataSource.getConnection().getMetaData(); DatabaseMetaData metaData = dataSource.getConnection().getMetaData();

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +1,2 @@
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,a){for(var u,i,f,l=0,s=[];l<t.length;l++)i=t[l],o[i]&&s.push(o[i][0]),o[i]=0;for(u in c)Object.prototype.hasOwnProperty.call(c,u)&&(e[u]=c[u]);for(r&&r(t,c,a);s.length;)s.shift()();if(a)for(l=0;l<a.length;l++)f=n(n.s=a[l]);return f};var t={},o={2:0};n.e=function(e){function r(){u.onerror=u.onload=null,clearTimeout(i);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var a=document.getElementsByTagName("head")[0],u=document.createElement("script");u.type="text/javascript",u.charset="utf-8",u.async=!0,u.timeout=12e4,n.nc&&u.setAttribute("nonce",n.nc),u.src=n.p+""+e+".js?"+{0:"84aaa1f845500c1f32eb",1:"0a0403eb1820498dc9bc"}[e];var i=setTimeout(r,12e4);return u.onerror=u.onload=r,a.appendChild(u),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]); !function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var i,a,f,l=0,s=[];l<t.length;l++)a=t[l],o[a]&&s.push(o[a][0]),o[a]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(a);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.charset="utf-8",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute("nonce",n.nc),i.src=n.p+""+e+".js?"+{0:"872bb020b9d9767172e2",1:"0a0403eb1820498dc9bc"}[e];var a=setTimeout(r,12e4);return i.onerror=i.onload=r,u.appendChild(i),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?fdc840871c16aef2ccfc //# sourceMappingURL=doc-db-manifest.js.map?4e55a966af01f8e2a030

View File

@@ -8,7 +8,7 @@
<body> <body>
<div id="app"></div> <div id="app"></div>
<script type="text/javascript" src="doc-db-manifest.js?fdc840871c16aef2ccfc"></script><script type="text/javascript" src="doc-db-vendor.js?0a0403eb1820498dc9bc"></script><script type="text/javascript" src="doc-db-index.js?84aaa1f845500c1f32eb"></script></body> <script type="text/javascript" src="doc-db-manifest.js?4e55a966af01f8e2a030"></script><script type="text/javascript" src="doc-db-vendor.js?0a0403eb1820498dc9bc"></script><script type="text/javascript" src="doc-db-index.js?872bb020b9d9767172e2"></script></body>
</html> </html>

View File

@@ -25,12 +25,12 @@
</div> </div>
<!--增加数据源弹窗--> <!--增加数据源弹窗-->
<el-dialog :inline="true" :title="newDatasource.id>0?'编辑数据源':'新增数据源'" :visible.sync="datasourceDialogVisible" width="760px"> <el-dialog :inline="true" :title="newDatasource.id>0?'编辑数据源':'新增数据源'" :visible.sync="datasourceDialogVisible" width="760px">
<el-alert <!-- <el-alert-->
title="重要提醒" <!-- title="重要提醒"-->
description="请录入正确可用的数据库连接、账号、密码信息,否则初始化数据源失败将影响整个系统,有可能需要重启服务才能解决" <!-- description="请录入正确可用的数据库连接、账号、密码信息,否则初始化数据源失败将影响整个系统,有可能需要重启服务才能解决"-->
type="warning" :closable="false" <!-- type="warning" :closable="false"-->
show-icon style="margin-bottom: 10px;"> <!-- show-icon style="margin-bottom: 10px;">-->
</el-alert> <!-- </el-alert>-->
<el-form label-width="120px"> <el-form label-width="120px">
<el-form-item label="名字:"> <el-form-item label="名字:">
<el-input v-model="newDatasource.name" placeholder="中文名字"></el-input> <el-input v-model="newDatasource.name" placeholder="中文名字"></el-input>
@@ -197,8 +197,8 @@
}).catch(()=>{}); }).catch(()=>{});
}, },
saveDatasource() { saveDatasource() {
this.datasourceDialogVisible = false;
this.common.post(this.apilist1.manageUpdateDatasource, this.newDatasource, function (json) { this.common.post(this.apilist1.manageUpdateDatasource, this.newDatasource, function (json) {
app.datasourceDialogVisible = false;
app.$message.success("保存成功!"); app.$message.success("保存成功!");
app.getDatasourceList(); app.getDatasourceList();
}); });