|
| 1 | + |
| 2 | +package com.github.lybgeek.config; |
| 3 | + |
| 4 | +import com.google.common.collect.Lists; |
| 5 | +import io.swagger.annotations.ApiOperation; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import springfox.documentation.builders.ApiInfoBuilder; |
| 10 | +import springfox.documentation.builders.ParameterBuilder; |
| 11 | +import springfox.documentation.builders.PathSelectors; |
| 12 | +import springfox.documentation.builders.RequestHandlerSelectors; |
| 13 | +import springfox.documentation.schema.ModelRef; |
| 14 | +import springfox.documentation.service.*; |
| 15 | +import springfox.documentation.spi.DocumentationType; |
| 16 | +import springfox.documentation.spi.service.contexts.SecurityContext; |
| 17 | +import springfox.documentation.spring.web.plugins.Docket; |
| 18 | +import springfox.documentation.swagger2.annotations.EnableSwagger2; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | + |
| 24 | +@Configuration |
| 25 | +@EnableSwagger2 |
| 26 | +public class SwaggerConfig { |
| 27 | + |
| 28 | + |
| 29 | + /** 是否开启swagger */ |
| 30 | + @Value("${swagger.enabled:true}") |
| 31 | + private boolean enabled; |
| 32 | + |
| 33 | + /** 设置请求的统一前缀 */ |
| 34 | + @Value("${swagger.pathMapping:/}") |
| 35 | + private String pathMapping; |
| 36 | + |
| 37 | + /** 是否需要过滤token处理 **/ |
| 38 | + @Value("${swagger.skipToken.enabled:false}") |
| 39 | + private Boolean skipTokenEnabled; |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + @Bean |
| 46 | + public Docket groupRestApi() { |
| 47 | + buildHeaderParams(); |
| 48 | + return new Docket(DocumentationType.SWAGGER_2) |
| 49 | + // 是否启用Swagger |
| 50 | + .enable(enabled) |
| 51 | + // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) |
| 52 | + .apiInfo(groupApiInfo()) |
| 53 | + // 设置哪些接口暴露给Swagger展示 |
| 54 | + .select() |
| 55 | + // 扫描所有有注解的api,用这种方式更灵活 |
| 56 | + .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| 57 | + // 扫描指定包中的swagger注解 |
| 58 | + // .apis(RequestHandlerSelectors.basePackage("com.github.lybgeek")) |
| 59 | + // 扫描所有 .apis(RequestHandlerSelectors.any()) |
| 60 | + .paths(PathSelectors.any()) |
| 61 | + .build() |
| 62 | + // 设置安全模式,swagger可以设置访问token |
| 63 | + .securityContexts(Lists.newArrayList(securityContext(),securityContext1())) |
| 64 | + .securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(),apiKey1())) |
| 65 | + .pathMapping(pathMapping) |
| 66 | + .globalOperationParameters(buildHeaderParams()); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 设置请求头信息 |
| 72 | + * @return |
| 73 | + */ |
| 74 | + private List<Parameter> buildHeaderParams() { |
| 75 | + List<Parameter> parameters = new ArrayList<>(); |
| 76 | + ParameterBuilder skipTokenAuthorize = new ParameterBuilder(); |
| 77 | + if (skipTokenEnabled) { |
| 78 | + skipTokenAuthorize.name("skipTokenAuthorize").description("跳过token认证").modelRef(new ModelRef("string")).parameterType("header").defaultValue("true").required(false).build(); |
| 79 | + parameters.add(skipTokenAuthorize.build()); |
| 80 | + } |
| 81 | + return parameters; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * 添加摘要信息 |
| 86 | + */ |
| 87 | + private ApiInfo groupApiInfo(){ |
| 88 | + return new ApiInfoBuilder() |
| 89 | + .title("springboot-xxl-job-executor") |
| 90 | + .description("<div style='font-size:14px;color:red;'>SPRINGBOOT-XXL-JOB-EXECUTOR RESTful APIs</div>") |
| 91 | + .termsOfServiceUrl("http://www.retail.com/") |
| 92 | + .version("1.0") |
| 93 | + .build(); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * 指定header key |
| 99 | + */ |
| 100 | + private ApiKey apiKey() { |
| 101 | + return new ApiKey("Authorization", "Authorization", "header"); |
| 102 | + } |
| 103 | + |
| 104 | + private ApiKey apiKey1() { |
| 105 | + return new ApiKey("serviceName", "serviceName", "header"); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * 安全上下文 |
| 110 | + */ |
| 111 | + private SecurityContext securityContext() { |
| 112 | + return SecurityContext.builder() |
| 113 | + .securityReferences(defaultAuth()) |
| 114 | + .forPaths(PathSelectors.regex("/.*")) |
| 115 | + .build(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * 安全模式,这里指定token通过Authorization头请求头传递 |
| 120 | + */ |
| 121 | + List<SecurityReference> defaultAuth() { |
| 122 | + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| 123 | + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| 124 | + authorizationScopes[0] = authorizationScope; |
| 125 | + return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes)); |
| 126 | + } |
| 127 | + |
| 128 | + private SecurityContext securityContext1() { |
| 129 | + return SecurityContext.builder() |
| 130 | + .securityReferences(defaultAuth1()) |
| 131 | + .forPaths(PathSelectors.regex("/.*")) |
| 132 | + .build(); |
| 133 | + } |
| 134 | + |
| 135 | + List<SecurityReference> defaultAuth1() { |
| 136 | + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| 137 | + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| 138 | + authorizationScopes[0] = authorizationScope; |
| 139 | + return Lists.newArrayList(new SecurityReference("serviceName", authorizationScopes)); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | +} |
0 commit comments