♻️ Refactoring code. up swagger 3.0 重构 common-swagger 模块

This commit is contained in:
lengleng 2020-10-04 14:19:54 +08:00
parent c4e2bbb8cc
commit f213de9143
17 changed files with 315 additions and 169 deletions

View File

@ -1,18 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Copyright (c) 2018-2025, lengleng All rights reserved.
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~ ~
~ http://www.apache.org/licenses/LICENSE-2.0 ~ Redistribution and use in source and binary forms, with or without
~ modification, are permitted provided that the following conditions are met:
~
~ 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)
~ ~
~ 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.
--> -->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
@ -31,11 +34,38 @@
<dependencies> <dependencies>
<!--swagger 依赖--> <!--接口文档-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<dependency> <dependency>
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId> <artifactId>springfox-swagger2</artifactId>
<version>${swagger.fox.version}</version> <version>${swagger.fox.version}</version>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-oas</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<!--webflux 相关包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>provided</scope>
</dependency>
<!--网关 swagger 聚合依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -14,29 +14,25 @@
* limitations under the License. * limitations under the License.
*/ */
package com.pig4cloud.pig.gateway.config; package com.pig4cloud.pig.common.swagger.annotation;
import lombok.Data; import com.pig4cloud.pig.common.swagger.config.GatewaySwaggerAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties; import com.pig4cloud.pig.common.swagger.config.SwaggerAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList; import java.lang.annotation.*;
import java.util.List;
/** /**
* @author lengleng * @author lengleng
* @date 2019/2/1 放行参数配置 * @date 2020/10/2 开启pig swagger
*/ */
@Data @Target({ ElementType.TYPE })
@Component @Retention(RetentionPolicy.RUNTIME)
@RefreshScope @Documented
@ConfigurationProperties(prefix = "ignore") @Inherited
public class IgnoreClientConfiguration { @EnableSwagger2
@Import({ SwaggerAutoConfiguration.class, GatewaySwaggerAutoConfiguration.class })
/** public @interface EnablePigSwagger2 {
* 放行终端配置网关不校验此处的终端
*/
private List<String> clients = new ArrayList<>();
} }

View File

@ -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
* <p>
* 网关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);
}
}

View File

@ -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"); * Redistribution and use in source and binary forms, with or without
* you may not use this file except in compliance with the License. * modification, are permitted provided that the following conditions are met:
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Redistributions of source code must retain the above copyright notice,
* * this list of conditions and the following disclaimer.
* Unless required by applicable law or agreed to in writing, software * Redistributions in binary form must reproduce the above copyright
* distributed under the License is distributed on an "AS IS" BASIS, * notice, this list of conditions and the following disclaimer in the
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * documentation and/or other materials provided with the distribution.
* See the License for the specific language governing permissions and * Neither the name of the pig4cloud.com developer nor the names of its
* limitations under the License. * 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 org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import com.google.common.base.Predicates; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import com.pig4cloud.pig.common.swagger.config.SwaggerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*; import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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
@Configuration(proxyBeanMethods = false) @EnableAutoConfiguration
@EnableConfigurationProperties(SwaggerProperties.class)
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
public class SwaggerAutoConfiguration { public class SwaggerAutoConfiguration {
@ -52,15 +55,18 @@ public class SwaggerAutoConfiguration {
private static final String BASE_PATH = "/**"; private static final String BASE_PATH = "/**";
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() {
return new SwaggerProperties();
}
@Bean @Bean
public Docket api(SwaggerProperties swaggerProperties) { public Docket api(SwaggerProperties swaggerProperties) {
// base-path处理 // base-path处理
if (swaggerProperties.getBasePath().isEmpty()) { if (swaggerProperties.getBasePath().isEmpty()) {
swaggerProperties.getBasePath().add(BASE_PATH); swaggerProperties.getBasePath().add(BASE_PATH);
} }
// noinspection unchecked
List<Predicate<String>> basePath = new ArrayList();
swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));
// exclude-path处理 // exclude-path处理
if (swaggerProperties.getExcludePath().isEmpty()) { if (swaggerProperties.getExcludePath().isEmpty()) {
@ -69,48 +75,58 @@ public class SwaggerAutoConfiguration {
List<Predicate<String>> excludePath = new ArrayList<>(); List<Predicate<String>> excludePath = new ArrayList<>();
swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path))); swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path)));
// noinspection Guava // 版本请求头处理
return new Docket(DocumentationType.SWAGGER_2).host(swaggerProperties.getHost()) List<RequestParameter> pars = new ArrayList<>();
.apiInfo(apiInfo(swaggerProperties)).select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) RequestParameterBuilder versionPar = new RequestParameterBuilder().description("灰度路由版本信息")
.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath))).build() .in(ParameterType.HEADER).name("VERSION").required(false)
.securitySchemes(Collections.singletonList(securitySchema(swaggerProperties))) .query(param -> param.model(model -> model.scalarModel(ScalarType.STRING)));
.securityContexts(Collections.singletonList(securityContext(swaggerProperties))).pathMapping("/");
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 * 配置默认的全局鉴权策略的开关通过正则表达式进行匹配默认匹配所有URL
* @return * @return
*/ */
private SecurityContext securityContext(SwaggerProperties swaggerProperties) { private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth(swaggerProperties)) return SecurityContext.builder().securityReferences(defaultAuth()).build();
.forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex())).build();
} }
/** /**
* 默认的全局鉴权策略 * 默认的全局鉴权策略
* @return * @return
*/ */
private List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) { private List<SecurityReference> defaultAuth() {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>(); ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList() swaggerProperties().getAuthorization().getAuthorizationScopeList()
.forEach(authorizationScope -> authorizationScopeList.add( .forEach(authorizationScope -> authorizationScopeList.add(
new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()]; AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
return Collections return Collections
.singletonList(SecurityReference.builder().reference(swaggerProperties.getAuthorization().getName()) .singletonList(SecurityReference.builder().reference(swaggerProperties().getAuthorization().getName())
.scopes(authorizationScopeList.toArray(authorizationScopes)).build()); .scopes(authorizationScopeList.toArray(authorizationScopes)).build());
} }
private OAuth securitySchema(SwaggerProperties swaggerProperties) { private OAuth securitySchema() {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>(); ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList() swaggerProperties().getAuthorization().getAuthorizationScopeList()
.forEach(authorizationScope -> authorizationScopeList.add( .forEach(authorizationScope -> authorizationScopeList.add(
new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
ArrayList<GrantType> grantTypes = new ArrayList<>(); ArrayList<GrantType> grantTypes = new ArrayList<>();
swaggerProperties.getAuthorization().getTokenUrlList() swaggerProperties().getAuthorization().getTokenUrlList()
.forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl))); .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) { private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {

View File

@ -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"); * Redistribution and use in source and binary forms, with or without
* you may not use this file except in compliance with the License. * modification, are permitted provided that the following conditions are met:
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Redistributions of source code must retain the above copyright notice,
* * this list of conditions and the following disclaimer.
* Unless required by applicable law or agreed to in writing, software * Redistributions in binary form must reproduce the above copyright
* distributed under the License is distributed on an "AS IS" BASIS, * notice, this list of conditions and the following disclaimer in the
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * documentation and/or other materials provided with the distribution.
* See the License for the specific language governing permissions and * Neither the name of the pig4cloud.com developer nor the names of its
* limitations under the License. * 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; package com.pig4cloud.pig.common.swagger.config;
@ -25,8 +26,8 @@ import java.util.List;
/** /**
* SwaggerProperties * SwaggerProperties
* *
* @author: lengleng * @author lengleng
* @date: 2018/7/25 14:00 * @date 2018/7/25 14:00
*/ */
@Data @Data
@ConfigurationProperties("swagger") @ConfigurationProperties("swagger")
@ -52,6 +53,11 @@ public class SwaggerProperties {
**/ **/
private List<String> excludePath = new ArrayList<>(); private List<String> excludePath = new ArrayList<>();
/**
* 需要排除的服务
*/
private List<String> ignoreProviders = new ArrayList<>();
/** /**
* 标题 * 标题
**/ **/

View File

@ -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
* <p>
* 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);
}
}

View File

@ -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"); * Redistribution and use in source and binary forms, with or without
* you may not use this file except in compliance with the License. * modification, are permitted provided that the following conditions are met:
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Redistributions of source code must retain the above copyright notice,
* * this list of conditions and the following disclaimer.
* Unless required by applicable law or agreed to in writing, software * Redistributions in binary form must reproduce the above copyright
* distributed under the License is distributed on an "AS IS" BASIS, * notice, this list of conditions and the following disclaimer in the
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * documentation and/or other materials provided with the distribution.
* See the License for the specific language governing permissions and * Neither the name of the pig4cloud.com developer nor the names of its
* limitations under the License. * 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 lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils; import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResource;
@ -29,20 +33,23 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* @author lengleng * @author Sywd 聚合接口文档注册和zuul实现类似
* @date 2019-11-28
*/ */
@Component
@Primary @Primary
@Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class SwaggerProviderConfiguration implements SwaggerResourcesProvider { public class SwaggerProvider implements SwaggerResourcesProvider {
private static final String API_URI = "/v2/api-docs"; private static final String API_URI = "/v2/api-docs";
private final RouteLocator routeLocator; private final SwaggerProperties swaggerProperties;
private final GatewayProperties gatewayProperties; private final GatewayProperties gatewayProperties;
@Lazy
@Autowired
private RouteLocator routeLocator;
@Override @Override
public List<SwaggerResource> get() { public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>(); List<SwaggerResource> resources = new ArrayList<>();
@ -51,14 +58,15 @@ public class SwaggerProviderConfiguration implements SwaggerResourcesProvider {
gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())) gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
.forEach(routeDefinition -> routeDefinition.getPredicates().stream() .forEach(routeDefinition -> routeDefinition.getPredicates().stream()
.filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName())) .filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName()))
.filter(predicateDefinition -> !"pig-auth".equalsIgnoreCase(routeDefinition.getId())) .filter(predicateDefinition -> !swaggerProperties.getIgnoreProviders()
.contains(routeDefinition.getId()))
.forEach(predicateDefinition -> resources .forEach(predicateDefinition -> resources
.add(swaggerResource(routeDefinition.getId(), predicateDefinition.getArgs() .add(swaggerResource(routeDefinition.getId(), predicateDefinition.getArgs()
.get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", API_URI))))); .get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", API_URI)))));
return resources; return resources;
} }
private SwaggerResource swaggerResource(String name, String location) { private static SwaggerResource swaggerResource(String name, String location) {
SwaggerResource swaggerResource = new SwaggerResource(); SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name); swaggerResource.setName(name);
swaggerResource.setLocation(location); swaggerResource.setLocation(location);

View File

@ -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"); * Redistribution and use in source and binary forms, with or without
* you may not use this file except in compliance with the License. * modification, are permitted provided that the following conditions are met:
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Redistributions of source code must retain the above copyright notice,
* * this list of conditions and the following disclaimer.
* Unless required by applicable law or agreed to in writing, software * Redistributions in binary form must reproduce the above copyright
* distributed under the License is distributed on an "AS IS" BASIS, * notice, this list of conditions and the following disclaimer in the
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * documentation and/or other materials provided with the distribution.
* See the License for the specific language governing permissions and * Neither the name of the pig4cloud.com developer nor the names of its
* limitations under the License. * 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 lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -34,7 +35,7 @@ import springfox.documentation.swagger.web.SwaggerResourcesProvider;
*/ */
@Slf4j @Slf4j
@Component @Component
@RequiredArgsConstructor @AllArgsConstructor
public class SwaggerResourceHandler implements HandlerFunction<ServerResponse> { public class SwaggerResourceHandler implements HandlerFunction<ServerResponse> {
private final SwaggerResourcesProvider swaggerResources; private final SwaggerResourcesProvider swaggerResources;

View File

@ -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"); * Redistribution and use in source and binary forms, with or without
* you may not use this file except in compliance with the License. * modification, are permitted provided that the following conditions are met:
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Redistributions of source code must retain the above copyright notice,
* * this list of conditions and the following disclaimer.
* Unless required by applicable law or agreed to in writing, software * Redistributions in binary form must reproduce the above copyright
* distributed under the License is distributed on an "AS IS" BASIS, * notice, this list of conditions and the following disclaimer in the
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * documentation and/or other materials provided with the distribution.
* See the License for the specific language governing permissions and * Neither the name of the pig4cloud.com developer nor the names of its
* limitations under the License. * 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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -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"); * Redistribution and use in source and binary forms, with or without
* you may not use this file except in compliance with the License. * modification, are permitted provided that the following conditions are met:
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * Redistributions of source code must retain the above copyright notice,
* * this list of conditions and the following disclaimer.
* Unless required by applicable law or agreed to in writing, software * Redistributions in binary form must reproduce the above copyright
* distributed under the License is distributed on an "AS IS" BASIS, * notice, this list of conditions and the following disclaimer in the
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * documentation and/or other materials provided with the distribution.
* See the License for the specific language governing permissions and * Neither the name of the pig4cloud.com developer nor the names of its
* limitations under the License. * 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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pig.common.swagger.SwaggerAutoConfiguration

View File

@ -16,6 +16,7 @@
package com.pig4cloud.pig.gateway; package com.pig4cloud.pig.gateway;
import com.pig4cloud.pig.common.swagger.annotation.EnablePigSwagger2;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.client.SpringCloudApplication;
@ -25,6 +26,7 @@ import org.springframework.cloud.client.SpringCloudApplication;
* <p> * <p>
* 网关应用 * 网关应用
*/ */
@EnablePigSwagger2
@SpringCloudApplication @SpringCloudApplication
public class PigGatewayApplication { public class PigGatewayApplication {

View File

@ -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
* <p>
* 网关配置文件
*/
@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<String> ignoreClients;
}

View File

@ -17,9 +17,6 @@
package com.pig4cloud.pig.gateway.config; package com.pig4cloud.pig.gateway.config;
import com.pig4cloud.pig.gateway.handler.ImageCodeHandler; 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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -42,24 +39,10 @@ public class RouterFunctionConfiguration {
private final ImageCodeHandler imageCodeHandler; private final ImageCodeHandler imageCodeHandler;
private final SwaggerResourceHandler swaggerResourceHandler;
private final SwaggerSecurityHandler swaggerSecurityHandler;
private final SwaggerUiHandler swaggerUiHandler;
@Bean @Bean
public RouterFunction routerFunction() { public RouterFunction routerFunction() {
return RouterFunctions return RouterFunctions.route(
.route(RequestPredicates.path("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), RequestPredicates.path("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler);
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);
} }
} }

View File

@ -24,8 +24,9 @@ import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.pig4cloud.pig.common.core.constant.SecurityConstants; import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.gateway.config.GatewayConfigProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; 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.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
@ -45,14 +46,14 @@ import java.util.Map;
*/ */
@Slf4j @Slf4j
@Component @Component
@RequiredArgsConstructor
public class PasswordDecoderFilter extends AbstractGatewayFilterFactory { public class PasswordDecoderFilter extends AbstractGatewayFilterFactory {
private static final String PASSWORD = "password"; private static final String PASSWORD = "password";
private static final String KEY_ALGORITHM = "AES"; private static final String KEY_ALGORITHM = "AES";
@Value("${security.encode.key:1234567812345678}") private GatewayConfigProperties configProperties;
private String encodeKey;
private static String decryptAES(String data, String pass) { private static String decryptAES(String data, String pass) {
AES aes = new AES(Mode.CBC, Padding.NoPadding, new SecretKeySpec(pass.getBytes(), KEY_ALGORITHM), 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); String password = paramMap.get(PASSWORD);
if (StrUtil.isNotBlank(password)) { if (StrUtil.isNotBlank(password)) {
try { try {
password = decryptAES(password, encodeKey); password = decryptAES(password, configProperties.getEncodeKey());
} }
catch (Exception e) { catch (Exception e) {
log.error("密码解密失败:{}", password); log.error("密码解密失败:{}", password);

View File

@ -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.exception.ValidateCodeException;
import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.WebUtils; 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.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -48,7 +48,7 @@ import reactor.core.publisher.Mono;
@RequiredArgsConstructor @RequiredArgsConstructor
public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory { public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
private final IgnoreClientConfiguration ignoreClient; private final GatewayConfigProperties configProperties;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
@ -73,7 +73,7 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
// 终端设置不校验 直接向下执行 // 终端设置不校验 直接向下执行
try { try {
String[] clientInfos = WebUtils.getClientId(request); String[] clientInfos = WebUtils.getClientId(request);
if (ignoreClient.getClients().contains(clientInfos[0])) { if (configProperties.getIgnoreClients().contains(clientInfos[0])) {
return chain.filter(exchange); return chain.filter(exchange);
} }

View File

@ -18,6 +18,7 @@ package com.pig4cloud.pig.admin;
import com.pig4cloud.pig.common.security.annotation.EnablePigFeignClients; import com.pig4cloud.pig.common.security.annotation.EnablePigFeignClients;
import com.pig4cloud.pig.common.security.annotation.EnablePigResourceServer; import com.pig4cloud.pig.common.security.annotation.EnablePigResourceServer;
import com.pig4cloud.pig.common.swagger.annotation.EnablePigSwagger2;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.client.SpringCloudApplication;
@ -25,6 +26,7 @@ import org.springframework.cloud.client.SpringCloudApplication;
* @author lengleng * @author lengleng
* @date 2018年06月21日 用户统一管理系统 * @date 2018年06月21日 用户统一管理系统
*/ */
@EnablePigSwagger2
@EnablePigResourceServer @EnablePigResourceServer
@EnablePigFeignClients @EnablePigFeignClients
@SpringCloudApplication @SpringCloudApplication