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 index 874de21e..856c95c5 100644 --- 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 @@ -2,6 +2,7 @@ package com.pig4cloud.pig.common.swagger.config; import com.pig4cloud.pig.common.swagger.support.*; import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.context.annotation.Bean; @@ -10,7 +11,10 @@ import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; -import springfox.documentation.swagger.web.*; +import springfox.documentation.swagger.web.SecurityConfiguration; +import springfox.documentation.swagger.web.SecurityConfigurationBuilder; +import springfox.documentation.swagger.web.UiConfiguration; +import springfox.documentation.swagger.web.UiConfigurationBuilder; /** * 网关swagger 配置类,仅在webflux 环境生效哦 @@ -36,6 +40,12 @@ public class GatewaySwaggerAutoConfiguration { return new WebFluxSwaggerConfiguration(); } + @Bean + @ConditionalOnProperty(value = "swagger.basic.enabled", havingValue = "true") + public SwaggerBasicGatewayFilter swaggerBasicGatewayFilter(SwaggerProperties swaggerProperties) { + return new SwaggerBasicGatewayFilter(swaggerProperties); + } + @Bean public SwaggerSecurityHandler swaggerSecurityHandler( ObjectProvider securityConfigurationObjectProvider) { diff --git a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerBasicGatewayFilter.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerBasicGatewayFilter.java new file mode 100644 index 00000000..3eef84e9 --- /dev/null +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerBasicGatewayFilter.java @@ -0,0 +1,75 @@ +package com.pig4cloud.pig.common.swagger.support; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.util.Base64Utils; +import org.springframework.util.StringUtils; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.nio.charset.StandardCharsets; + +/** + * swagger 开启basic 认证 + * + * @author Lht + * @date 2021/8/8 + */ +@Slf4j +@RequiredArgsConstructor +public class SwaggerBasicGatewayFilter implements GlobalFilter { + + private static final String API_URI = "/v2/api-docs"; + + private static final String BASIC_PREFIX = "Basic "; + + private final SwaggerProperties swaggerProperties; + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + ServerHttpRequest request = exchange.getRequest(); + + if (!request.getURI().getPath().contains(API_URI)) { + return chain.filter(exchange); + } + + if (hasAuth(exchange)) { + return chain.filter(exchange); + } + else { + ServerHttpResponse response = exchange.getResponse(); + response.setStatusCode(HttpStatus.UNAUTHORIZED); + response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, "Basic Realm=\"pig\""); + return response.setComplete(); + } + } + + /** + * 简单的basic认证 + * @param exchange 上下文 + * @return 是否有权限 + */ + private boolean hasAuth(ServerWebExchange exchange) { + ServerHttpRequest request = exchange.getRequest(); + String auth = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION); + log.info("Basic认证信息为:{}", auth); + if (!StringUtils.hasText(auth) || !auth.startsWith(BASIC_PREFIX)) { + return Boolean.FALSE; + } + + String username = swaggerProperties.getBasic().getUsername(); + String password = swaggerProperties.getBasic().getPassword(); + + String encodeToString = Base64Utils + .encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); + + return auth.equals(BASIC_PREFIX + encodeToString); + } + +} diff --git a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProperties.java b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProperties.java index d83f96b8..d5d7d737 100644 --- a/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProperties.java +++ b/pig-common/pig-common-swagger/src/main/java/com/pig4cloud/pig/common/swagger/support/SwaggerProperties.java @@ -19,7 +19,6 @@ package com.pig4cloud.pig.common.swagger.support; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @@ -104,6 +103,11 @@ public class SwaggerProperties { **/ private Authorization authorization = new Authorization(); + /** + * 认证参数 + */ + private SwaggerBasic basic = new SwaggerBasic(); + @Data @NoArgsConstructor public static class Contact { @@ -164,4 +168,24 @@ public class SwaggerProperties { } + @Data + public static class SwaggerBasic { + + /** + * 是否开启 basic 认证 + */ + private Boolean enabled; + + /** + * 用户名 + */ + private String username; + + /** + * 密码 + */ + private String password; + + } + }