mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-05-08 03:16:39 +08:00
feat:swagger 添加认证配置
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.ssssssss.magicapi.swagger;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -23,9 +22,7 @@ import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(SwaggerConfig.class)
|
||||
@@ -58,8 +55,37 @@ public class MagicSwaggerConfiguration implements MagicPluginConfiguration {
|
||||
RequestMappingInfo requestMappingInfo = mapping.paths(swaggerConfig.getLocation()).build();
|
||||
SwaggerEntity.License license = new SwaggerEntity.License("MIT", "https://gitee.com/ssssssss-team/magic-api/blob/master/LICENSE");
|
||||
SwaggerEntity.Info info = new SwaggerEntity.Info(swaggerConfig.getDescription(), swaggerConfig.getVersion(), swaggerConfig.getTitle(), license, swaggerConfig.getConcat());
|
||||
|
||||
//具体参考:https://swagger.io/docs/specification/2-0/authentication/
|
||||
Map<String, Object> securityDefinitionMap = new HashMap<>();
|
||||
Map<String, Object> securityMap = new HashMap<>();
|
||||
|
||||
if (swaggerConfig.getBasicAuth() != null) {
|
||||
securityDefinitionMap.put(SwaggerEntity.BasicAuth.KEY_NAME, swaggerConfig.getBasicAuth());
|
||||
|
||||
//the Basic and API key security items use an empty array instead.
|
||||
securityMap.put(SwaggerEntity.BasicAuth.KEY_NAME, new String[]{});
|
||||
}
|
||||
if (swaggerConfig.getApiKeyAuth() != null) {
|
||||
securityDefinitionMap.put(SwaggerEntity.ApiKeyAuth.KEY_NAME, swaggerConfig.getApiKeyAuth());
|
||||
|
||||
//the Basic and API key security items use an empty array instead.
|
||||
securityMap.put(SwaggerEntity.ApiKeyAuth.KEY_NAME, new String[]{});
|
||||
}
|
||||
if (swaggerConfig.getOauth2() != null) {
|
||||
SwaggerEntity.OAuth2 oAuth2 = swaggerConfig.getOauth2();
|
||||
securityDefinitionMap.put(SwaggerEntity.OAuth2.KEY_NAME, oAuth2);
|
||||
|
||||
Map<String, String> scopes = oAuth2.getScopes();
|
||||
if (scopes != null) {
|
||||
Set<String> strings = scopes.keySet();
|
||||
securityMap.put(SwaggerEntity.OAuth2.KEY_NAME, strings);
|
||||
}
|
||||
}
|
||||
|
||||
// 构建文档信息
|
||||
SwaggerProvider swaggerProvider = new SwaggerProvider(requestMagicDynamicRegistry, magicResourceService, servletContext.getContextPath(), info, properties.isPersistenceResponseBody(), properties.getPrefix());
|
||||
SwaggerProvider swaggerProvider = new SwaggerProvider(requestMagicDynamicRegistry, magicResourceService, servletContext.getContextPath(),
|
||||
info, properties.isPersistenceResponseBody(), properties.getPrefix(), securityDefinitionMap, securityMap);
|
||||
|
||||
|
||||
// 注册swagger.json
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.ssssssss.magicapi.swagger;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
import org.ssssssss.magicapi.swagger.entity.SwaggerEntity;
|
||||
|
||||
@@ -36,6 +35,24 @@ public class SwaggerConfig {
|
||||
@NestedConfigurationProperty
|
||||
private SwaggerEntity.Concat concat = new SwaggerEntity.Concat();
|
||||
|
||||
/**
|
||||
* 基本认证
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private SwaggerEntity.BasicAuth basicAuth;
|
||||
|
||||
/**
|
||||
* api密钥认证
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private SwaggerEntity.ApiKeyAuth apiKeyAuth;
|
||||
|
||||
/**
|
||||
* oauth2认证
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private SwaggerEntity.OAuth2 oauth2;
|
||||
|
||||
/**
|
||||
* 文档版本
|
||||
*/
|
||||
@@ -88,4 +105,28 @@ public class SwaggerConfig {
|
||||
public void setConcat(SwaggerEntity.Concat concat) {
|
||||
this.concat = concat;
|
||||
}
|
||||
|
||||
public SwaggerEntity.ApiKeyAuth getApiKeyAuth() {
|
||||
return apiKeyAuth;
|
||||
}
|
||||
|
||||
public void setApiKeyAuth(SwaggerEntity.ApiKeyAuth apiKeyAuth) {
|
||||
this.apiKeyAuth = apiKeyAuth;
|
||||
}
|
||||
|
||||
public SwaggerEntity.BasicAuth getBasicAuth() {
|
||||
return basicAuth;
|
||||
}
|
||||
|
||||
public void setBasicAuth(SwaggerEntity.BasicAuth basicAuth) {
|
||||
this.basicAuth = basicAuth;
|
||||
}
|
||||
|
||||
public SwaggerEntity.OAuth2 getOauth2() {
|
||||
return oauth2;
|
||||
}
|
||||
|
||||
public void setOauth2(SwaggerEntity.OAuth2 oauth2) {
|
||||
this.oauth2 = oauth2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ public class SwaggerEntity {
|
||||
|
||||
private Info info;
|
||||
|
||||
private final Map<String, Object> securityDefinitions = new HashMap<>();
|
||||
|
||||
private final List<Map<String, Object>> security = new ArrayList<>();
|
||||
|
||||
private final Set<Tag> tags = new TreeSet<>(Comparator.comparing(Tag::getName));
|
||||
|
||||
private final Map<String, Object> definitions = new HashMap<>();
|
||||
@@ -142,6 +146,22 @@ public class SwaggerEntity {
|
||||
return paths;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSecurityDefinitions() {
|
||||
return securityDefinitions;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSecurity() {
|
||||
return security;
|
||||
}
|
||||
|
||||
public void addSecurityDefinitions(Map<String, Object> map) {
|
||||
securityDefinitions.putAll(map);
|
||||
}
|
||||
|
||||
public void addSecurity(Map<String, Object> map) {
|
||||
security.add(map);
|
||||
}
|
||||
|
||||
public static class Concat {
|
||||
|
||||
private String name;
|
||||
@@ -479,7 +499,6 @@ public class SwaggerEntity {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class License {
|
||||
|
||||
private String name;
|
||||
@@ -507,4 +526,112 @@ public class SwaggerEntity {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BasicAuth {
|
||||
|
||||
public final static String KEY_NAME = "BasicAuth";
|
||||
|
||||
/**
|
||||
* 类型,默认值
|
||||
*/
|
||||
private String type = "basic";
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ApiKeyAuth {
|
||||
|
||||
public final static String KEY_NAME = "ApiKeyAuth";
|
||||
|
||||
private String type = "apiKey";
|
||||
|
||||
private String name = "header";
|
||||
|
||||
private String in = "X-API-Key";
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getIn() {
|
||||
return in;
|
||||
}
|
||||
|
||||
public void setIn(String in) {
|
||||
this.in = in;
|
||||
}
|
||||
}
|
||||
|
||||
public static class OAuth2 {
|
||||
|
||||
public final static String KEY_NAME = "OAuth2";
|
||||
|
||||
private String type = "oauth2";
|
||||
|
||||
private String flow;
|
||||
|
||||
private String authorizationUrl;
|
||||
|
||||
private String tokenUrl;
|
||||
|
||||
private Map<String, String> scopes;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getFlow() {
|
||||
return flow;
|
||||
}
|
||||
|
||||
public void setFlow(String flow) {
|
||||
this.flow = flow;
|
||||
}
|
||||
|
||||
public String getAuthorizationUrl() {
|
||||
return authorizationUrl;
|
||||
}
|
||||
|
||||
public void setAuthorizationUrl(String authorizationUrl) {
|
||||
this.authorizationUrl = authorizationUrl;
|
||||
}
|
||||
|
||||
public String getTokenUrl() {
|
||||
return tokenUrl;
|
||||
}
|
||||
|
||||
public void setTokenUrl(String tokenUrl) {
|
||||
this.tokenUrl = tokenUrl;
|
||||
}
|
||||
|
||||
public Map<String, String> getScopes() {
|
||||
return scopes;
|
||||
}
|
||||
|
||||
public void setScopes(Map<String, String> scopes) {
|
||||
this.scopes = scopes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,14 +47,19 @@ public class SwaggerProvider {
|
||||
private final SwaggerEntity.Info info;
|
||||
private final boolean persistenceResponseBody;
|
||||
private final String prefix;
|
||||
private final Map<String, Object> securityDefinitionMap;
|
||||
private final Map<String, Object> securityMap;
|
||||
|
||||
public SwaggerProvider(RequestMagicDynamicRegistry requestMagicDynamicRegistry, MagicResourceService magicResourceService, String basePath, SwaggerEntity.Info info, boolean persistenceResponseBody, String prefix) {
|
||||
public SwaggerProvider(RequestMagicDynamicRegistry requestMagicDynamicRegistry, MagicResourceService magicResourceService,
|
||||
String basePath, SwaggerEntity.Info info, boolean persistenceResponseBody, String prefix, Map<String, Object> securityDefinitionMap, Map<String, Object> securityMap) {
|
||||
this.requestMagicDynamicRegistry = requestMagicDynamicRegistry;
|
||||
this.magicResourceService = magicResourceService;
|
||||
this.basePath = basePath;
|
||||
this.info = info;
|
||||
this.persistenceResponseBody = persistenceResponseBody;
|
||||
this.prefix = StringUtils.defaultIfBlank(prefix, "") + "/";
|
||||
this.securityDefinitionMap = securityDefinitionMap;
|
||||
this.securityMap = securityMap;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@@ -64,6 +69,9 @@ public class SwaggerProvider {
|
||||
SwaggerEntity swaggerEntity = new SwaggerEntity();
|
||||
swaggerEntity.setInfo(info);
|
||||
swaggerEntity.setBasePath(this.basePath);
|
||||
swaggerEntity.addSecurityDefinitions(securityDefinitionMap);
|
||||
swaggerEntity.addSecurity(securityMap);
|
||||
|
||||
for (ApiInfo info : infos) {
|
||||
String groupName = magicResourceService.getGroupName(info.getGroupId()).replace("/", "-");
|
||||
String requestPath = PathUtils.replaceSlash(this.prefix + magicResourceService.getGroupPath(info.getGroupId()) + "/" + info.getPath());
|
||||
|
||||
Reference in New Issue
Block a user