数据源测试连接
This commit is contained in:
parent
3438d122bd
commit
8475bea737
@ -577,7 +577,9 @@ public class MagicAPIAutoConfiguration implements WebMvcConfigurer, WebSocketCon
|
||||
MagicWorkbenchController magicWorkbenchController = new MagicWorkbenchController(configuration, properties.getSecretKey());
|
||||
if (base != null) {
|
||||
configuration.setEnableWeb(true);
|
||||
mapping.registerController(magicWorkbenchController).registerController(new MagicResourceController(configuration));
|
||||
mapping.registerController(magicWorkbenchController)
|
||||
.registerController(new MagicResourceController(configuration))
|
||||
.registerController(new MagicDataSourceController(configuration));
|
||||
}
|
||||
// 注册接收推送的接口
|
||||
if (StringUtils.isNotBlank(properties.getSecretKey())) {
|
||||
|
||||
@ -62,12 +62,7 @@ public class MagicController implements JsonCodeConstants {
|
||||
return true;
|
||||
}
|
||||
MagicUser magicUser = (MagicUser) request.getAttribute(Constants.ATTRIBUTE_MAGIC_USER);
|
||||
if (entity instanceof ApiInfo) {
|
||||
return configuration.getAuthorizationInterceptor().allowVisit(magicUser, request, authorization, (ApiInfo) entity);
|
||||
} else if (entity instanceof FunctionInfo) {
|
||||
return configuration.getAuthorizationInterceptor().allowVisit(magicUser, request, authorization, (FunctionInfo) entity);
|
||||
}
|
||||
return false;
|
||||
return configuration.getAuthorizationInterceptor().allowVisit(magicUser, request, authorization, entity);
|
||||
}
|
||||
|
||||
boolean allowVisit(HttpServletRequest request, Authorization authorization, Group group) {
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package org.ssssssss.magicapi.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.ssssssss.magicapi.config.MagicConfiguration;
|
||||
import org.ssssssss.magicapi.model.DataSourceInfo;
|
||||
import org.ssssssss.magicapi.model.JsonBean;
|
||||
import org.ssssssss.magicapi.utils.JdbcUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class MagicDataSourceController extends MagicController implements MagicExceptionHandler {
|
||||
|
||||
public MagicDataSourceController(MagicConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
@RequestMapping("/datasource/jdbc/test")
|
||||
@ResponseBody
|
||||
public JsonBean<String> test(@RequestBody DataSourceInfo properties) {
|
||||
try {
|
||||
Connection connection = JdbcUtils.getConnection(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword());
|
||||
JdbcUtils.close(connection);
|
||||
} catch (Exception e) {
|
||||
return new JsonBean<>(e.getMessage());
|
||||
}
|
||||
return new JsonBean<>("ok");
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,8 @@
|
||||
package org.ssssssss.magicapi.interceptor;
|
||||
|
||||
import org.ssssssss.magicapi.exception.MagicLoginException;
|
||||
import org.ssssssss.magicapi.model.ApiInfo;
|
||||
import org.ssssssss.magicapi.model.DataSourceInfo;
|
||||
import org.ssssssss.magicapi.model.FunctionInfo;
|
||||
import org.ssssssss.magicapi.model.Group;
|
||||
import org.ssssssss.magicapi.model.MagicEntity;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@ -75,25 +73,13 @@ public interface AuthorizationInterceptor {
|
||||
* @param magicUser 登录的用户对象
|
||||
* @param request HttpServletRequest
|
||||
* @param authorization 鉴权方法
|
||||
* @param apiInfo 接口信息
|
||||
* @param entity 接口、函数、数据源信息
|
||||
* @return true 有权限访问, false 无权限访问
|
||||
*/
|
||||
default boolean allowVisit(MagicUser magicUser, HttpServletRequest request, Authorization authorization, ApiInfo apiInfo) {
|
||||
default boolean allowVisit(MagicUser magicUser, HttpServletRequest request, Authorization authorization, MagicEntity entity) {
|
||||
return allowVisit(magicUser, request, authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否拥有对该函数的增删改权限
|
||||
*
|
||||
* @param magicUser 登录的用户对象
|
||||
* @param request HttpServletRequest
|
||||
* @param authorization 鉴权方法
|
||||
* @param functionInfo 函数信息
|
||||
* @return true 有权限访问, false 无权限访问
|
||||
*/
|
||||
default boolean allowVisit(MagicUser magicUser, HttpServletRequest request, Authorization authorization, FunctionInfo functionInfo) {
|
||||
return allowVisit(magicUser, request, authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否拥有对该分组的增删改权限
|
||||
@ -108,16 +94,4 @@ public interface AuthorizationInterceptor {
|
||||
return allowVisit(magicUser, request, authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否拥有对该数据源的增删改权限
|
||||
*
|
||||
* @param magicUser 登录的用户对象
|
||||
* @param request HttpServletRequest
|
||||
* @param authorization 鉴权方法
|
||||
* @param dataSourceInfo 数据源信息
|
||||
* @return true 有权限访问, false 无权限访问
|
||||
*/
|
||||
default boolean allowVisit(MagicUser magicUser, HttpServletRequest request, Authorization authorization, DataSourceInfo dataSourceInfo) {
|
||||
return allowVisit(magicUser, request, authorization);
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,81 +133,6 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
|
||||
@Override
|
||||
public void upload(InputStream inputStream, String mode) throws IOException {
|
||||
// ZipResource root = new ZipResource(inputStream);
|
||||
// Set<String> apiPaths = new LinkedHashSet<>();
|
||||
// Set<String> functionPaths = new LinkedHashSet<>();
|
||||
// Set<String> websocketPaths = new LinkedHashSet<>();
|
||||
// Set<Group> groups = new LinkedHashSet<>();
|
||||
// Set<ApiInfo> apiInfos = new LinkedHashSet<>();
|
||||
// Set<FunctionInfo> functionInfos = new LinkedHashSet<>();
|
||||
// Set<WebSocketInfo> websocketInfos = new LinkedHashSet<>();
|
||||
// boolean checked = !UPLOAD_MODE_FULL.equals(mode);
|
||||
// // 检查上传资源中是否有冲突
|
||||
// readPaths(groups, apiPaths, functionPaths, websocketPaths, apiInfos, functionInfos, websocketInfos, "/", root, checked);
|
||||
// Resource item = root.getResource(GROUP_METABASE);
|
||||
// if (item.exists()) {
|
||||
// Group group = groupServiceProvider.readGroup(item);
|
||||
// // 检查上级分组是否存在
|
||||
// isTrue("0".equals(group.getParentId()) || groupServiceProvider.getGroupResource(group.getParentId()).exists(), GROUP_NOT_FOUND);
|
||||
// }
|
||||
// if (checked) {
|
||||
// // 检测分组是否有冲突
|
||||
// groups.forEach(group -> {
|
||||
// Resource resource;
|
||||
// if ("0".equals(group.getParentId())) {
|
||||
// resource = workspace.getDirectory(GROUP_TYPE_API.equals(group.getType()) ? PATH_API : (GROUP_TYPE_WEBSOCKET.equals(group.getType()) ? PATH_WEBSOCKET : PATH_FUNCTION)).getDirectory(group.getName());
|
||||
// } else {
|
||||
// resource = groupServiceProvider.getGroupResource(group.getId());
|
||||
// }
|
||||
// if (resource != null && resource.exists()) {
|
||||
// Group src = groupServiceProvider.readGroup(resource.getResource(GROUP_METABASE));
|
||||
// isTrue(src == null || src.getId().equals(group.getId()), GROUP_CONFLICT);
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// Resource resource = workspace.getDirectory(PATH_API);
|
||||
// resource.delete();
|
||||
// resource.mkdir();
|
||||
// resource = workspace.getDirectory(PATH_FUNCTION);
|
||||
// resource.delete();
|
||||
// resource.mkdir();
|
||||
// resource = workspace.getDirectory(PATH_WEBSOCKET);
|
||||
// resource.delete();
|
||||
// resource.mkdir();
|
||||
// resource = workspace.getDirectory(PATH_DATASOURCE);
|
||||
// resource.delete();
|
||||
// resource.mkdir();
|
||||
// }
|
||||
// for (Group group : groups) {
|
||||
// Resource groupResource = groupServiceProvider.getGroupResource(group.getId());
|
||||
// if (groupResource != null && groupResource.exists()) {
|
||||
// groupServiceProvider.update(group);
|
||||
// } else {
|
||||
// groupServiceProvider.insert(group);
|
||||
// }
|
||||
// }
|
||||
// // 保存
|
||||
// write(mappingRegistry.getStoreServiceProvider(), apiInfos);
|
||||
// write(functionRegistry.getStoreServiceProvider(), functionInfos);
|
||||
// write(webSocketRegistry.getStoreServiceProvider(), websocketInfos);
|
||||
// // 备份
|
||||
// apiInfos.forEach(backupService::backup);
|
||||
// functionInfos.forEach(backupService::backup);
|
||||
// websocketInfos.forEach(backupService::backup);
|
||||
// // 重新注册
|
||||
// this.mappingRegistries.forEach(AbstractMagicDynamicMappingRegistry::registerAll);
|
||||
// Resource uploadDatasourceResource = root.getResource(PATH_DATASOURCE + "/");
|
||||
// if (uploadDatasourceResource.exists()) {
|
||||
// uploadDatasourceResource.files(".json").forEach(it -> {
|
||||
// byte[] content = it.read();
|
||||
// // 保存数据源
|
||||
// this.datasourceResource.getResource(it.name()).write(content);
|
||||
// // TODO 备份数据源
|
||||
// });
|
||||
// }
|
||||
// // TODO 会造成闪断,需要上锁处理。
|
||||
// registerAllDataSource();
|
||||
// magicNotifyService.sendNotify(new MagicNotify(instanceId));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -0,0 +1,44 @@
|
||||
package org.ssssssss.magicapi.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.jdbc.DatabaseDriver;
|
||||
import org.ssssssss.magicapi.exception.MagicAPIException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class JdbcUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
|
||||
|
||||
public static Connection getConnection(String driver, String url, String username, String password) {
|
||||
try {
|
||||
if (StringUtils.isBlank(driver)) {
|
||||
driver = DatabaseDriver.fromJdbcUrl(url).getDriverClassName();
|
||||
if (StringUtils.isBlank(driver)) {
|
||||
throw new MagicAPIException("无法从url中获得驱动类");
|
||||
}
|
||||
}
|
||||
Class.forName(driver);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new MagicAPIException("找不到驱动:" + driver);
|
||||
}
|
||||
try {
|
||||
return DriverManager.getConnection(url, username, password);
|
||||
} catch (SQLException e) {
|
||||
logger.error("获取Jdbc链接失败", e);
|
||||
throw new MagicAPIException("获取Jdbc链接失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void close(Connection connection) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user