¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.web.core.config; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import com.ruoyi.common.config.RuoYiConfig; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | | import springfox.documentation.service.ApiInfo; |
| | | import springfox.documentation.service.ApiKey; |
| | | import springfox.documentation.service.AuthorizationScope; |
| | | import springfox.documentation.service.Contact; |
| | | 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; |
| | | |
| | | /** |
| | | * Swagger2çæ¥å£é
ç½® |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Configuration |
| | | @EnableSwagger2 |
| | | public class SwaggerConfig |
| | | { |
| | | /** ç³»ç»åºç¡é
ç½® */ |
| | | @Autowired |
| | | private RuoYiConfig ruoyiConfig; |
| | | |
| | | /** æ¯å¦å¼å¯swagger */ |
| | | @Value("${swagger.enabled}") |
| | | private boolean enabled; |
| | | |
| | | /** 设置请æ±çç»ä¸åç¼ */ |
| | | @Value("${swagger.pathMapping}") |
| | | private String pathMapping; |
| | | |
| | | /** |
| | | * å建API |
| | | */ |
| | | @Bean |
| | | public Docket createRestApi() |
| | | { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | // æ¯å¦å¯ç¨Swagger |
| | | .enable(enabled) |
| | | // ç¨æ¥å建该APIçåºæ¬ä¿¡æ¯ï¼å±ç¤ºå¨ææ¡£ç页é¢ä¸ï¼èªå®ä¹å±ç¤ºçä¿¡æ¯ï¼ |
| | | .apiInfo(apiInfo()) |
| | | // 设置åªäºæ¥å£æ´é²ç»Swaggerå±ç¤º |
| | | .select() |
| | | // æ«æææææ³¨è§£çapiï¼ç¨è¿ç§æ¹å¼æ´çµæ´» |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | // æ«ææå®å
ä¸çswagger注解 |
| | | // .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger")) |
| | | // æ«æææ .apis(RequestHandlerSelectors.any()) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置å®å
¨æ¨¡å¼ï¼swaggerå¯ä»¥è®¾ç½®è®¿é®token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()) |
| | | .pathMapping(pathMapping); |
| | | } |
| | | |
| | | /** |
| | | * å®å
¨æ¨¡å¼ï¼è¿éæå®tokenéè¿Authorization头请æ±å¤´ä¼ é |
| | | */ |
| | | private List<ApiKey> securitySchemes() |
| | | { |
| | | List<ApiKey> apiKeyList = new ArrayList<ApiKey>(); |
| | | apiKeyList.add(new ApiKey("Authorization", "Authorization", "header")); |
| | | return apiKeyList; |
| | | } |
| | | |
| | | /** |
| | | * å®å
¨ä¸ä¸æ |
| | | */ |
| | | private List<SecurityContext> securityContexts() |
| | | { |
| | | List<SecurityContext> securityContexts = new ArrayList<>(); |
| | | securityContexts.add( |
| | | SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .forPaths(PathSelectors.regex("^(?!auth).*$")) |
| | | .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; |
| | | } |
| | | |
| | | /** |
| | | * æ·»å æè¦ä¿¡æ¯ |
| | | */ |
| | | private ApiInfo apiInfo() |
| | | { |
| | | // ç¨ApiInfoBuilderè¿è¡å®å¶ |
| | | return new ApiInfoBuilder() |
| | | // 设置æ é¢ |
| | | .title("æ é¢ï¼è¥ä¾ç®¡çç³»ç»_æ¥å£ææ¡£") |
| | | // æè¿° |
| | | .description("æè¿°ï¼ç¨äºç®¡çé墿ä¸å
¬å¸ç人åä¿¡æ¯,å
·ä½å
æ¬XXX,XXX模å...") |
| | | // ä½è
ä¿¡æ¯ |
| | | .contact(new Contact(ruoyiConfig.getName(), null, null)) |
| | | // çæ¬ |
| | | .version("çæ¬å·:" + ruoyiConfig.getVersion()) |
| | | .build(); |
| | | } |
| | | } |