mirror of
https://github.com/yangzongzhuan/RuoYi-Cloud.git
synced 2025-09-04 11:50:59 +00:00
若依 1.0
This commit is contained in:
34
ruoyi-common/ruoyi-common-swagger/pom.xml
Normal file
34
ruoyi-common/ruoyi-common-swagger/pom.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-swagger</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-common-swagger系统接口
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- SpringBoot Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Swagger-->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${swagger.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,20 @@
|
||||
package com.ruoyi.common.swagger.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import com.ruoyi.common.swagger.config.SwaggerAutoConfiguration;
|
||||
|
||||
@Target({ ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Import({ SwaggerAutoConfiguration.class })
|
||||
public @interface EnableCustomSwagger2
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,132 @@
|
||||
package com.ruoyi.common.swagger.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.AuthorizationScope;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.service.GrantType;
|
||||
import springfox.documentation.service.OAuth;
|
||||
import springfox.documentation.service.ResourceOwnerPasswordCredentialsGrant;
|
||||
import springfox.documentation.service.SecurityReference;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableAutoConfiguration
|
||||
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
|
||||
public class SwaggerAutoConfiguration
|
||||
{
|
||||
/**
|
||||
* 默认的排除路径,排除Spring Boot默认的错误处理路径和端点
|
||||
*/
|
||||
private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");
|
||||
|
||||
private static final String BASE_PATH = "/**";
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SwaggerProperties swaggerProperties()
|
||||
{
|
||||
return new SwaggerProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Docket api(SwaggerProperties swaggerProperties)
|
||||
{
|
||||
// base-path处理
|
||||
if (swaggerProperties.getBasePath().isEmpty())
|
||||
{
|
||||
swaggerProperties.getBasePath().add(BASE_PATH);
|
||||
}
|
||||
// noinspection unchecked
|
||||
List<Predicate<String>> basePath = new ArrayList<Predicate<String>>();
|
||||
swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));
|
||||
|
||||
// exclude-path处理
|
||||
if (swaggerProperties.getExcludePath().isEmpty())
|
||||
{
|
||||
swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);
|
||||
}
|
||||
List<Predicate<String>> excludePath = new ArrayList<>();
|
||||
swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path)));
|
||||
|
||||
//noinspection Guava
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.host(swaggerProperties.getHost())
|
||||
.apiInfo(apiInfo(swaggerProperties)).select()
|
||||
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
|
||||
.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath)))
|
||||
.build()
|
||||
.securitySchemes(Collections.singletonList(securitySchema()))
|
||||
.securityContexts(Collections.singletonList(securityContext()))
|
||||
.pathMapping("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private SecurityContext securityContext()
|
||||
{
|
||||
return SecurityContext.builder()
|
||||
.securityReferences(defaultAuth())
|
||||
.forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认的全局鉴权策略
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private List<SecurityReference> defaultAuth()
|
||||
{
|
||||
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
||||
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
|
||||
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
|
||||
return Collections.singletonList(SecurityReference.builder()
|
||||
.reference(swaggerProperties().getAuthorization().getName())
|
||||
.scopes(authorizationScopeList.toArray(authorizationScopes))
|
||||
.build());
|
||||
}
|
||||
|
||||
private OAuth securitySchema()
|
||||
{
|
||||
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
||||
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
|
||||
ArrayList<GrantType> grantTypes = new ArrayList<>();
|
||||
swaggerProperties().getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
|
||||
return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes);
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo(SwaggerProperties swaggerProperties)
|
||||
{
|
||||
return new ApiInfoBuilder()
|
||||
.title(swaggerProperties.getTitle())
|
||||
.description(swaggerProperties.getDescription())
|
||||
.license(swaggerProperties.getLicense())
|
||||
.licenseUrl(swaggerProperties.getLicenseUrl())
|
||||
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
|
||||
.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
|
||||
.version(swaggerProperties.getVersion())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,343 @@
|
||||
package com.ruoyi.common.swagger.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties("swagger")
|
||||
public class SwaggerProperties
|
||||
{
|
||||
/**
|
||||
* 是否开启swagger
|
||||
*/
|
||||
private Boolean enabled;
|
||||
|
||||
/**
|
||||
* swagger会解析的包路径
|
||||
**/
|
||||
private String basePackage = "";
|
||||
|
||||
/**
|
||||
* swagger会解析的url规则
|
||||
**/
|
||||
private List<String> basePath = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 在basePath基础上需要排除的url规则
|
||||
**/
|
||||
private List<String> excludePath = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 标题
|
||||
**/
|
||||
private String title = "";
|
||||
|
||||
/**
|
||||
* 描述
|
||||
**/
|
||||
private String description = "";
|
||||
|
||||
/**
|
||||
* 版本
|
||||
**/
|
||||
private String version = "";
|
||||
|
||||
/**
|
||||
* 许可证
|
||||
**/
|
||||
private String license = "";
|
||||
|
||||
/**
|
||||
* 许可证URL
|
||||
**/
|
||||
private String licenseUrl = "";
|
||||
|
||||
/**
|
||||
* 服务条款URL
|
||||
**/
|
||||
private String termsOfServiceUrl = "";
|
||||
|
||||
/**
|
||||
* host信息
|
||||
**/
|
||||
private String host = "";
|
||||
|
||||
/**
|
||||
* 联系人信息
|
||||
*/
|
||||
private Contact contact = new Contact();
|
||||
|
||||
/**
|
||||
* 全局统一鉴权配置
|
||||
**/
|
||||
private Authorization authorization = new Authorization();
|
||||
|
||||
public Boolean getEnabled()
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(Boolean enabled)
|
||||
{
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getBasePackage()
|
||||
{
|
||||
return basePackage;
|
||||
}
|
||||
|
||||
public void setBasePackage(String basePackage)
|
||||
{
|
||||
this.basePackage = basePackage;
|
||||
}
|
||||
|
||||
public List<String> getBasePath()
|
||||
{
|
||||
return basePath;
|
||||
}
|
||||
|
||||
public void setBasePath(List<String> basePath)
|
||||
{
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
public List<String> getExcludePath()
|
||||
{
|
||||
return excludePath;
|
||||
}
|
||||
|
||||
public void setExcludePath(List<String> excludePath)
|
||||
{
|
||||
this.excludePath = excludePath;
|
||||
}
|
||||
|
||||
public String getTitle()
|
||||
{
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title)
|
||||
{
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription()
|
||||
{
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description)
|
||||
{
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getVersion()
|
||||
{
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version)
|
||||
{
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getLicense()
|
||||
{
|
||||
return license;
|
||||
}
|
||||
|
||||
public void setLicense(String license)
|
||||
{
|
||||
this.license = license;
|
||||
}
|
||||
|
||||
public String getLicenseUrl()
|
||||
{
|
||||
return licenseUrl;
|
||||
}
|
||||
|
||||
public void setLicenseUrl(String licenseUrl)
|
||||
{
|
||||
this.licenseUrl = licenseUrl;
|
||||
}
|
||||
|
||||
public String getTermsOfServiceUrl()
|
||||
{
|
||||
return termsOfServiceUrl;
|
||||
}
|
||||
|
||||
public void setTermsOfServiceUrl(String termsOfServiceUrl)
|
||||
{
|
||||
this.termsOfServiceUrl = termsOfServiceUrl;
|
||||
}
|
||||
|
||||
public String getHost()
|
||||
{
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host)
|
||||
{
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public Contact getContact()
|
||||
{
|
||||
return contact;
|
||||
}
|
||||
|
||||
public void setContact(Contact contact)
|
||||
{
|
||||
this.contact = contact;
|
||||
}
|
||||
|
||||
public Authorization getAuthorization()
|
||||
{
|
||||
return authorization;
|
||||
}
|
||||
|
||||
public void setAuthorization(Authorization authorization)
|
||||
{
|
||||
this.authorization = authorization;
|
||||
}
|
||||
|
||||
public static class Contact
|
||||
{
|
||||
/**
|
||||
* 联系人
|
||||
**/
|
||||
private String name = "";
|
||||
/**
|
||||
* 联系人url
|
||||
**/
|
||||
private String url = "";
|
||||
/**
|
||||
* 联系人email
|
||||
**/
|
||||
private String email = "";
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url)
|
||||
{
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getEmail()
|
||||
{
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email)
|
||||
{
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Authorization
|
||||
{
|
||||
/**
|
||||
* 鉴权策略ID,需要和SecurityReferences ID保持一致
|
||||
*/
|
||||
private String name = "";
|
||||
|
||||
/**
|
||||
* 需要开启鉴权URL的正则
|
||||
*/
|
||||
private String authRegex = "^.*$";
|
||||
|
||||
/**
|
||||
* 鉴权作用域列表
|
||||
*/
|
||||
private List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
||||
|
||||
private List<String> tokenUrlList = new ArrayList<>();
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAuthRegex()
|
||||
{
|
||||
return authRegex;
|
||||
}
|
||||
|
||||
public void setAuthRegex(String authRegex)
|
||||
{
|
||||
this.authRegex = authRegex;
|
||||
}
|
||||
|
||||
public List<AuthorizationScope> getAuthorizationScopeList()
|
||||
{
|
||||
return authorizationScopeList;
|
||||
}
|
||||
|
||||
public void setAuthorizationScopeList(List<AuthorizationScope> authorizationScopeList)
|
||||
{
|
||||
this.authorizationScopeList = authorizationScopeList;
|
||||
}
|
||||
|
||||
public List<String> getTokenUrlList()
|
||||
{
|
||||
return tokenUrlList;
|
||||
}
|
||||
|
||||
public void setTokenUrlList(List<String> tokenUrlList)
|
||||
{
|
||||
this.tokenUrlList = tokenUrlList;
|
||||
}
|
||||
}
|
||||
|
||||
public static class AuthorizationScope
|
||||
{
|
||||
/**
|
||||
* 作用域名称
|
||||
*/
|
||||
private String scope = "";
|
||||
|
||||
/**
|
||||
* 作用域描述
|
||||
*/
|
||||
private String description = "";
|
||||
|
||||
public String getScope()
|
||||
{
|
||||
return scope;
|
||||
}
|
||||
|
||||
public void setScope(String scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public String getDescription()
|
||||
{
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description)
|
||||
{
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.ruoyi.common.swagger.config.SwaggerAutoConfiguration
|
Reference in New Issue
Block a user