当前位置: 首页 > 知识库问答 >
问题:

没有上下文根路径的spring-webflux安全中的白名单endpoint

常培
2023-03-14

我试图将所有以 /health结尾的自定义endpoint列入白名单,但在WebFlux Security中,我无法使用正则表达式来匹配所有endpoint。例如 /app/health、 /app/meta/health、 /app/custom/meta/health、 /api/app/custom/meta/health等。在构建SecurityWebFilterChain时,我找不到任何模式(或正则表达式)用一个条目将这些endpoint列入白名单。如果我尝试使用正则表达式,我也会收到警告

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.

下面是代码片段

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();
}

我可以通过提供antMatcher在Spring Security(不是反应性/Web流量安全)中实现相同的功能

@Override
public void configure(WebSecurity web) {
   web.ignoring().antMatchers("/**/health");
}

注意:这不是spring执行器健康endpoint,而是自定义健康endpoint

共有1个答案

佟涵畅
2023-03-14

你可以写你自己的匹配器。


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));
    }
}

然后,您可以使用自己的匹配器,而不是路径匹配器。

.matchers(exchange -> RegexRequestMatcher.matches(".*/health", exchange)).permitAll()
 类似资料:
  • 我是一个Spring新手,正在制作一个Spring Web应用程序(不是Spring-boot,这有多大区别?)。部署在Tomcat7服务器上。 应用程序已启动并运行。我的问题是只能通过标准URL访问: http://mycompany.com:8081/cwing-0.0.3-snapshot/index.html 以下操作不起作用:http://mycompany.com:8081/cwing

  • 我正在使用application.properties文件中指定的Spring Boot上下文路径,它工作得很好 Spring Boot 2.0及以上版本

  • 我使用Spring启动与网络通量和删除嵌入的tomcat依赖从初学者网络,我想添加基本上下文路径为我的应用程序,有什么办法我可以做??我需要这个,因为我有在kubernetes集群和重定向基于上下文路径的ingrees属性。

  • 我有一个Spring Boot应用程序,我将把它作为.war文件部署在现有的Tomcat和Undertow(Wildfly)容器中。如何从应用程序内部配置上下文路径? 我知道我可以直接使用.war名称,但我不想这样做,因为.war名称包含版本信息等。

  • 问题内容: 使用上下文路径时,Spring Boot中的静态内容出现问题。即:我希望将我的应用程序部署到 当我在没有上下文路径的情况下运行应用程序时,一切正常,Spring Boot会找到并运行我的文件(来自资源/ templates /,我正在使用)和JS文件(来自资源/ static / js /),但是当我添加上下文路径时与: 要么 然后这些页面仍由JS文件显示,产生 404错误 。 我已经

  • 当我在没有上下文路径的情况下运行应用程序时,一切都运行得很好,并且Spring Boot查找并运行我的文件(来自resources/templates/,我使用)和JS文件(来自resources/static/JS/),但是当我添加上下文路径时: 或 则页面仍然显示由JS文件生成的404个错误。 我尝试更改中的和我的中重写方法,但似乎都不起作用 我使用类,因为我需要定义一个和一个,但这是我在中所