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

在CORS spring security webFlux中启用通配符

寿子轩
2023-03-14

我将Spring SecurityCORS启用到一个使用spring webFlux制作的项目中。我的问题是,例如,我们接受来自以下方面的请求:http://localhost:4200.我如何使CORS接受来自http的REQ://*。本地主机:4200 likehttp://a.localhost:4200, http://b.localhost:4200 ?

我的CORS配置如下所示:

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public CorsWebFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);

    config.setAllowedOrigins(corsConfigData.getAllowedOrigins());
    config.setAllowedHeaders(corsConfigData.getAllowedHeaders());
    config.setAllowedMethods(corsConfigData.getAllowedMethods());

    source.registerCorsConfiguration("/**", config);
    return new CorsWebFilter(source);
}

你有什么想法吗???

共有2个答案

樊飞飙
2023-03-14

我认为,正如在回答这个问题时所指出的,CORS规范不允许通配符子域。具体见https://www.w3.org/TR/cors/#access-控件允许原始响应标头

您可以按照他们关于这个答案的建议,将处理转移到一些中间件层,如NGINX或Apache,这些中间件层可以根据请求中的域动态设置CORS头,或者在Spring Boot配置中指定您想要的所有子域,如果这还没有达到无法管理的数量。

尽管如此,在问题的第一部分,您声明您接受来自http://localhost:4200.如果你不需要子域,这应该不是问题,那么你可以明确地将那一个域列为白名单,或者我误解了吗?

郝永思
2023-03-14

我想我找到了一个有效的解决方案。这仅仅意味着创建一个自定义的CorsConfiguration,覆盖Check Origin方法并创建一个自定义匹配器来解释超文本传输协议://*。localhost:4200正确的。代码如下所示:

public class RegexCorsConfiguration extends CorsConfiguration {

private List<String> allowedOriginsRegexes = new ArrayList<>();

/**
 * Check the origin of the request against the configured allowed origins.
 * @param requestOrigin the origin to check
 * @return the origin to use for the response, possibly {@code null} which
 * means the request origin is not allowed
 */
public String checkOrigin(String requestOrigin) {
    if (!StringUtils.hasText(requestOrigin)) {
        return null;
    }

    if (this.allowedOriginsRegexes.isEmpty()) {
        return null;
    }

    if (this.allowedOriginsRegexes.contains(ALL)) {
        if (getAllowCredentials() != Boolean.TRUE) {
            return ALL;
        } else {
            return requestOrigin;
        }
    }

    for (String allowedOriginRegex : this.allowedOriginsRegexes) {
        if (createMatcher(requestOrigin, allowedOriginRegex).matches()) {
            return requestOrigin;
        }
    }

    return null;
}

public void setAllowedOriginRegex(List<String> allowedOriginsRegexes) {
    this.allowedOriginsRegexes = allowedOriginsRegexes;
}

private Matcher createMatcher(String origin, String allowedOrigin) {
    String regex = this.parseAllowedWildcardOriginToRegex(allowedOrigin);
    Pattern pattern = Pattern.compile(regex);
    return pattern.matcher(origin);
}

private String parseAllowedWildcardOriginToRegex(String allowedOrigin) {
    String regex = allowedOrigin.replace(".", "\\.");
    return regex.replace("*", ".*");
}}

当然,从如下配置类中注入corsConfig:

    @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public CorsWebFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    RegexCorsConfiguration regexCorsConfiguration = new RegexCorsConfiguration();
    regexCorsConfiguration.setAllowCredentials(true);

    regexCorsConfiguration.setAllowedOriginRegex(corsConfigData.getAllowedOrigins());
    regexCorsConfiguration.setAllowedHeaders(corsConfigData.getAllowedHeaders());
    regexCorsConfiguration.setAllowedMethods(corsConfigData.getAllowedMethods());

    source.registerCorsConfiguration("/**", regexCorsConfiguration);
    return new CorsWebFilter(source);
}
 类似资料:
  • 问题内容: 我的mysql数据库中的表名是动态生成的。有什么方法可以从名称与模式匹配的表中选择数据?我想它将看起来像: 问题答案: 不,您不能使用MySQL做到这一点。无法动态指定查询中的表-您必须在应用程序中构建列表(或执行多个单表查询)。

  • 问题内容: 使用Type通配符的Update()也存在该问题,但是我发现DocumentExists()的作用相同,因此在此将问题简化如下: 这有效… 但这失败了 如果我完全省略Type,它也会失败。有人知道如何进行这项工作吗?(即使不管文档的类型如何都可以,对我而言还是可以的。) 问题答案: 据我所知,不可能在类型名称中指定通配符,但是您可以做一些技巧。 您可以在索引中查询具有特定ID的文档,并

  • 问题内容: 我正在运行应该执行方法的Shell脚本: 在这一点上,我得到这个异常: 这是因为弹簧罐位于另一个文件夹中。所以我更改了脚本: 但是,使用此脚本无法找到com.example.ClassTest。关于这个问题有什么想法吗? 提前致谢 问题答案: Java类路径通配符扩展不常见。 从文档: 了解类路径通配符 类路径条目可以包含基本名称通配符,这被认为等效于指定目录中所有扩展名为.jar或.

  • 问题内容: 我有一个具有属性名称和姓氏的用户对象。我想使用一个查询来搜索这些字段,并且在文档中找到了该字段,但是我不知道如何将其与通配符一起正确使用。可能吗? 我尝试了查询,但没有成功: 问题答案: 或者,您可以对通配符使用查询。 这将比在索引时使用nGram过滤器慢(请参阅我的其他答案),但是如果您正在寻找一种快速且肮脏的解决方案… 我也不确定您的映射,但是如果您使用而不是映射,则需要如下所示:

  • 问题内容: 在Oracle中使用SQL运算符时,如何转义通配符(和)? 我今天遇到一个愚蠢的问题。我需要使用来搜索varchar列上是否存在下划线。正如预期的那样,它不起作用- 因为根据SQL,下划线是通配符。这是我的(简化)代码: 我在PostgreSQL中尝试了以下查询,它们返回了我想要的行: 不幸的是,它在Oracle 12c中不起作用。是否有“标准”的转义通配符方式?还是至少在Oracle