| | |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | | import springfox.documentation.builders.ParameterBuilder; |
| | | import springfox.documentation.schema.ModelRef; |
| | | import springfox.documentation.service.ApiInfo; |
| | | import springfox.documentation.service.ApiKey; |
| | | import springfox.documentation.service.AuthorizationScope; |
| | | import springfox.documentation.service.Contact; |
| | | import springfox.documentation.service.Parameter; |
| | | 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; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Configuration |
| | | @EnableSwagger2 // 开启Swagger2 |
| | | @EnableSwagger2 // 开启Swagger2 |
| | | public class Swagger2Config { |
| | | |
| | | /** |
| | |
| | | .apis(RequestHandlerSelectors.basePackage("com.linghu.controller")) |
| | | // 匹配所有路径 |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | // 添加安全方案配置 |
| | | .securitySchemes(Arrays.asList(securityScheme())) |
| | | .securityContexts(Arrays.asList(securityContext())) |
| | | // 添加全局参数(header中的token参数) |
| | | .globalOperationParameters(globalParameters()); |
| | | } |
| | | |
| | | /** |
| | | * 配置JWT安全方案 |
| | | */ |
| | | private ApiKey securityScheme() { |
| | | return new ApiKey("BearerToken", "Authorization", "header"); |
| | | } |
| | | |
| | | /** |
| | | * 配置安全上下文 |
| | | */ |
| | | private SecurityContext securityContext() { |
| | | return SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .forPaths(PathSelectors.any()) |
| | | .build(); |
| | | } |
| | | |
| | | /** |
| | | * 默认的安全引用 |
| | | */ |
| | | private List<SecurityReference> defaultAuth() { |
| | | AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| | | AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| | | authorizationScopes[0] = authorizationScope; |
| | | return Arrays.asList(new SecurityReference("BearerToken", authorizationScopes)); |
| | | } |
| | | |
| | | /** |
| | | * 配置全局参数 |
| | | */ |
| | | private List<Parameter> globalParameters() { |
| | | List<Parameter> parameters = new ArrayList<>(); |
| | | parameters.add(new ParameterBuilder() |
| | | .name("Authorization") |
| | | .description("JWT Token (格式: Bearer [token])") |
| | | .modelRef(new ModelRef("string")) |
| | | .parameterType("header") |
| | | .required(false) |
| | | .build()); |
| | | return parameters; |
| | | } |
| | | |
| | | /** |
| | | * http://127.0.0.1:8080/swagger-ui.html |
| | | * 配置API文档基本信息(标题、描述、作者等) |
| | | */ |
| | | private ApiInfo apiInfo() { |
| | | return new ApiInfoBuilder() |
| | | .title("灵狐GEO系统 接口文档") // 文档标题 |
| | | .description("使用 Swagger2 生成的API文档") // 文档描述 |
| | | .version("1.0.0") // 版本号 |
| | | |
| | | .title("灵狐GEO系统 接口文档") // 文档标题 |
| | | .description("使用 Swagger2 生成的API文档") // 文档描述 |
| | | .version("1.0.0") // 版本号 |
| | | .build(); |
| | | } |
| | | } |