mirror of https://gitee.com/maxjhandsome/pig
✨ Introducing new features. closed #I4313X 【新功能】Swagger-ui 添加账号密码
This commit is contained in:
parent
d3839397fa
commit
f8e70d4e9f
|
@ -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<SecurityConfiguration> securityConfigurationObjectProvider) {
|
||||
|
|
|
@ -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<Void> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue