效果
实现
SwaggerAutoConfiguration里新增配置:
package com.fxkj.common.config; import com.google.common.base.Predicates; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.*; 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; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; /** * @author Jackson */ @Profile({"default", "dev", "test"}) @Configuration @EnableConfigurationProperties({SwaggerProperties.class}) @ConditionalOnProperty(prefix = "swagger",value = "enable") @EnableSwagger2 public class SwaggerAutoConfiguration { @Resource private SecurityConfig securityConfig; @Bean public Docket swaggerSpringMvcPlugin(@Autowired SwaggerProperties swaggerConfigProps) { return new Docket(DocumentationType.SWAGGER_2) .enable(swaggerConfigProps.isEnable()) .useDefaultResponseMessages(false) .apiInfo(apiInfo(swaggerConfigProps)) .securitySchemes(getSecuritySchemes()) .securityContexts(getSecurityContexts()) .select() .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build(); } private ApiInfo apiInfo(SwaggerProperties properties) { return new ApiInfoBuilder() .title(properties.getTitle()) .version(properties.getVersion()) .description(properties.getDescription()) .contact(new Contact(properties.getContactName(), properties.getContactUrl(), properties.getContactEmail())) .license("Apache License Version 2.0") .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE") .version("2.0") .build(); } @Bean public Docket api_system(@Autowired SwaggerProperties swaggerConfigProps) { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfoSystem(swaggerConfigProps)) .securitySchemes(getSecuritySchemes()) .securityContexts(getSecurityContexts()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.ant("/system/**")) .build() .groupName("系统设置") .pathMapping("/"); } @Bean public Docket api_role(@Autowired SwaggerProperties swaggerConfigProps) { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfoSystem(swaggerConfigProps)) .securitySchemes(getSecuritySchemes()) .securityContexts(getSecurityContexts()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.ant("/roles/**")) .build() .groupName("角色设置") .pathMapping("/"); } @Bean public Docket api_dict(@Autowired SwaggerProperties swaggerConfigProps) { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfoSystem(swaggerConfigProps)) .securitySchemes(getSecuritySchemes()) .securityContexts(getSecurityContexts()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.ant("/dict/**")) .build() .groupName("字典维护") .pathMapping("/"); } private ApiInfo apiInfoSystem(SwaggerProperties properties) { return new ApiInfoBuilder() .title("系统设置相关API") .version(properties.getVersion()) .description(properties.getDescription()) .contact(new Contact(properties.getContactName(), properties.getContactUrl(), properties.getContactEmail())) .license("Apache License Version 2.0") .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE") .version("2.0") .build(); } private ArrayList<ApiKey> getSecuritySchemes() { return Lists.newArrayList( new ApiKey("token", "token", "header") ); } private List<SecurityContext> getSecurityContexts() { AuthorizationScope[] scopes = { new AuthorizationScope("global", "accessEverything") }; List<SecurityReference> securityReferences = Lists.newArrayList( new SecurityReference("token", scopes) ); return Lists.newArrayList(SecurityContext.builder() .securityReferences(securityReferences) .forPaths(Predicates.not(Predicates.in(securityConfig.getIgnoreUrls()))) .build()); } }