¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.config; |
| | | |
| | | import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.config.BeanPostProcessor; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.context.annotation.Import; |
| | | import org.springframework.util.ReflectionUtils; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; |
| | | import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.ParameterBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | | import springfox.documentation.schema.ModelRef; |
| | | import springfox.documentation.service.*; |
| | | import springfox.documentation.spi.DocumentationType; |
| | | import springfox.documentation.spi.service.contexts.SecurityContext; |
| | | import springfox.documentation.spring.web.plugins.Docket; |
| | | import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; |
| | | import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; |
| | | import springfox.documentation.swagger2.annotations.EnableSwagger2; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Author scott |
| | | */ |
| | | @Configuration |
| | | @EnableSwagger2 //å¼å¯ Swagger2 |
| | | @EnableKnife4j //å¼å¯ knife4jï¼å¯ä»¥ä¸å |
| | | @Import(BeanValidatorPluginsConfiguration.class) |
| | | public class Swagger2Config implements WebMvcConfigurer { |
| | | |
| | | /** |
| | | * |
| | | * æ¾ç¤ºswagger-ui.htmlææ¡£å±ç¤ºé¡µï¼è¿å¿
须注å
¥swaggerèµæºï¼ |
| | | * |
| | | * @param registry |
| | | */ |
| | | @Override |
| | | public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| | | registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); |
| | | registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); |
| | | registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); |
| | | } |
| | | |
| | | /** |
| | | * swagger2çé
ç½®æä»¶ï¼è¿éå¯ä»¥é
ç½®swagger2çä¸äºåºæ¬çå
å®¹ï¼æ¯å¦æ«æçå
çç |
| | | * |
| | | * @return Docket |
| | | */ |
| | | @Bean(value = "defaultApi2") |
| | | public Docket defaultApi2() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("default"); |
| | | } |
| | | |
| | | /** |
| | | * swagger2çé
ç½®æä»¶ï¼è¿éå¯ä»¥é
ç½®swagger2çä¸äºåºæ¬çå
å®¹ï¼æ¯å¦æ«æçå
çç ï¼MDCï¼ |
| | | * |
| | | * @return Docket |
| | | */ |
| | | @Bean(value = "defaultApi") |
| | | public Docket defaultApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules.mdc")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("MDC"); |
| | | } |
| | | |
| | | /** |
| | | * swagger2çé
ç½®æä»¶ï¼è¿éå¯ä»¥é
ç½®swagger2çä¸äºåºæ¬çå
å®¹ï¼æ¯å¦æ«æçå
çç ï¼DNCï¼ |
| | | * |
| | | * @return Docket |
| | | */ |
| | | @Bean(value = "defaultApiDnc") |
| | | public Docket defaultApi3() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules.dnc")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("DNC"); |
| | | } |
| | | |
| | | @Bean(value = "defaultApiAct") |
| | | public Docket activitiApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules.flowable")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("Flowable模å"); |
| | | } |
| | | |
| | | @Bean(value = "defaultApiMsi") |
| | | public Docket activitiApiMsi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules.msi")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("éæ"); |
| | | } |
| | | |
| | | @Bean(value = "defaultApiEam") |
| | | public Docket activitiApiEam() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules.eam")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("eam"); |
| | | } |
| | | |
| | | @Bean(value = "defaultApiBoard") |
| | | public Docket activitiApiBoard() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //æ¤å
è·¯å¾ä¸çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules.board")) |
| | | //å äºApiOperation注解çç±»ï¼æçææ¥å£ææ¡£ |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .securitySchemes(Collections.singletonList(securityScheme())) |
| | | .securityContexts(securityContexts()) |
| | | .globalOperationParameters(setHeaderToken()) |
| | | .groupName("æ°ååªççæ¿"); |
| | | } |
| | | |
| | | /*** |
| | | * oauth2é
ç½® |
| | | * éè¦å¢å swaggerææåè°å°å |
| | | * http://localhost:8888/webjars/springfox-swagger-ui/o2c.html |
| | | * @return |
| | | */ |
| | | @Bean |
| | | SecurityScheme securityScheme() { |
| | | return new ApiKey(CommonConstant.X_ACCESS_TOKEN, CommonConstant.X_ACCESS_TOKEN, "header"); |
| | | } |
| | | /** |
| | | * JWT token |
| | | * @return |
| | | */ |
| | | private List<Parameter> setHeaderToken() { |
| | | ParameterBuilder tokenPar = new ParameterBuilder(); |
| | | List<Parameter> pars = new ArrayList<>(); |
| | | tokenPar.name(CommonConstant.X_ACCESS_TOKEN).description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); |
| | | pars.add(tokenPar.build()); |
| | | return pars; |
| | | } |
| | | |
| | | /** |
| | | * apiææ¡£ç详ç»ä¿¡æ¯å½æ°,注æè¿éçæ³¨è§£å¼ç¨çæ¯åªä¸ª |
| | | * |
| | | * @return |
| | | */ |
| | | private ApiInfo apiInfo() { |
| | | return new ApiInfoBuilder() |
| | | // //大æ é¢ |
| | | .title("JeecgBoot åå°æå¡APIæ¥å£ææ¡£") |
| | | // çæ¬å· |
| | | .version("1.0") |
| | | // .termsOfServiceUrl("NO terms of service") |
| | | // æè¿° |
| | | .description("åå°APIæ¥å£") |
| | | // ä½è
|
| | | .contact(new Contact("å京å½ç¬ä¿¡æ¯ææ¯æéå
¬å¸","www.jeccg.com","jeecgos@163.com")) |
| | | .license("The Apache License, Version 2.0") |
| | | .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") |
| | | .build(); |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢ securityContexts ä¿æç»å½ç¶æ |
| | | */ |
| | | private List<SecurityContext> securityContexts() { |
| | | return new ArrayList( |
| | | Collections.singleton(SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .forPaths(PathSelectors.regex("^(?!auth).*$")) |
| | | .build()) |
| | | ); |
| | | } |
| | | |
| | | private List<SecurityReference> defaultAuth() { |
| | | AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| | | AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| | | authorizationScopes[0] = authorizationScope; |
| | | return new ArrayList( |
| | | Collections.singleton(new SecurityReference(CommonConstant.X_ACCESS_TOKEN, authorizationScopes))); |
| | | } |
| | | |
| | | /** |
| | | * è§£å³springboot2.6 åspringfoxä¸å
¼å®¹é®é¢ |
| | | * @return |
| | | */ |
| | | @Bean |
| | | public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { |
| | | return new BeanPostProcessor() { |
| | | |
| | | @Override |
| | | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| | | if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { |
| | | customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); |
| | | } |
| | | return bean; |
| | | } |
| | | |
| | | private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) { |
| | | List<T> copy = mappings.stream() |
| | | .filter(mapping -> mapping.getPatternParser() == null) |
| | | .collect(Collectors.toList()); |
| | | mappings.clear(); |
| | | mappings.addAll(copy); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) { |
| | | try { |
| | | Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); |
| | | field.setAccessible(true); |
| | | return (List<RequestMappingInfoHandlerMapping>) field.get(bean); |
| | | } catch (IllegalArgumentException | IllegalAccessException e) { |
| | | throw new IllegalStateException(e); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | |
| | | } |