在 CORS spring security + webFlux 中启用通配符

2023-12-25

我在使用 spring webFlux 制作的项目中启用了 spring security + CORS。我的问题是我们接受来自以下机构的请求:http://本地主机:4200 http://localhost:4200。我怎样才能让 CORS 接受来自的请求http://*.localhost:4200 like http://a.localhost:4200 http://a.localhost:4200, http://b.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);
}

你有什么想法 ???


我想我找到了一个有效的解决方案。这仅仅意味着创建一个自定义 CorsConfiguration,覆盖 checkOrigin 方法并创建一个将解释的自定义匹配器http://*.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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 CORS spring security + webFlux 中启用通配符 的相关文章

随机推荐

  • 推送到 git 存储库子文件夹上的远程源?

    我有一个 git 存储库 它使用 Vagrant 为 WordPress 项目构建虚拟服务器 仅限本地开发 文件夹结构大致如下 出于问题的目的 Vagrantfile puppet wordpress www public folder r
  • facebook“发送”按钮打开一个空白框

    我的网站是 alonsart com 当单击我网站上的发送按钮时 它会打开一个空白框 这就是我的代码的样子 div div
  • 混合混合模式在应用于一个元素但不适用于另一元素时起作用

    我在用mix blend mode在 css 生成的内容上创建倍增的背景效果 当我将这个生成的元素应用到外部包装器时 它具有预期的效果 standard cover background blue color fff position ab
  • 如何浅拉按分支名称跟踪的子模块

    您好 我有一个包含子模块的超级项目 子模块通过分支名称而不是 sha 提交号进行跟踪 在我们的构建服务器上 我想尽可能少地拉动 所以我尝试了 git submodule update remote init 然而这并不肤浅 似乎拉出所有内容
  • IteratorGetNext 上的 TensorFlow 性能瓶颈

    在摆弄 TensorFlow 时 我注意到一个相对简单的任务 批处理一些 3D 加速度计数据并获取每个周期的总和 的性能相对较差 一旦我得到了 非常漂亮 这就是我所运行的本质 Timeline https stackoverflow com
  • 为什么 F5 在 Visual Studio 中执行之前不重建项目?

    If I press F5 my project runs but it doesn t see any of the changes that I made I need to manually re build before press
  • 如何将一个库静态链接到另一个静态库?

    我有 2 个具有不同版本 MinGW 的构建环境 一种为 Qt 配置 另一种则没有 然而 两者都有 Qt 的存根静态库 最终实际上链接到 dll 问题是我想在Qt MinGW中构建一个静态库 然后将其包含在非Qt MinGW中 当我尝试时
  • 如何实现logstash配置的单元或集成测试?

    使用logstash 1 2 1 人们现在可以有条件地做各种事情 如果管理许多日志文件并实现指标提取 即使是早期版本的 conf 文件也会变得复杂 看完之后这个全面的 http untergeek com 2013 09 11 gettin
  • 如何按最小增量(或接近最小增量)改变浮点数?

    我有一个double value f并希望有一种方法将其稍微放大 或缩小 以获得一个尽可能接近原始值但仍严格大于 或小于 原始值的新值 它不必接近最后一位 更重要的是 我所做的任何更改都保证产生不同的值 而不是舍入到原始值 检查你的 mat
  • jQuery DatePicker 填充输入 - 多次点击问题

    所以 我最近面临的困境是 我使用 jQuery DatePick 不是 DatePicker 来选择日期并用这些日期填充输入字段 问题是每次我单击日期时输入字段都会被填充 因此 如果我单击第 23 个 然后再次单击它以取消选择它 它会自动添
  • 正则表达式重用一个模式来捕获多个组?

    我想多次匹配某个模式 就像描述的那样here https stackoverflow com questions 41878948 is it possible to define a pattern and reuse it to cap
  • 为 NSTextField 设置边框

    哇 我真的掉进兔子洞了 我试图在 UI 部分的背景上添加文本 并将文本字段作为另一部分 例如生日在 然后我想重新调整该文本字段的用途以允许输入文本 所以我做了类似的事情 myTextFieldName editable true myTex
  • 在 Preact 和 typescript 中使用 Web 组件

    我在用着自定义元素 https developer mozilla org en US docs Web Web Components Using custom elements又称为网络组件Preact https preactjs co
  • 如何绘制堆叠和归一化直方图?

    我有一个将连续值映射到离散类别的数据集 我想显示一个直方图 其中连续值作为 x 类别作为 y 其中条形堆叠并标准化 例子 import numpy as np import pandas as pd import matplotlib im
  • 在 Azure 数据工厂查找中处理 >5000 行

    我有一个复制活动 它将表从 MySQL 复制到 Azure 表存储 这很好用 但是当我在 Azure 表上进行查找时 出现错误 数据太多 这是按照文档设计的 Lookup 活动最多可包含 5 000 行 最大大小为 2 MB 另外还提 到了
  • Android:将参数传递给选项卡

    在我的 Android 应用程序中 我使用以下代码来创建选项卡 mTabHost FragmentTabHost findViewById android R id tabhost mTabHost setup this getSuppor
  • 脸书状态栏

    有人知道如何在新 Facebook 中找到状态栏的好教程吗 底部的那个 看起来真的很酷 我以为它是用 ajax 或 jquery 编写的 但不确定 以下是一些可能对您有帮助的有用链接 插件 定位页脚 http plugins jquery
  • 计算向量中元素的所有成对差异[重复]

    这个问题在这里已经有答案了 我的问题与之前的帖子密切相关 计算 R 中向量内的所有成对差异 https stackoverflow com questions 24314878 compute all pairwise difference
  • Rails 3 应用程序中的 Sass 导入错误 - “未找到或无法读取要导入的文件:指南针”

    我有一个成功运行的 Rails 3 应用程序compass init rails using blueprint 我可以 import stylesheets 目录中的文件 但当我尝试时出现错误 import compass 现在该应用程序
  • 在 CORS spring security + webFlux 中启用通配符

    我在使用 spring webFlux 制作的项目中启用了 spring security CORS 我的问题是我们接受来自以下机构的请求 http 本地主机 4200 http localhost 4200 我怎样才能让 CORS 接受来