| | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Configuration |
| | | @EnableSwagger2 // 开启Swagger2 |
| | | @EnableSwagger2 |
| | | public class Swagger2Config { |
| | | |
| | | /** |
| | | * 配置Swagger2核心对象Docket |
| | | */ |
| | | @Bean |
| | | public Docket createRestApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | // 配置API文档基本信息 |
| | | .apiInfo(apiInfo()) |
| | | // 选择需要生成文档的接口 |
| | | .select() |
| | | // 扫描指定包下的接口(替换为你的Controller包路径) |
| | | .apis(RequestHandlerSelectors.basePackage("com.linghu.controller")) |
| | | // 匹配所有路径 |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | // 添加安全方案配置 |
| | | .securitySchemes(Arrays.asList(securityScheme())) |
| | | .securityContexts(Arrays.asList(securityContext())) |
| | | // 添加全局参数(header中的token参数) |
| | | .globalOperationParameters(globalParameters()); |
| | | // 重点修改:使用 securitySchemes + securityContext 替代全局参数 |
| | | .securitySchemes(Arrays.asList(apiKey())) // 添加安全方案 |
| | | .securityContexts(Arrays.asList(securityContext())); // 应用安全上下文 |
| | | } |
| | | |
| | | /** |
| | | * 配置JWT安全方案 |
| | | */ |
| | | private ApiKey securityScheme() { |
| | | // 1. 定义安全方案(在Swagger UI顶部添加Authorize按钮) |
| | | private ApiKey apiKey() { |
| | | return new ApiKey("BearerToken", "Authorization", "header"); |
| | | } |
| | | |
| | | /** |
| | | * 配置安全上下文 |
| | | */ |
| | | // 2. 配置安全上下文(全局生效) |
| | | private SecurityContext securityContext() { |
| | | return SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .forPaths(PathSelectors.any()) |
| | | .forPaths(PathSelectors.any()) // 对所有路径生效 |
| | | .build(); |
| | | } |
| | | |
| | | /** |
| | | * 默认的安全引用 |
| | | */ |
| | | // 3. 设置默认授权范围 |
| | | 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文档基本信息(标题、描述、作者等) |
| | | */ |
| | | //http://localhost:8080/swagger-ui.html |
| | | 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(); |
| | | } |
| | | } |