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 4ff7637c67..d6ad340de4 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 @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; 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.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem; import io.metersphere.api.dto.definition.response.HttpResponse; @@ -72,7 +73,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { if (request.getAuthManager() != null && StringUtils.isNotBlank(request.getAuthManager().getUsername()) && StringUtils.isNotBlank(request.getAuthManager().getPassword()) - && request.getAuthManager().getVerification().equals("Basic Auth")) { + && MsAuthManager.mechanismMap.containsKey(request.getAuthManager().getVerification())) { AuthorizationValue authorizationValue = new AuthorizationValue(); authorizationValue.setType("header"); authorizationValue.setKeyName("Authorization"); 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 b7203f5481..7ca7b0a87a 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 @@ -9,6 +9,7 @@ import io.metersphere.api.dto.definition.SwaggerApiExportResult; import io.metersphere.api.dto.definition.parse.swagger.SwaggerApiInfo; import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo; import io.metersphere.api.dto.definition.parse.swagger.SwaggerParams; +import io.metersphere.api.dto.definition.request.auth.MsAuthManager; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem; import io.metersphere.api.dto.definition.response.HttpResponse; @@ -80,7 +81,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { if (request.getAuthManager() != null && StringUtils.isNotBlank(request.getAuthManager().getUsername()) && StringUtils.isNotBlank(request.getAuthManager().getPassword()) - && request.getAuthManager().getVerification().equals("Basic Auth")) { + && MsAuthManager.mechanismMap.containsKey(request.getAuthManager().getVerification())) { AuthorizationValue authorizationValue = new AuthorizationValue(); authorizationValue.setType("header"); authorizationValue.setKeyName("Authorization"); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java index ec48eba466..e6a1871578 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java @@ -16,7 +16,9 @@ import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Data @EqualsAndHashCode(callSuper = true) @@ -52,18 +54,19 @@ public class MsAuthManager extends MsTestElement { @JSONField(ordinal = 28) private String environment; + public static final Map mechanismMap = new HashMap<>() {{ + this.put("Basic Auth", AuthManager.Mechanism.BASIC); + this.put("Digest Auth", AuthManager.Mechanism.DIGEST); + }}; + @Override public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { if (!this.isEnable()) { return; } - if (StringUtils.equals(this.getVerification(), "Basic Auth")) { + if (mechanismMap.containsKey(this.getVerification())) { ParameterConfig config = (ParameterConfig) msParameter; - AuthManager authManager = new AuthManager(); - authManager.setEnabled(true); - authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager"); - authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName()); - authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel")); + AuthManager authManager = initBase(); Authorization auth = new Authorization(); if (this.url != null) { auth.setURL(this.url); @@ -77,7 +80,7 @@ public class MsAuthManager extends MsTestElement { } auth.setUser(this.username); auth.setPass(this.password); - auth.setMechanism(AuthManager.Mechanism.DIGEST); + auth.setMechanism(mechanismMap.get(this.getVerification())); authManager.addAuth(auth); tree.add(authManager); } @@ -85,20 +88,25 @@ public class MsAuthManager extends MsTestElement { public void setAuth(HashTree tree, MsAuthManager msAuthManager, HTTPSamplerProxy samplerProxy) { try { - AuthManager authManager = new AuthManager(); - authManager.setEnabled(true); - authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager"); - authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName()); - authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel")); + AuthManager authManager = initBase(); Authorization auth = new Authorization(); auth.setURL(samplerProxy.getProtocol() + "://" + samplerProxy.getDomain()); auth.setUser(msAuthManager.getUsername()); auth.setPass(msAuthManager.getPassword()); - auth.setMechanism(AuthManager.Mechanism.DIGEST); + auth.setMechanism(mechanismMap.get(msAuthManager.getVerification())); authManager.addAuth(auth); tree.add(authManager); } catch (Exception e) { LogUtil.error(e); } } + + private AuthManager initBase() { + AuthManager authManager = new AuthManager(); + authManager.setEnabled(true); + authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager"); + authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName()); + authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel")); + return authManager; + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 12927347f8..649b491986 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -34,7 +34,6 @@ import io.metersphere.commons.utils.LogUtil; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; -import io.metersphere.utils.LoggerUtil; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -238,7 +237,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()), httpConfig); } - if (this.authManager != null && StringUtils.equals(this.authManager.getVerification(), "Basic Auth")) { + if (this.authManager != null && MsAuthManager.mechanismMap.containsKey(this.authManager.getVerification())) { this.authManager.setAuth(httpSamplerTree, this.authManager, sampler); } @@ -665,28 +664,28 @@ public class MsHTTPSamplerProxy extends MsTestElement { list.stream(). filter(KeyValue::isValid). filter(KeyValue::isEnable).forEach(keyValue -> { - try { - String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); - HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); - if (keyValue.getValue() == null) { - httpArgument.setValue(""); - } - httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); - if (StringUtils.isNotBlank(keyValue.getContentType())) { - httpArgument.setContentType(keyValue.getContentType()); - } - if (StringUtils.equalsIgnoreCase(this.method, "get")) { - if (StringUtils.isNotEmpty(httpArgument.getValue())) { - arguments.addArgument(httpArgument); - } - } else { - arguments.addArgument(httpArgument); - } - } catch (Exception e) { - - } + try { + String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); + HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); + if (keyValue.getValue() == null) { + httpArgument.setValue(""); } - ); + httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); + if (StringUtils.isNotBlank(keyValue.getContentType())) { + httpArgument.setContentType(keyValue.getContentType()); + } + if (StringUtils.equalsIgnoreCase(this.method, "get")) { + if (StringUtils.isNotEmpty(httpArgument.getValue())) { + arguments.addArgument(httpArgument); + } + } else { + arguments.addArgument(httpArgument); + } + } catch (Exception e) { + + } + } + ); return arguments; } 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 e18bf79339..2519c39871 100644 --- a/frontend/src/business/components/api/definition/components/auth/ApiAuthConfig.vue +++ b/frontend/src/business/components/api/definition/components/auth/ApiAuthConfig.vue @@ -8,10 +8,10 @@ + v-for="item in options" + :key="item.name" + :label="item.name" + :value="item.name"> @@ -43,10 +43,10 @@ + v-for="item in encryptOptions" + :key="item.id" + :label="item.name" + :value="item.id"> @@ -79,7 +79,7 @@ export default { }, data() { return { - options: [{name: "No Auth"}, {name: "Basic Auth"}], + options: [{name: "No Auth"}, {name: "Basic Auth"}, {name: "Digest Auth"}], encryptOptions: [{id: false, name: this.$t('commons.encrypted')}], activeName: "verified", rule: {}, @@ -88,9 +88,9 @@ export default { }, methods: { change() { - if (this.authConfig.verification === "Basic Auth") { + if (this.authConfig.verification === "Basic Auth" || this.authConfig.verification === "Digest Auth") { let authManager = createComponent("AuthManager"); - authManager.verification = "Basic Auth"; + authManager.verification = this.authConfig.verification; authManager.environment = this.request.useEnvironment; if (this.request.hashTree == undefined) { this.request.hashTree = [];