¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.yuanchu.limslaboratory.config; |
| | | |
| | | |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | | import springfox.documentation.oas.annotations.EnableOpenApi; |
| | | import springfox.documentation.service.*; |
| | | import springfox.documentation.spi.DocumentationType; |
| | | import springfox.documentation.spi.service.contexts.SecurityContext; |
| | | import springfox.documentation.spring.web.plugins.Docket; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | @Configuration |
| | | @EnableOpenApi |
| | | public class Swagger3 { |
| | | // è·¯å¾http://localhost:8080/doc.html |
| | | @Value("${swagger.enabled}") |
| | | private boolean enable; |
| | | |
| | | private final String swaggerPackage = "com.yuanchu.limslaboratory"; |
| | | |
| | | /** |
| | | * æ·»å æè¦ä¿¡æ¯ |
| | | */ |
| | | private ApiInfo apiInfo() { |
| | | // ç¨ApiInfoBuilderè¿è¡å®å¶ |
| | | return new ApiInfoBuilder() |
| | | // 设置æ é¢ |
| | | .title("LIMS管çç³»ç»") |
| | | // æè¿° |
| | | .description("LIMS管çç³»ç»") |
| | | // ä½è
ä¿¡æ¯ |
| | | .contact(new Contact("Crunchy", null, null)) |
| | | // çæ¬ |
| | | .version("çæ¬å·:V.1") |
| | | //åè®® |
| | | .license("The Apache License") |
| | | //åè®®url |
| | | .licenseUrl("http://www.baidu.com") |
| | | .build(); |
| | | } |
| | | |
| | | /** |
| | | * å建API |
| | | * http:IP:端å£å·/swagger-ui/index.html åçå°å |
| | | * http:IP:端å£å·/doc.html bootStrap-UIå°å |
| | | */ |
| | | @Bean |
| | | public Docket createRestApi() { |
| | | return new Docket(DocumentationType.OAS_30).pathMapping("/") |
| | | // ç¨æ¥å建该APIçåºæ¬ä¿¡æ¯ï¼å±ç¤ºå¨ææ¡£ç页é¢ä¸ï¼èªå®ä¹å±ç¤ºçä¿¡æ¯ï¼ |
| | | .enable(enable) |
| | | .apiInfo(apiInfo()) |
| | | // 设置åªäºæ¥å£æ´é²ç»Swaggerå±ç¤º |
| | | .select() |
| | | // æ«æææææ³¨è§£çapiï¼ç¨è¿ç§æ¹å¼æ´çµæ´» |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | |
| | | |
| | | // æ«ææå®å
ä¸çswagger注解 |
| | | .apis(RequestHandlerSelectors.basePackage(swaggerPackage)) |
| | | // æ«æææ .apis(RequestHandlerSelectors.any()) |
| | | .paths(PathSelectors.regex("(?!/ApiError.*).*")) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | // æ¯æçé讯åè®®éå |
| | | .protocols(newHashSet("https", "http")) |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | |
| | | /** |
| | | * æ¯æçé讯åè®®éå |
| | | * @param type1 |
| | | * @param type2 |
| | | * @return |
| | | */ |
| | | private Set<String> newHashSet(String type1, String type2){ |
| | | Set<String> set = new HashSet<>(); |
| | | set.add(type1); |
| | | set.add(type2); |
| | | return set; |
| | | } |
| | | |
| | | /** |
| | | * 认è¯çå®å
¨ä¸ä¸æ |
| | | */ |
| | | private List<SecurityScheme> securitySchemes() { |
| | | List<SecurityScheme> securitySchemes = new ArrayList<>(); |
| | | securitySchemes.add((SecurityScheme) new ApiKey("token", "token", "header")); |
| | | return securitySchemes; |
| | | } |
| | | |
| | | /** |
| | | * ææä¿¡æ¯å
¨å±åºç¨ |
| | | */ |
| | | private List<SecurityContext> securityContexts() { |
| | | List<SecurityContext> securityContexts = new ArrayList<>(); |
| | | securityContexts.add(SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .forPaths(PathSelectors.any()).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; |
| | | } |
| | | } |