首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有上下文根路径的spring-webflux安全性中的白名单端点

没有上下文根路径的spring-webflux安全性中的白名单端点
EN

Stack Overflow用户
提问于 2021-04-08 21:34:05
回答 1查看 720关注 0票数 0

我试图白化所有以/health结尾的自定义端点,但在WebFlux Security中,我无法使用正则表达式来匹配所有端点。例如/app/health、/app/meta/health、/app/定制/meta/health、/api/app/定制/meta/health等。在构造SecurityWebFilterChain时,我没有找到任何模式(或regex)来白化这些端点。此外,如果我试图使用regex,也会收到警告。

代码语言:javascript
复制
spring-security '**' patterns are not supported in the middle of patterns and will be rejected in the future. Consider using '*' instead for matching a single path segment.

下面是代码片段

代码语言:javascript
复制
String[] ALLOWED_PATTERNS = {".*/health"};
// String[] ALLOWED_PATTERNS = {"/*/health"};  // allows whitelist /app/health endpoint
// String[] ALLOWED_PATTERNS = {"/*/health", "/*/*/health"};  // need generic way to whitelist all

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
    return http
        .authorizeExchange()
        .pathMatchers(ALLOWED_PATTERNS).permitAll()
        .anyExchange().authenticated()
         ...
        .build();
}

通过提供antMatchers,我可以在Security (不是反应性/web流量安全)中实现同样的目标。

代码语言:javascript
复制
@Override
public void configure(WebSecurity web) {
   web.ignoring().antMatchers("/**/health");
}

注意:这不是弹簧致动器健康端点,而是自定义的健康端点

EN

回答 1

Stack Overflow用户

发布于 2022-01-18 13:04:12

你可以写你自己的matcher。

代码语言:javascript
复制
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class RegexRequestMatcher {

    private static final Cache<String, Pattern> CACHE = CacheBuilder.newBuilder().build();
    private static final ConcurrentMap<String, Pattern> PATTERNS = CACHE.asMap();

    public static Mono<ServerWebExchangeMatcher.MatchResult> matches(String regex, ServerWebExchange exchange) {
        return matches(regex, exchange.getRequest().getURI().getPath());
    }

    public static Mono<ServerWebExchangeMatcher.MatchResult> matches(String regex, String uri) {
        Pattern pattern = getPattern(regex);
        Matcher matcher = pattern.matcher(uri);
        boolean matches = matcher.matches();
        return Mono.just(matches)
                .flatMap(m -> m ? ServerWebExchangeMatcher.MatchResult.match() : ServerWebExchangeMatcher.MatchResult.notMatch());
    }

    private static Pattern getPattern(String regex) {
        return PATTERNS.computeIfAbsent(regex, r -> Pattern.compile(r));
    }
}

然后,您可以使用自己的matcher代替pathMatchers。

代码语言:javascript
复制
.matchers(exchange -> RegexRequestMatcher.matches(".*/health", exchange)).permitAll()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67012174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档