From 43eb75c26fa839db081c5f14e610ad6aa0555f6d Mon Sep 17 00:00:00 2001 From: ZJH Date: Thu, 9 Dec 2021 14:31:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20S?= =?UTF-8?q?wagger=20URL=20=E5=AF=BC=E5=85=A5=E6=88=96=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=8E=A5=E5=8F=A3=E6=97=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1007799 --user=周骏弘 [Github#7602]当Swagger URL存在登录验证或其它认证机制时,通过Swagger URL的方式导入API接口,报错解析失败,请优化 #7602 https://www.tapd.cn/55049933/s/1079026 --- .../api/dto/ApiTestImportRequest.java | 8 ++ .../dto/definition/parse/Swagger2Parser.java | 55 +++++++- .../dto/definition/parse/Swagger3Parser.java | 59 +++++++- .../api/service/ApiDefinitionService.java | 25 +++- .../base/domain/SwaggerUrlProject.java | 5 +- .../base/mapper/SwaggerUrlProjectMapper.java | 9 +- .../base/mapper/SwaggerUrlProjectMapper.xml | 126 +++++++++++++----- .../ext/ExtSwaggerUrlScheduleMapper.xml | 1 + .../controller/request/ScheduleRequest.java | 8 ++ .../job/sechedule/SwaggerUrlImportJob.java | 32 ++++- .../db/migration/V102__v1.15.4_release.sql | 2 + .../components/auth/ApiAuthConfig.vue | 44 ++++-- .../complete/EditCompleteHTTPApi.vue | 2 +- .../components/import/ApiImport.vue | 60 ++++++++- .../components/import/ApiSchedule.vue | 70 +++++++++- frontend/src/i18n/en-US.js | 2 + frontend/src/i18n/zh-CN.js | 2 + frontend/src/i18n/zh-TW.js | 2 + 18 files changed, 438 insertions(+), 74 deletions(-) create mode 100644 backend/src/main/resources/db/migration/V102__v1.15.4_release.sql diff --git a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java index dcad5bebc0..f23e1c766b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java @@ -1,7 +1,10 @@ package io.metersphere.api.dto; +import io.metersphere.api.dto.definition.request.auth.MsAuthManager; +import io.metersphere.api.dto.scenario.KeyValue; import lombok.Getter; import lombok.Setter; +import java.util.List; @Setter @Getter @@ -24,4 +27,9 @@ public class ApiTestImportRequest { private String type; // 是否开启自定义ID private Boolean openCustomNum = false; + // 鉴权相关 + private List headers; + private List arguments; + private MsAuthManager authManager; + } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java index 196ed26a13..fb1b41846c 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java @@ -14,13 +14,13 @@ import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import io.metersphere.commons.constants.SwaggerParameterType; import io.swagger.models.*; +import io.swagger.models.auth.AuthorizationValue; import io.swagger.models.parameters.*; import io.swagger.models.properties.*; import io.swagger.parser.SwaggerParser; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; - import java.io.InputStream; import java.util.*; @@ -32,24 +32,69 @@ public class Swagger2Parser extends SwaggerAbstractParser { public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) { Swagger swagger; String sourceStr = ""; + List auths = setAuths(request); if (StringUtils.isNotBlank(request.getSwaggerUrl())) { // 使用 url 导入 swagger - swagger = new SwaggerParser().read(request.getSwaggerUrl()); + swagger = new SwaggerParser().read(request.getSwaggerUrl(), auths, true); } else { sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String - swagger = new SwaggerParser().readWithInfo(sourceStr).getSwagger(); + swagger = new SwaggerParser().readWithInfo(sourceStr, auths, true).getSwagger(); } - if (swagger == null || swagger.getSwagger() == null) { // 不是 2.0 版本,则尝试转换 3.0 Swagger3Parser swagger3Parser = new Swagger3Parser(); + return swagger3Parser.parse(sourceStr, request); } - ApiDefinitionImport definitionImport = new ApiDefinitionImport(); this.projectId = request.getProjectId(); definitionImport.setData(parseRequests(swagger, request)); return definitionImport; } + // 鉴权设置 + private List setAuths(ApiTestImportRequest request) { + List auths = new ArrayList<>(); + // 如果有 BaseAuth 参数,base64 编码后转换成 headers + if(request.getAuthManager() != null + && StringUtils.isNotBlank(request.getAuthManager().getUsername()) + && StringUtils.isNotBlank(request.getAuthManager().getPassword()) + && request.getAuthManager().getVerification().equals("Basic Auth")){ + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType("header"); + authorizationValue.setKeyName("Authorization"); + String authValue = "Basic " + Base64.getUrlEncoder().encodeToString((request.getAuthManager().getUsername() + + ":" + request.getAuthManager().getPassword()).getBytes()); + authorizationValue.setValue(authValue); + auths.add(authorizationValue); + } + // 设置 headers + if(!CollectionUtils.isEmpty(request.getHeaders())){ + for(KeyValue keyValue : request.getHeaders()){ + // 当有 key 时才进行设置 + if(StringUtils.isNotBlank(keyValue.getName())){ + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType("header"); + authorizationValue.setKeyName(keyValue.getName()); + authorizationValue.setValue(keyValue.getValue()); + auths.add(authorizationValue); + } + } + } + // 设置 query 参数 + if(!CollectionUtils.isEmpty(request.getArguments())){ + for(KeyValue keyValue : request.getArguments()){ + if(StringUtils.isNotBlank(keyValue.getName())){ + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType("query"); + authorizationValue.setKeyName(keyValue.getName()); + authorizationValue.setValue(keyValue.getValue()); + auths.add(authorizationValue); + } + } + } + return CollectionUtils.size(auths) == 0 ? null : auths; + } + + private List parseRequests(Swagger swagger, ApiTestImportRequest importRequest) { Map paths = swagger.getPaths(); Set pathNames = paths.keySet(); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java index 00d3ac43ad..847673ec68 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java @@ -27,12 +27,12 @@ import io.swagger.v3.oas.models.media.*; import io.swagger.v3.oas.models.parameters.*; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; +import io.swagger.v3.parser.core.models.AuthorizationValue; import io.swagger.v3.parser.core.models.SwaggerParseResult; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpMethod; - import java.io.InputStream; import java.util.*; @@ -51,29 +51,74 @@ public class Swagger3Parser extends SwaggerAbstractParser { } public ApiDefinitionImport parse(String sourceStr, ApiTestImportRequest request) { + + List auths = setAuths(request); SwaggerParseResult result; if (StringUtils.isNotBlank(request.getSwaggerUrl())) { - result = new OpenAPIParser().readLocation(request.getSwaggerUrl(), null, null); + result = new OpenAPIParser().readLocation(request.getSwaggerUrl(), auths, null); } else { - result = new OpenAPIParser().readContents(sourceStr, null, null); + result = new OpenAPIParser().readContents(sourceStr, auths, null); } - if (result == null) { MSException.throwException("解析失败,请确认选择的是 swagger 格式!"); } - OpenAPI openAPI = result.getOpenAPI(); - if (result.getMessages() != null) { result.getMessages().forEach(msg -> LogUtil.error(msg)); // validation errors and warnings } - ApiDefinitionImport definitionImport = new ApiDefinitionImport(); this.projectId = request.getProjectId(); definitionImport.setData(parseRequests(openAPI, request)); return definitionImport; } + + // 鉴权设置 + private List setAuths(ApiTestImportRequest request) { + List auths = new ArrayList<>(); + // 如果有 BaseAuth 参数,base64 编码后转换成 headers + if(request.getAuthManager() != null + && StringUtils.isNotBlank(request.getAuthManager().getUsername()) + && StringUtils.isNotBlank(request.getAuthManager().getPassword()) + && request.getAuthManager().getVerification().equals("Basic Auth")){ + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType("header"); + authorizationValue.setKeyName("Authorization"); + String authValue = "Basic " + Base64.getUrlEncoder().encodeToString((request.getAuthManager().getUsername() + + ":" + request.getAuthManager().getPassword()).getBytes()); + authorizationValue.setValue(authValue); + auths.add(authorizationValue); + } + // 设置 headers + if(!CollectionUtils.isEmpty(request.getHeaders())){ + for(KeyValue keyValue : request.getHeaders()){ + // 当有 key 时才进行设置 + if(StringUtils.isNotBlank(keyValue.getName())){ + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType("header"); + authorizationValue.setKeyName(keyValue.getName()); + authorizationValue.setValue(keyValue.getValue()); + auths.add(authorizationValue); + } + } + } + // 设置 query 参数 + if(!CollectionUtils.isEmpty(request.getArguments())){ + for(KeyValue keyValue : request.getArguments()){ + if(StringUtils.isNotBlank(keyValue.getName())){ + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType("query"); + authorizationValue.setKeyName(keyValue.getName()); + authorizationValue.setValue(keyValue.getValue()); + auths.add(authorizationValue); + } + } + } + return CollectionUtils.size(auths) == 0 ? null : auths; + } + + + private List parseRequests(OpenAPI openAPI, ApiTestImportRequest importRequest) { Paths paths = openAPI.getPaths(); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 378a72447e..6b827f1fc0 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -66,7 +66,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.multipart.MultipartFile; import sun.security.util.Cache; - import javax.annotation.Resource; import java.net.MalformedURLException; import java.util.*; @@ -1351,10 +1350,13 @@ public class ApiDefinitionService { /*swagger定时导入*/ public void createSchedule(ScheduleRequest request) { + String config = setAuthParams(request); /*保存swaggerUrl*/ SwaggerUrlProject swaggerUrlProject = new SwaggerUrlProject(); BeanUtils.copyBean(swaggerUrlProject, request); swaggerUrlProject.setId(UUID.randomUUID().toString()); + // 设置鉴权信息 + swaggerUrlProject.setConfig(config); scheduleService.addSwaggerUrlSchedule(swaggerUrlProject); request.setResourceId(swaggerUrlProject.getId()); @@ -1375,8 +1377,11 @@ public class ApiDefinitionService { } public void updateSchedule(ScheduleRequest request) { + String config = setAuthParams(request); SwaggerUrlProject swaggerUrlProject = new SwaggerUrlProject(); BeanUtils.copyBean(swaggerUrlProject, request); + // 设置鉴权信息 + swaggerUrlProject.setConfig(config); scheduleService.updateSwaggerUrlSchedule(swaggerUrlProject); // 只修改表达式和名称 Schedule schedule = new Schedule(); @@ -1394,6 +1399,24 @@ public class ApiDefinitionService { this.addOrUpdateSwaggerImportCronJob(request); } + + // 设置 SwaggerUrl 同步鉴权参数 + public String setAuthParams(ScheduleRequest request){ + // list 数组转化成 json 字符串 + JSONObject configObj = new JSONObject(); + configObj.put("headers", request.getHeaders()); + configObj.put("arguments", request.getArguments()); + // 设置 BaseAuth 参数 + if(request.getAuthManager() != null + && StringUtils.isNotBlank(request.getAuthManager().getUsername()) + && StringUtils.isNotBlank(request.getAuthManager().getPassword())){ + configObj.put("authManager", request.getAuthManager()); + } + return JSONObject.toJSONString(configObj); + } + + + /** * 列表开关切换 * diff --git a/backend/src/main/java/io/metersphere/base/domain/SwaggerUrlProject.java b/backend/src/main/java/io/metersphere/base/domain/SwaggerUrlProject.java index 025660bfbc..82bdf9865d 100644 --- a/backend/src/main/java/io/metersphere/base/domain/SwaggerUrlProject.java +++ b/backend/src/main/java/io/metersphere/base/domain/SwaggerUrlProject.java @@ -1,8 +1,7 @@ package io.metersphere.base.domain; -import lombok.Data; - import java.io.Serializable; +import lombok.Data; @Data public class SwaggerUrlProject implements Serializable { @@ -18,5 +17,7 @@ public class SwaggerUrlProject implements Serializable { private String modeId; + private String config; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.java b/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.java index 3984f00183..62fe4f7f9a 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.java @@ -2,9 +2,8 @@ package io.metersphere.base.mapper; import io.metersphere.base.domain.SwaggerUrlProject; import io.metersphere.base.domain.SwaggerUrlProjectExample; -import org.apache.ibatis.annotations.Param; - import java.util.List; +import org.apache.ibatis.annotations.Param; public interface SwaggerUrlProjectMapper { long countByExample(SwaggerUrlProjectExample example); @@ -17,15 +16,21 @@ public interface SwaggerUrlProjectMapper { int insertSelective(SwaggerUrlProject record); + List selectByExampleWithBLOBs(SwaggerUrlProjectExample example); + List selectByExample(SwaggerUrlProjectExample example); SwaggerUrlProject selectByPrimaryKey(String id); int updateByExampleSelective(@Param("record") SwaggerUrlProject record, @Param("example") SwaggerUrlProjectExample example); + int updateByExampleWithBLOBs(@Param("record") SwaggerUrlProject record, @Param("example") SwaggerUrlProjectExample example); + int updateByExample(@Param("record") SwaggerUrlProject record, @Param("example") SwaggerUrlProjectExample example); int updateByPrimaryKeySelective(SwaggerUrlProject record); + int updateByPrimaryKeyWithBLOBs(SwaggerUrlProject record); + int updateByPrimaryKey(SwaggerUrlProject record); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.xml index 6fe4cf1452..2db45dd488 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/SwaggerUrlProjectMapper.xml @@ -2,12 +2,15 @@ - - - - - - + + + + + + + + + @@ -70,43 +73,64 @@ id, project_id, swagger_url, module_id, module_path, mode_id - select distinct - + + , + from swagger_url_project - + order by ${orderByClause} - select - + + distinct + + + from swagger_url_project + + + + + order by ${orderByClause} + + + - delete - from swagger_url_project + delete from swagger_url_project where id = #{id,jdbcType=VARCHAR} delete from swagger_url_project - + - insert into swagger_url_project (id, project_id, swagger_url, - module_id, module_path, mode_id) - values (#{id,jdbcType=VARCHAR}, #{projectId,jdbcType=VARCHAR}, #{swaggerUrl,jdbcType=VARCHAR}, - #{moduleId,jdbcType=VARCHAR}, #{modulePath,jdbcType=VARCHAR}, #{modeId,jdbcType=VARCHAR}) + insert into swagger_url_project (id, project_id, swagger_url, + module_id, module_path, mode_id, + config) + values (#{id,jdbcType=VARCHAR}, #{projectId,jdbcType=VARCHAR}, #{swaggerUrl,jdbcType=VARCHAR}, + #{moduleId,jdbcType=VARCHAR}, #{modulePath,jdbcType=VARCHAR}, #{modeId,jdbcType=VARCHAR}, + #{config,jdbcType=LONGVARCHAR}) insert into swagger_url_project @@ -129,6 +153,9 @@ mode_id, + + config, + @@ -149,13 +176,15 @@ #{modeId,jdbcType=VARCHAR}, + + #{config,jdbcType=LONGVARCHAR}, + - select count(*) from swagger_url_project - + @@ -179,21 +208,37 @@ mode_id = #{record.modeId,jdbcType=VARCHAR}, + + config = #{record.config,jdbcType=LONGVARCHAR}, + - + + + + + update swagger_url_project + set id = #{record.id,jdbcType=VARCHAR}, + project_id = #{record.projectId,jdbcType=VARCHAR}, + swagger_url = #{record.swaggerUrl,jdbcType=VARCHAR}, + module_id = #{record.moduleId,jdbcType=VARCHAR}, + module_path = #{record.modulePath,jdbcType=VARCHAR}, + mode_id = #{record.modeId,jdbcType=VARCHAR}, + config = #{record.config,jdbcType=LONGVARCHAR} + + update swagger_url_project set id = #{record.id,jdbcType=VARCHAR}, - project_id = #{record.projectId,jdbcType=VARCHAR}, - swagger_url = #{record.swaggerUrl,jdbcType=VARCHAR}, - module_id = #{record.moduleId,jdbcType=VARCHAR}, - module_path = #{record.modulePath,jdbcType=VARCHAR}, - mode_id = #{record.modeId,jdbcType=VARCHAR} + project_id = #{record.projectId,jdbcType=VARCHAR}, + swagger_url = #{record.swaggerUrl,jdbcType=VARCHAR}, + module_id = #{record.moduleId,jdbcType=VARCHAR}, + module_path = #{record.modulePath,jdbcType=VARCHAR}, + mode_id = #{record.modeId,jdbcType=VARCHAR} - + @@ -214,16 +259,29 @@ mode_id = #{modeId,jdbcType=VARCHAR}, + + config = #{config,jdbcType=LONGVARCHAR}, + where id = #{id,jdbcType=VARCHAR} + + update swagger_url_project + set project_id = #{projectId,jdbcType=VARCHAR}, + swagger_url = #{swaggerUrl,jdbcType=VARCHAR}, + module_id = #{moduleId,jdbcType=VARCHAR}, + module_path = #{modulePath,jdbcType=VARCHAR}, + mode_id = #{modeId,jdbcType=VARCHAR}, + config = #{config,jdbcType=LONGVARCHAR} + where id = #{id,jdbcType=VARCHAR} + update swagger_url_project - set project_id = #{projectId,jdbcType=VARCHAR}, - swagger_url = #{swaggerUrl,jdbcType=VARCHAR}, - module_id = #{moduleId,jdbcType=VARCHAR}, - module_path = #{modulePath,jdbcType=VARCHAR}, - mode_id = #{modeId,jdbcType=VARCHAR} + set project_id = #{projectId,jdbcType=VARCHAR}, + swagger_url = #{swaggerUrl,jdbcType=VARCHAR}, + module_id = #{moduleId,jdbcType=VARCHAR}, + module_path = #{modulePath,jdbcType=VARCHAR}, + mode_id = #{modeId,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml index 7799b2f8d4..e803a7256b 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml @@ -9,6 +9,7 @@ sup.mode_id, sup.module_id, sup.project_id, + sup.config, sch.value as rule, sch.enable, sch.id as taskId diff --git a/backend/src/main/java/io/metersphere/controller/request/ScheduleRequest.java b/backend/src/main/java/io/metersphere/controller/request/ScheduleRequest.java index 901519fe19..6dd3360e1a 100644 --- a/backend/src/main/java/io/metersphere/controller/request/ScheduleRequest.java +++ b/backend/src/main/java/io/metersphere/controller/request/ScheduleRequest.java @@ -1,8 +1,11 @@ package io.metersphere.controller.request; +import io.metersphere.api.dto.definition.request.auth.MsAuthManager; +import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.base.domain.Schedule; import lombok.Getter; import lombok.Setter; +import java.util.List; /** * @author song.tianyang @@ -28,4 +31,9 @@ public class ScheduleRequest extends Schedule { private String taskId; + // 鉴权相关 + private List headers; + private List arguments; + private MsAuthManager authManager; + } diff --git a/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java b/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java index 45f1872eb8..de9912e51b 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java @@ -1,14 +1,21 @@ package io.metersphere.job.sechedule; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiTestImportRequest; +import io.metersphere.api.dto.definition.request.auth.MsAuthManager; +import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.base.domain.SwaggerUrlProject; import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.utils.CommonBeanFactory; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobKey; import org.quartz.TriggerKey; +import java.util.List; public class SwaggerUrlImportJob extends MsScheduleJob { private ApiDefinitionService apiDefinitionService; @@ -21,8 +28,11 @@ public class SwaggerUrlImportJob extends MsScheduleJob { void businessExecute(JobExecutionContext context) { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); String resourceId = jobDataMap.getString("resourceId"); - SwaggerUrlProject swaggerUrlProject=apiDefinitionService.getSwaggerInfo(resourceId); + SwaggerUrlProject swaggerUrlProject = apiDefinitionService.getSwaggerInfo(resourceId); ApiTestImportRequest request = new ApiTestImportRequest(); + // 获取鉴权设置 + String config = swaggerUrlProject.getConfig(); + setAuthInfo(config, request); request.setProjectId(swaggerUrlProject.getProjectId()); request.setSwaggerUrl(swaggerUrlProject.getSwaggerUrl()); request.setModuleId(swaggerUrlProject.getModuleId()); @@ -41,4 +51,24 @@ public class SwaggerUrlImportJob extends MsScheduleJob { public static TriggerKey getTriggerKey(String resourceId) { return new TriggerKey(resourceId, ScheduleGroup.SWAGGER_IMPORT.name()); } + + public void setAuthInfo(String config, ApiTestImportRequest request){ + // 获取鉴权设置 + if(StringUtils.isNotBlank(config)){ + JSONObject configObj = JSON.parseObject(config); + List headers = JSONObject.parseArray(configObj.getString("headers"), KeyValue.class); + if(CollectionUtils.isNotEmpty(headers)){ + request.setHeaders(headers); + } + List arguments = JSONObject.parseArray(configObj.getString("arguments"), KeyValue.class); + if(CollectionUtils.isNotEmpty(arguments)){ + request.setArguments(arguments); + } + MsAuthManager msAuthManager = JSONObject.parseObject(configObj.getString("authManager"), MsAuthManager.class); + if(msAuthManager != null){ + request.setAuthManager(msAuthManager); + } + } + } + } diff --git a/backend/src/main/resources/db/migration/V102__v1.15.4_release.sql b/backend/src/main/resources/db/migration/V102__v1.15.4_release.sql new file mode 100644 index 0000000000..b2247a93b2 --- /dev/null +++ b/backend/src/main/resources/db/migration/V102__v1.15.4_release.sql @@ -0,0 +1,2 @@ +-- 新增字段 +ALTER TABLE `swagger_url_project` ADD COLUMN `config` longtext COMMENT '鉴权配置信息' AFTER `mode_id`; \ No newline at end of file diff --git a/frontend/src/business/components/api/definition/components/auth/ApiAuthConfig.vue b/frontend/src/business/components/api/definition/components/auth/ApiAuthConfig.vue index 907c105d33..2accb865a6 100644 --- a/frontend/src/business/components/api/definition/components/auth/ApiAuthConfig.vue +++ b/frontend/src/business/components/api/definition/components/auth/ApiAuthConfig.vue @@ -34,7 +34,7 @@ - + @@ -62,19 +62,18 @@ export default { components: {}, props: { request: {}, + encryptShow: { + type: Boolean, + default: true, + } + }, + watch: { + request() { + this.initData(); + } }, created() { - if (this.request.hashTree) { - for (let index in this.request.hashTree) { - if (this.request.hashTree[index].type == 'AuthManager') { - this.request.authManager = this.request.hashTree[index]; - this.request.hashTree.splice(index, 1); - } - } - } - if (this.request.authManager) { - this.authConfig = this.request.authManager; - } + this.initData(); }, data() { return { @@ -91,8 +90,14 @@ export default { let authManager = createComponent("AuthManager"); authManager.verification = "Basic Auth"; authManager.environment = this.request.useEnvironment; + if(this.request.hashTree == undefined){ + this.request.hashTree = []; + } this.request.hashTree.push(authManager); - this.authConfig = authManager; + // 这里做个判断,如果原来有值则不覆盖 + if(this.authConfig.username == undefined && this.authConfig.password == undefined){ + this.authConfig = authManager; + } } else { for (let index in this.request.hashTree) { if (this.request.hashTree[index].type === "AuthManager") { @@ -102,6 +107,19 @@ export default { this.request.authManager = {}; } this.request.authManager = this.authConfig; + }, + initData(){ + if (this.request.hashTree) { + for (let index in this.request.hashTree) { + if (this.request.hashTree[index].type == 'AuthManager') { + this.request.authManager = this.request.hashTree[index]; + this.request.hashTree.splice(index, 1); + } + } + } + if (this.request.authManager) { + this.authConfig = this.request.authManager; + } } } } diff --git a/frontend/src/business/components/api/definition/components/complete/EditCompleteHTTPApi.vue b/frontend/src/business/components/api/definition/components/complete/EditCompleteHTTPApi.vue index 2c0b66d697..4375579bb1 100644 --- a/frontend/src/business/components/api/definition/components/complete/EditCompleteHTTPApi.vue +++ b/frontend/src/business/components/api/definition/components/complete/EditCompleteHTTPApi.vue @@ -144,7 +144,7 @@ components: { ApiOtherInfo, MsFormDivider, - MsJsr233Processor, MsResponseText, MsApiRequestForm, MsInputTag, MsSelectTree,MsChangeHistory}, + MsJsr233Processor, MsResponseText, MsApiRequestForm, MsInputTag, MsSelectTree, MsChangeHistory}, data() { let validateURL = (rule, value, callback) => { if (!this.httpForm.path.startsWith("/") || this.httpForm.path.match(/\s/) != null) { diff --git a/frontend/src/business/components/api/definition/components/import/ApiImport.vue b/frontend/src/business/components/api/definition/components/import/ApiImport.vue index 11613cd086..dd0b698e83 100644 --- a/frontend/src/business/components/api/definition/components/import/ApiImport.vue +++ b/frontend/src/business/components/api/definition/components/import/ApiImport.vue @@ -1,5 +1,5 @@