mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	fix 修复swagger开关无法控制关闭问题
This commit is contained in:
		
							
								
								
									
										21
									
								
								.editorconfig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								.editorconfig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
# http://editorconfig.org
 | 
			
		||||
root = true
 | 
			
		||||
 | 
			
		||||
# 空格替代Tab缩进在各种编辑工具下效果一致
 | 
			
		||||
[*]
 | 
			
		||||
indent_style = space
 | 
			
		||||
indent_size = 4
 | 
			
		||||
charset = utf-8
 | 
			
		||||
end_of_line = lf
 | 
			
		||||
trim_trailing_whitespace = true
 | 
			
		||||
insert_final_newline = true
 | 
			
		||||
 | 
			
		||||
[*.java]
 | 
			
		||||
indent_style = tab
 | 
			
		||||
 | 
			
		||||
[*.{json,yml}]
 | 
			
		||||
indent_size = 2
 | 
			
		||||
 | 
			
		||||
[*.md]
 | 
			
		||||
insert_final_newline = false
 | 
			
		||||
trim_trailing_whitespace = false
 | 
			
		||||
@@ -193,6 +193,11 @@ swagger:
 | 
			
		||||
  description: '描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...'
 | 
			
		||||
  # 版本
 | 
			
		||||
  version: '版本号: ${ruoyi-vue-plus.version}'
 | 
			
		||||
  # 作者信息
 | 
			
		||||
  contact:
 | 
			
		||||
    name: Lion Li
 | 
			
		||||
    email: crazylionli@163.com
 | 
			
		||||
    url: https://gitee.com/JavaLionLi/RuoYi-Vue-Plus
 | 
			
		||||
 | 
			
		||||
# 防止XSS攻击
 | 
			
		||||
xss:
 | 
			
		||||
@@ -295,4 +300,4 @@ management:
 | 
			
		||||
        include: '*'
 | 
			
		||||
  endpoint:
 | 
			
		||||
    logfile:
 | 
			
		||||
      external-file: ./logs/sys-console.log
 | 
			
		||||
      external-file: ./logs/sys-console.log
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,9 @@
 | 
			
		||||
package com.ruoyi.framework.config;
 | 
			
		||||
 | 
			
		||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
 | 
			
		||||
import com.ruoyi.common.config.RuoYiConfig;
 | 
			
		||||
import com.ruoyi.framework.config.properties.SwaggerProperties;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import springfox.documentation.builders.ApiInfoBuilder;
 | 
			
		||||
@@ -29,103 +26,83 @@ import java.util.List;
 | 
			
		||||
@Configuration
 | 
			
		||||
@EnableSwagger2
 | 
			
		||||
@EnableKnife4j
 | 
			
		||||
@ConditionalOnClass({Docket.class, ApiInfoBuilder.class})
 | 
			
		||||
@ConditionalOnProperty(prefix = "swagger", value = "enable", matchIfMissing = true)
 | 
			
		||||
public class SwaggerConfig {
 | 
			
		||||
    /**
 | 
			
		||||
     * 系统基础配置
 | 
			
		||||
     */
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RuoYiConfig ruoyiConfig;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 标题
 | 
			
		||||
     */
 | 
			
		||||
    @Value("${swagger.title}")
 | 
			
		||||
    private String title;
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private SwaggerProperties swaggerProperties;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 描述
 | 
			
		||||
     */
 | 
			
		||||
    @Value("${swagger.description}")
 | 
			
		||||
    private String description;
 | 
			
		||||
	/**
 | 
			
		||||
	 * 创建API
 | 
			
		||||
	 */
 | 
			
		||||
	@Bean
 | 
			
		||||
	public Docket createRestApi() {
 | 
			
		||||
		return new Docket(DocumentationType.SWAGGER_2)
 | 
			
		||||
			.enable(swaggerProperties.getEnabled())
 | 
			
		||||
			// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
 | 
			
		||||
			.apiInfo(apiInfo())
 | 
			
		||||
			// 设置哪些接口暴露给Swagger展示
 | 
			
		||||
			.select()
 | 
			
		||||
			// 扫描所有有注解的api,用这种方式更灵活
 | 
			
		||||
			.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
 | 
			
		||||
			// 扫描指定包中的swagger注解
 | 
			
		||||
			// .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger"))
 | 
			
		||||
			// 扫描所有 .apis(RequestHandlerSelectors.any())
 | 
			
		||||
			.paths(PathSelectors.any())
 | 
			
		||||
			.build()
 | 
			
		||||
			/* 设置安全模式,swagger可以设置访问token */
 | 
			
		||||
			.securitySchemes(securitySchemes())
 | 
			
		||||
			.securityContexts(securityContexts());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 版本
 | 
			
		||||
     */
 | 
			
		||||
    @Value("${swagger.version}")
 | 
			
		||||
    private String version;
 | 
			
		||||
	/**
 | 
			
		||||
	 * 安全模式,这里指定token通过Authorization头请求头传递
 | 
			
		||||
	 */
 | 
			
		||||
	private List<SecurityScheme> securitySchemes() {
 | 
			
		||||
		List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
 | 
			
		||||
		apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
 | 
			
		||||
		return apiKeyList;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 创建API
 | 
			
		||||
     */
 | 
			
		||||
    @Bean
 | 
			
		||||
    public Docket createRestApi() {
 | 
			
		||||
        return new Docket(DocumentationType.SWAGGER_2)
 | 
			
		||||
                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
 | 
			
		||||
                .apiInfo(apiInfo())
 | 
			
		||||
                // 设置哪些接口暴露给Swagger展示
 | 
			
		||||
                .select()
 | 
			
		||||
                // 扫描所有有注解的api,用这种方式更灵活
 | 
			
		||||
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
 | 
			
		||||
                // 扫描指定包中的swagger注解
 | 
			
		||||
                // .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger"))
 | 
			
		||||
                // 扫描所有 .apis(RequestHandlerSelectors.any())
 | 
			
		||||
                .paths(PathSelectors.any())
 | 
			
		||||
                .build()
 | 
			
		||||
                /* 设置安全模式,swagger可以设置访问token */
 | 
			
		||||
                .securitySchemes(securitySchemes())
 | 
			
		||||
                .securityContexts(securityContexts());
 | 
			
		||||
    }
 | 
			
		||||
	/**
 | 
			
		||||
	 * 安全上下文
 | 
			
		||||
	 */
 | 
			
		||||
	private List<SecurityContext> securityContexts() {
 | 
			
		||||
		List<SecurityContext> securityContexts = new ArrayList<>();
 | 
			
		||||
		securityContexts.add(
 | 
			
		||||
			SecurityContext.builder()
 | 
			
		||||
				.securityReferences(defaultAuth())
 | 
			
		||||
				.forPaths(PathSelectors.regex("^(?!auth).*$"))
 | 
			
		||||
				.build());
 | 
			
		||||
		return securityContexts;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 安全模式,这里指定token通过Authorization头请求头传递
 | 
			
		||||
     */
 | 
			
		||||
    private List<SecurityScheme> securitySchemes() {
 | 
			
		||||
        List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
 | 
			
		||||
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
 | 
			
		||||
        return apiKeyList;
 | 
			
		||||
    }
 | 
			
		||||
	/**
 | 
			
		||||
	 * 默认的安全上引用
 | 
			
		||||
	 */
 | 
			
		||||
	private List<SecurityReference> defaultAuth() {
 | 
			
		||||
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
 | 
			
		||||
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
 | 
			
		||||
		authorizationScopes[0] = authorizationScope;
 | 
			
		||||
		List<SecurityReference> securityReferences = new ArrayList<>();
 | 
			
		||||
		securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
 | 
			
		||||
		return securityReferences;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 安全上下文
 | 
			
		||||
     */
 | 
			
		||||
    private List<SecurityContext> securityContexts() {
 | 
			
		||||
        List<SecurityContext> securityContexts = new ArrayList<>();
 | 
			
		||||
        securityContexts.add(
 | 
			
		||||
                SecurityContext.builder()
 | 
			
		||||
                        .securityReferences(defaultAuth())
 | 
			
		||||
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
 | 
			
		||||
                        .build());
 | 
			
		||||
        return securityContexts;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 默认的安全上引用
 | 
			
		||||
     */
 | 
			
		||||
    private List<SecurityReference> defaultAuth() {
 | 
			
		||||
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
 | 
			
		||||
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
 | 
			
		||||
        authorizationScopes[0] = authorizationScope;
 | 
			
		||||
        List<SecurityReference> securityReferences = new ArrayList<>();
 | 
			
		||||
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
 | 
			
		||||
        return securityReferences;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 添加摘要信息
 | 
			
		||||
     */
 | 
			
		||||
    private ApiInfo apiInfo() {
 | 
			
		||||
        // 用ApiInfoBuilder进行定制
 | 
			
		||||
        return new ApiInfoBuilder()
 | 
			
		||||
                // 设置标题
 | 
			
		||||
                .title(title)
 | 
			
		||||
                // 描述
 | 
			
		||||
                .description(description)
 | 
			
		||||
                // 作者信息
 | 
			
		||||
                .contact(new Contact(ruoyiConfig.getName(), null, null))
 | 
			
		||||
                // 版本
 | 
			
		||||
                .version(version)
 | 
			
		||||
                .build();
 | 
			
		||||
    }
 | 
			
		||||
	/**
 | 
			
		||||
	 * 添加摘要信息
 | 
			
		||||
	 */
 | 
			
		||||
	private ApiInfo apiInfo() {
 | 
			
		||||
		// 用ApiInfoBuilder进行定制
 | 
			
		||||
		SwaggerProperties.Contact contact = swaggerProperties.getContact();
 | 
			
		||||
		return new ApiInfoBuilder()
 | 
			
		||||
			// 设置标题
 | 
			
		||||
			.title(swaggerProperties.getTitle())
 | 
			
		||||
			// 描述
 | 
			
		||||
			.description(swaggerProperties.getDescription())
 | 
			
		||||
			// 作者信息
 | 
			
		||||
			.contact(new Contact(contact.getName(), contact.getUrl(), contact.getEmail()))
 | 
			
		||||
			// 版本
 | 
			
		||||
			.version(swaggerProperties.getVersion())
 | 
			
		||||
			.build();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ package com.ruoyi.framework.config.properties;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import lombok.experimental.Accessors;
 | 
			
		||||
import org.springframework.boot.context.properties.ConfigurationProperties;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
@@ -33,4 +32,28 @@ public class SwaggerProperties {
 | 
			
		||||
     */
 | 
			
		||||
    private String version;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 联系方式
 | 
			
		||||
	 */
 | 
			
		||||
    private Contact contact;
 | 
			
		||||
 | 
			
		||||
    @Data
 | 
			
		||||
	@NoArgsConstructor
 | 
			
		||||
	public static class Contact{
 | 
			
		||||
 | 
			
		||||
		/**
 | 
			
		||||
		 * 联系人
 | 
			
		||||
		 **/
 | 
			
		||||
		private String name;
 | 
			
		||||
		/**
 | 
			
		||||
		 * 联系人url
 | 
			
		||||
		 **/
 | 
			
		||||
		private String url;
 | 
			
		||||
		/**
 | 
			
		||||
		 * 联系人email
 | 
			
		||||
		 **/
 | 
			
		||||
		private String email;
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user