diff --git a/pig-common/pig-common-swagger/pom.xml b/pig-common/pig-common-swagger/pom.xml index c3ca566a..8af4ab1c 100644 --- a/pig-common/pig-common-swagger/pom.xml +++ b/pig-common/pig-common-swagger/pom.xml @@ -1,18 +1,21 @@ - + + + io.springfox + springfox-swagger-ui + ${swagger.fox.version} + io.springfox springfox-swagger2 ${swagger.fox.version} - + + io.springfox + springfox-oas + ${swagger.fox.version} + + + + org.springframework + spring-webflux + provided + + + + org.springframework.cloud + spring-cloud-gateway-core + provided + + + org.springframework.cloud + spring-cloud-commons + provided + + diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/IgnoreClientConfiguration.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/annotation/EnablePigSwagger2.java old mode 100755 new mode 100644 similarity index 51% rename from pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/IgnoreClientConfiguration.java rename to pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/annotation/EnablePigSwagger2.java index 8cef9912..54f06269 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/IgnoreClientConfiguration.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/annotation/EnablePigSwagger2.java @@ -14,29 +14,25 @@ * limitations under the License. */ -package com.pig4cloud.pig.gateway.config; +package com.pig4cloud.pig.common.swagger.annotation; -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.stereotype.Component; +import com.pig4cloud.pig.common.swagger.config.GatewaySwaggerAutoConfiguration; +import com.pig4cloud.pig.common.swagger.config.SwaggerAutoConfiguration; +import org.springframework.context.annotation.Import; +import springfox.documentation.swagger2.annotations.EnableSwagger2; -import java.util.ArrayList; -import java.util.List; +import java.lang.annotation.*; /** * @author lengleng - * @date 2019/2/1 放行参数配置 + * @date 2020/10/2 开启pig swagger */ -@Data -@Component -@RefreshScope -@ConfigurationProperties(prefix = "ignore") -public class IgnoreClientConfiguration { - - /** - * 放行终端配置,网关不校验此处的终端 - */ - private List clients = new ArrayList<>(); +@Target({ ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@EnableSwagger2 +@Import({ SwaggerAutoConfiguration.class, GatewaySwaggerAutoConfiguration.class }) +public @interface EnablePigSwagger2 { } diff --git a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/GatewaySwaggerAutoConfiguration.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/GatewaySwaggerAutoConfiguration.java new file mode 100644 index 00000000..63285d3f --- /dev/null +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/GatewaySwaggerAutoConfiguration.java @@ -0,0 +1,48 @@ +package com.pig4cloud.pig.common.swagger.config; + +import com.pig4cloud.pig.common.swagger.support.SwaggerResourceHandler; +import com.pig4cloud.pig.common.swagger.support.SwaggerSecurityHandler; +import com.pig4cloud.pig.common.swagger.support.SwaggerUiHandler; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.server.RequestPredicates; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.RouterFunctions; + +/** + * @author lengleng + * @date 2020/10/2 + *

+ * 网关swagger 配置类,仅在webflux 环境生效哦 + */ +@RequiredArgsConstructor +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +@ComponentScan("com.pig4cloud.pig.common.swagger.support") +public class GatewaySwaggerAutoConfiguration { + + private final SwaggerResourceHandler swaggerResourceHandler; + + private final SwaggerSecurityHandler swaggerSecurityHandler; + + private final SwaggerUiHandler swaggerUiHandler; + + @Bean + public WebFluxSwaggerConfiguration fluxSwaggerConfiguration() { + return new WebFluxSwaggerConfiguration(); + } + + @Bean + public RouterFunction swaggerRouterFunction() { + return RouterFunctions + .route(RequestPredicates.GET("/swagger-resources").and(RequestPredicates.accept(MediaType.ALL)), + swaggerResourceHandler) + .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler) + .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler); + } + +} diff --git a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/SwaggerAutoConfiguration.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerAutoConfiguration.java similarity index 52% rename from pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/SwaggerAutoConfiguration.java rename to pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerAutoConfiguration.java index 583a58de..32b3199c 100644 --- a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/SwaggerAutoConfiguration.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerAutoConfiguration.java @@ -1,47 +1,50 @@ /* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * Copyright (c) 2018-2025, lengleng All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) */ -package com.pig4cloud.pig.common.swagger; +package com.pig4cloud.pig.common.swagger.config; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.pig4cloud.pig.common.swagger.config.SwaggerProperties; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.builders.RequestParameterBuilder; +import springfox.documentation.schema.ScalarType; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.ApiSelectorBuilder; import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.function.Predicate; /** - * @author lengleng swagger配置 + * @author lengleng swagger配置 禁用方法1:使用注解@Profile({"dev","test"}) + * 表示在开发或测试环境开启,而在生产关闭。(推荐使用) 禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable", + * havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启,生产环境不填则默认关闭Swagger. */ -@EnableSwagger2 -@Configuration(proxyBeanMethods = false) -@EnableConfigurationProperties(SwaggerProperties.class) +@Configuration +@EnableAutoConfiguration @ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true) public class SwaggerAutoConfiguration { @@ -52,15 +55,18 @@ public class SwaggerAutoConfiguration { private static final String BASE_PATH = "/**"; + @Bean + @ConditionalOnMissingBean + public SwaggerProperties swaggerProperties() { + return new SwaggerProperties(); + } + @Bean public Docket api(SwaggerProperties swaggerProperties) { // base-path处理 if (swaggerProperties.getBasePath().isEmpty()) { swaggerProperties.getBasePath().add(BASE_PATH); } - // noinspection unchecked - List> basePath = new ArrayList(); - swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path))); // exclude-path处理 if (swaggerProperties.getExcludePath().isEmpty()) { @@ -69,48 +75,58 @@ public class SwaggerAutoConfiguration { List> excludePath = new ArrayList<>(); swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path))); - // noinspection Guava - return new Docket(DocumentationType.SWAGGER_2).host(swaggerProperties.getHost()) - .apiInfo(apiInfo(swaggerProperties)).select() - .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) - .paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath))).build() - .securitySchemes(Collections.singletonList(securitySchema(swaggerProperties))) - .securityContexts(Collections.singletonList(securityContext(swaggerProperties))).pathMapping("/"); + // 版本请求头处理 + List pars = new ArrayList<>(); + + RequestParameterBuilder versionPar = new RequestParameterBuilder().description("灰度路由版本信息") + .in(ParameterType.HEADER).name("VERSION").required(false) + .query(param -> param.model(model -> model.scalarModel(ScalarType.STRING))); + + pars.add(versionPar.build()); + + ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2).host(swaggerProperties.getHost()) + .apiInfo(apiInfo(swaggerProperties)).globalRequestParameters(pars).select() + .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())); + + swaggerProperties.getBasePath().forEach(p -> builder.paths(PathSelectors.ant(p))); + swaggerProperties.getExcludePath().forEach(p -> builder.paths(PathSelectors.ant(p).negate())); + + return builder.build().securitySchemes(Collections.singletonList(securitySchema())) + .securityContexts(Collections.singletonList(securityContext())).pathMapping("/"); } /** * 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL * @return */ - private SecurityContext securityContext(SwaggerProperties swaggerProperties) { - return SecurityContext.builder().securityReferences(defaultAuth(swaggerProperties)) - .forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex())).build(); + private SecurityContext securityContext() { + return SecurityContext.builder().securityReferences(defaultAuth()).build(); } /** * 默认的全局鉴权策略 * @return */ - private List defaultAuth(SwaggerProperties swaggerProperties) { + private List defaultAuth() { ArrayList authorizationScopeList = new ArrayList<>(); - swaggerProperties.getAuthorization().getAuthorizationScopeList() + swaggerProperties().getAuthorization().getAuthorizationScopeList() .forEach(authorizationScope -> authorizationScopeList.add( new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()]; return Collections - .singletonList(SecurityReference.builder().reference(swaggerProperties.getAuthorization().getName()) + .singletonList(SecurityReference.builder().reference(swaggerProperties().getAuthorization().getName()) .scopes(authorizationScopeList.toArray(authorizationScopes)).build()); } - private OAuth securitySchema(SwaggerProperties swaggerProperties) { + private OAuth securitySchema() { ArrayList authorizationScopeList = new ArrayList<>(); - swaggerProperties.getAuthorization().getAuthorizationScopeList() + swaggerProperties().getAuthorization().getAuthorizationScopeList() .forEach(authorizationScope -> authorizationScopeList.add( new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); ArrayList grantTypes = new ArrayList<>(); - swaggerProperties.getAuthorization().getTokenUrlList() + swaggerProperties().getAuthorization().getTokenUrlList() .forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl))); - return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes); + return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes); } private ApiInfo apiInfo(SwaggerProperties swaggerProperties) { diff --git a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerProperties.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerProperties.java index b0aedda7..e084e7d7 100644 --- a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerProperties.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/SwaggerProperties.java @@ -1,17 +1,18 @@ /* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * Copyright (c) 2018-2025, lengleng All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) */ package com.pig4cloud.pig.common.swagger.config; @@ -25,8 +26,8 @@ import java.util.List; /** * SwaggerProperties * - * @author: lengleng - * @date: 2018/7/25 14:00 + * @author lengleng + * @date 2018/7/25 14:00 */ @Data @ConfigurationProperties("swagger") @@ -52,6 +53,11 @@ public class SwaggerProperties { **/ private List excludePath = new ArrayList<>(); + /** + * 需要排除的服务 + */ + private List ignoreProviders = new ArrayList<>(); + /** * 标题 **/ diff --git a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/WebFluxSwaggerConfiguration.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/WebFluxSwaggerConfiguration.java new file mode 100644 index 00000000..790de6e9 --- /dev/null +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/config/WebFluxSwaggerConfiguration.java @@ -0,0 +1,21 @@ +package com.pig4cloud.pig.common.swagger.config; + +import org.springframework.web.reactive.config.ResourceHandlerRegistry; +import org.springframework.web.reactive.config.WebFluxConfigurer; + +/** + * @author lengleng + * @date 2020/10/2 + *

+ * webflux 网关 swagger 资源路径配置 + */ +public class WebFluxSwaggerConfiguration implements WebFluxConfigurer { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/swagger-ui/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") + .resourceChain(false); + } + +} \ No newline at end of file diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SwaggerProviderConfiguration.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProvider.java similarity index 52% rename from pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SwaggerProviderConfiguration.java rename to pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProvider.java index 2bb62881..5a5aaed1 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SwaggerProviderConfiguration.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProvider.java @@ -1,25 +1,29 @@ /* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * Copyright (c) 2018-2025, lengleng All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) */ -package com.pig4cloud.pig.gateway.config; +package com.pig4cloud.pig.common.swagger.support; +import com.pig4cloud.pig.common.swagger.config.SwaggerProperties; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.support.NameUtils; +import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.swagger.web.SwaggerResource; @@ -29,20 +33,23 @@ import java.util.ArrayList; import java.util.List; /** - * @author lengleng - * @date 2019-11-28 + * @author Sywd 聚合接口文档注册,和zuul实现类似 */ -@Component @Primary +@Component @RequiredArgsConstructor -public class SwaggerProviderConfiguration implements SwaggerResourcesProvider { +public class SwaggerProvider implements SwaggerResourcesProvider { private static final String API_URI = "/v2/api-docs"; - private final RouteLocator routeLocator; + private final SwaggerProperties swaggerProperties; private final GatewayProperties gatewayProperties; + @Lazy + @Autowired + private RouteLocator routeLocator; + @Override public List get() { List resources = new ArrayList<>(); @@ -51,14 +58,15 @@ public class SwaggerProviderConfiguration implements SwaggerResourcesProvider { gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())) .forEach(routeDefinition -> routeDefinition.getPredicates().stream() .filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName())) - .filter(predicateDefinition -> !"pig-auth".equalsIgnoreCase(routeDefinition.getId())) + .filter(predicateDefinition -> !swaggerProperties.getIgnoreProviders() + .contains(routeDefinition.getId())) .forEach(predicateDefinition -> resources .add(swaggerResource(routeDefinition.getId(), predicateDefinition.getArgs() .get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", API_URI))))); return resources; } - private SwaggerResource swaggerResource(String name, String location) { + private static SwaggerResource swaggerResource(String name, String location) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerResourceHandler.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerResourceHandler.java similarity index 55% rename from pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerResourceHandler.java rename to pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerResourceHandler.java index 84afae76..e091789f 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerResourceHandler.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerResourceHandler.java @@ -1,22 +1,23 @@ /* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * Copyright (c) 2018-2025, lengleng All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) */ -package com.pig4cloud.pig.gateway.handler; +package com.pig4cloud.pig.common.swagger.support; -import lombok.RequiredArgsConstructor; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -34,7 +35,7 @@ import springfox.documentation.swagger.web.SwaggerResourcesProvider; */ @Slf4j @Component -@RequiredArgsConstructor +@AllArgsConstructor public class SwaggerResourceHandler implements HandlerFunction { private final SwaggerResourcesProvider swaggerResources; diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerSecurityHandler.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerSecurityHandler.java similarity index 62% rename from pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerSecurityHandler.java rename to pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerSecurityHandler.java index d41b348e..9cc20460 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerSecurityHandler.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerSecurityHandler.java @@ -1,20 +1,21 @@ /* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * Copyright (c) 2018-2025, lengleng All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) */ -package com.pig4cloud.pig.gateway.handler; +package com.pig4cloud.pig.common.swagger.support; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerUiHandler.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerUiHandler.java similarity index 61% rename from pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerUiHandler.java rename to pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerUiHandler.java index ad61e149..02a42693 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/SwaggerUiHandler.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerUiHandler.java @@ -1,20 +1,21 @@ /* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * Copyright (c) 2018-2025, lengleng All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) */ -package com.pig4cloud.pig.gateway.handler; +package com.pig4cloud.pig.common.swagger.support; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; diff --git a/pig-common/pig-common-swagger/src/main/resources/META-INF/spring.factories b/pig-common/pig-common-swagger/src/main/resources/META-INF/spring.factories deleted file mode 100755 index dd5a6756..00000000 --- a/pig-common/pig-common-swagger/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.pig4cloud.pig.common.swagger.SwaggerAutoConfiguration diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/PigGatewayApplication.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/PigGatewayApplication.java index 2e20d01e..a2bfd784 100755 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/PigGatewayApplication.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/PigGatewayApplication.java @@ -16,6 +16,7 @@ package com.pig4cloud.pig.gateway; +import com.pig4cloud.pig.common.swagger.annotation.EnablePigSwagger2; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; @@ -25,6 +26,7 @@ import org.springframework.cloud.client.SpringCloudApplication; *

* 网关应用 */ +@EnablePigSwagger2 @SpringCloudApplication public class PigGatewayApplication { diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GatewayConfigProperties.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GatewayConfigProperties.java new file mode 100644 index 00000000..31a64411 --- /dev/null +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GatewayConfigProperties.java @@ -0,0 +1,32 @@ +package com.pig4cloud.pig.gateway.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author lengleng + * @date 2020/10/4 + *

+ * 网关配置文件 + */ +@Data +@Component +@RefreshScope +@ConfigurationProperties("gateway") +public class GatewayConfigProperties { + + /** + * 网关解密登录前端密码 秘钥 {@link com.pig4cloud.pig.gateway.filter.PasswordDecoderFilter} + */ + public String encodeKey; + + /** + * 网关不需要校验验证码的客户端 {@link com.pig4cloud.pig.gateway.filter.ValidateCodeGatewayFilter} + */ + public List ignoreClients; + +} \ No newline at end of file diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/RouterFunctionConfiguration.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/RouterFunctionConfiguration.java index fb92f227..93f12b82 100755 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/RouterFunctionConfiguration.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/RouterFunctionConfiguration.java @@ -17,9 +17,6 @@ package com.pig4cloud.pig.gateway.config; import com.pig4cloud.pig.gateway.handler.ImageCodeHandler; -import com.pig4cloud.pig.gateway.handler.SwaggerResourceHandler; -import com.pig4cloud.pig.gateway.handler.SwaggerSecurityHandler; -import com.pig4cloud.pig.gateway.handler.SwaggerUiHandler; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; @@ -42,24 +39,10 @@ public class RouterFunctionConfiguration { private final ImageCodeHandler imageCodeHandler; - private final SwaggerResourceHandler swaggerResourceHandler; - - private final SwaggerSecurityHandler swaggerSecurityHandler; - - private final SwaggerUiHandler swaggerUiHandler; - @Bean public RouterFunction routerFunction() { - return RouterFunctions - .route(RequestPredicates.path("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), - imageCodeHandler) - .andRoute(RequestPredicates.GET("/swagger-resources").and(RequestPredicates.accept(MediaType.ALL)), - swaggerResourceHandler) - .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") - .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler) - .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security") - .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler); - + return RouterFunctions.route( + RequestPredicates.path("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler); } } diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/PasswordDecoderFilter.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/PasswordDecoderFilter.java index 9d87ff7c..8cf4a82c 100755 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/PasswordDecoderFilter.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/PasswordDecoderFilter.java @@ -24,8 +24,9 @@ import cn.hutool.crypto.Padding; import cn.hutool.crypto.symmetric.AES; import cn.hutool.http.HttpUtil; import com.pig4cloud.pig.common.core.constant.SecurityConstants; +import com.pig4cloud.pig.gateway.config.GatewayConfigProperties; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -45,14 +46,14 @@ import java.util.Map; */ @Slf4j @Component +@RequiredArgsConstructor public class PasswordDecoderFilter extends AbstractGatewayFilterFactory { private static final String PASSWORD = "password"; private static final String KEY_ALGORITHM = "AES"; - @Value("${security.encode.key:1234567812345678}") - private String encodeKey; + private GatewayConfigProperties configProperties; private static String decryptAES(String data, String pass) { AES aes = new AES(Mode.CBC, Padding.NoPadding, new SecretKeySpec(pass.getBytes(), KEY_ALGORITHM), @@ -78,7 +79,7 @@ public class PasswordDecoderFilter extends AbstractGatewayFilterFactory { String password = paramMap.get(PASSWORD); if (StrUtil.isNotBlank(password)) { try { - password = decryptAES(password, encodeKey); + password = decryptAES(password, configProperties.getEncodeKey()); } catch (Exception e) { log.error("密码解密失败:{}", password); diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java index d90bc7fe..8db98f8c 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java @@ -24,7 +24,7 @@ import com.pig4cloud.pig.common.core.constant.SecurityConstants; import com.pig4cloud.pig.common.core.exception.ValidateCodeException; import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.WebUtils; -import com.pig4cloud.pig.gateway.config.IgnoreClientConfiguration; +import com.pig4cloud.pig.gateway.config.GatewayConfigProperties; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -48,7 +48,7 @@ import reactor.core.publisher.Mono; @RequiredArgsConstructor public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory { - private final IgnoreClientConfiguration ignoreClient; + private final GatewayConfigProperties configProperties; private final ObjectMapper objectMapper; @@ -73,7 +73,7 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory { // 终端设置不校验, 直接向下执行 try { String[] clientInfos = WebUtils.getClientId(request); - if (ignoreClient.getClients().contains(clientInfos[0])) { + if (configProperties.getIgnoreClients().contains(clientInfos[0])) { return chain.filter(exchange); } diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/PigAdminApplication.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/PigAdminApplication.java index 4d89b2b8..e305d4c1 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/PigAdminApplication.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/PigAdminApplication.java @@ -18,6 +18,7 @@ package com.pig4cloud.pig.admin; import com.pig4cloud.pig.common.security.annotation.EnablePigFeignClients; import com.pig4cloud.pig.common.security.annotation.EnablePigResourceServer; +import com.pig4cloud.pig.common.swagger.annotation.EnablePigSwagger2; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; @@ -25,6 +26,7 @@ import org.springframework.cloud.client.SpringCloudApplication; * @author lengleng * @date 2018年06月21日 用户统一管理系统 */ +@EnablePigSwagger2 @EnablePigResourceServer @EnablePigFeignClients @SpringCloudApplication