⚡ 优化接口放行支持通配符匹配逻辑
This commit is contained in:
parent
82a0c3ec25
commit
efab0a675f
|
|
@ -32,6 +32,7 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
@ -49,6 +50,7 @@ import java.nio.charset.StandardCharsets;
|
||||||
public class AuthFilter implements GlobalFilter, Ordered {
|
public class AuthFilter implements GlobalFilter, Ordered {
|
||||||
private final AuthProperties authProperties;
|
private final AuthProperties authProperties;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
|
@ -72,8 +74,8 @@ public class AuthFilter implements GlobalFilter, Ordered {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isSkip(String path) {
|
private boolean isSkip(String path) {
|
||||||
return AuthProvider.getDefaultSkipUrl().stream().map(url -> url.replace(AuthProvider.TARGET, AuthProvider.REPLACEMENT)).anyMatch(path::contains)
|
return AuthProvider.getDefaultSkipUrl().stream().anyMatch(pattern -> antPathMatcher.match(pattern, path))
|
||||||
|| authProperties.getSkipUrl().stream().map(url -> url.replace(AuthProvider.TARGET, AuthProvider.REPLACEMENT)).anyMatch(path::contains);
|
|| authProperties.getSkipUrl().stream().anyMatch(pattern -> antPathMatcher.match(pattern, path));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<Void> unAuth(ServerHttpResponse resp, String msg) {
|
private Mono<Void> unAuth(ServerHttpResponse resp, String msg) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package org.springblade.gateway.filter;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* request过滤器
|
||||||
|
*
|
||||||
|
* @author lengleng
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class RequestFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
addOriginalRequestUrl(exchange, request.getURI());
|
||||||
|
String rawPath = request.getURI().getRawPath();
|
||||||
|
String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/"))
|
||||||
|
.skip(1L).collect(Collectors.joining("/"));
|
||||||
|
ServerHttpRequest newRequest = request.mutate()
|
||||||
|
.path(newPath)
|
||||||
|
.build();
|
||||||
|
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
|
||||||
|
return chain.filter(exchange.mutate().request(newRequest.mutate().build()).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return -1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -27,34 +27,32 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class AuthProvider {
|
public class AuthProvider {
|
||||||
|
|
||||||
public static String TARGET = "/**";
|
|
||||||
public static String REPLACEMENT = "";
|
|
||||||
public static String AUTH_KEY = TokenConstant.HEADER;
|
public static String AUTH_KEY = TokenConstant.HEADER;
|
||||||
private static final List<String> defaultSkipUrl = new ArrayList<>();
|
private static final List<String> DEFAULT_SKIP_URL = new ArrayList<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
defaultSkipUrl.add("/example");
|
DEFAULT_SKIP_URL.add("/example");
|
||||||
defaultSkipUrl.add("/token/**");
|
DEFAULT_SKIP_URL.add("/token/**");
|
||||||
defaultSkipUrl.add("/captcha/**");
|
DEFAULT_SKIP_URL.add("/captcha/**");
|
||||||
defaultSkipUrl.add("/actuator/health/**");
|
DEFAULT_SKIP_URL.add("/actuator/health/**");
|
||||||
defaultSkipUrl.add("/v2/api-docs/**");
|
DEFAULT_SKIP_URL.add("/v2/api-docs/**");
|
||||||
defaultSkipUrl.add("/auth/**");
|
DEFAULT_SKIP_URL.add("/auth/**");
|
||||||
defaultSkipUrl.add("/oauth/**");
|
DEFAULT_SKIP_URL.add("/oauth/**");
|
||||||
defaultSkipUrl.add("/log/**");
|
DEFAULT_SKIP_URL.add("/log/**");
|
||||||
defaultSkipUrl.add("/menu/routes");
|
DEFAULT_SKIP_URL.add("/menu/routes");
|
||||||
defaultSkipUrl.add("/menu/auth-routes");
|
DEFAULT_SKIP_URL.add("/menu/auth-routes");
|
||||||
defaultSkipUrl.add("/tenant/info");
|
DEFAULT_SKIP_URL.add("/tenant/info");
|
||||||
defaultSkipUrl.add("/order/create/**");
|
DEFAULT_SKIP_URL.add("/order/create/**");
|
||||||
defaultSkipUrl.add("/storage/deduct/**");
|
DEFAULT_SKIP_URL.add("/storage/deduct/**");
|
||||||
defaultSkipUrl.add("/error/**");
|
DEFAULT_SKIP_URL.add("/error/**");
|
||||||
defaultSkipUrl.add("/assets/**");
|
DEFAULT_SKIP_URL.add("/assets/**");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认无需鉴权的API
|
* 默认无需鉴权的API
|
||||||
*/
|
*/
|
||||||
public static List<String> getDefaultSkipUrl() {
|
public static List<String> getDefaultSkipUrl() {
|
||||||
return defaultSkipUrl;
|
return DEFAULT_SKIP_URL;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user