使用 CorsWebFilter 进行 Spring boot 的跨域访问设置

CorsWebFilter 是 Spring Framework 中的一个过滤器,用于处理跨源资源共享(CORS)的请求。CORS 是一种 W3C 规范,它定义了一种浏览器和服务器交互的方式来确定是否允许跨源请求。

在 Spring WebFlux(响应式编程模型)或 Spring MVC(传统的基于 Servlet 的模型)应用中,你可以使用 CorsWebFilter 来配置 CORS 策略。这个过滤器允许你定义哪些源(域、协议和端口)有权访问你的应用资源,哪些 HTTP 方法被允许,哪些头信息可以被包含,以及预检请求的存活时间等。

以下是一个简单的例子,展示了如何在 Spring WebFlux 应用中配置 CorsWebFilter

import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.web.cors.CorsConfiguration;  
import org.springframework.web.cors.reactive.CorsWebFilter;  
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;  
  
@Configuration  
public class CorsConfig {  
  
    @Bean  
    public CorsWebFilter corsWebFilter() {  
        CorsConfiguration config = new CorsConfiguration();  
        config.addAllowedOrigin("http://example.com"); // 允许来自 http://example.com 的请求  
        config.addAllowedMethod("GET"); // 允许 GET 请求方法  
        config.addAllowedMethod("POST"); // 允许 POST 请求方法  
        config.addAllowedHeader("*"); // 允许所有头信息  
        config.setAllowCredentials(true); // 是否允许发送 cookie  
        config.setMaxAge(168000); // 预检请求的有效期,单位为秒  
  
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
        source.registerCorsConfiguration("/**", config); // 对所有路径应用 CORS 配置  
  
        return new CorsWebFilter(source);  
    }  
}

在这个例子中,我们创建了一个 CorsConfiguration 对象,并设置了允许的源、HTTP 方法、头信息以及其他 CORS 相关的设置。然后,我们使用 UrlBasedCorsConfigurationSource 来注册这个配置,使其应用于所有路径("/**")。最后,我们创建并返回了 CorsWebFilter bean,Spring 会将其添加到过滤器链中。

请注意,对于 Spring MVC 应用,你可能需要使用不同的方式来配置 CORS,例如通过实现 WebMvcConfigurer 接口并重写 addCorsMappings 方法。然而,在 Spring WebFlux 中,通常使用 CorsWebFilter

addAllowedOriginPattern 是 CorsConfiguration 类中的一个方法,用于在 Spring Framework 的跨源资源共享(CORS)配置中设置允许进行跨域请求的源的模式。这个方法接受一个字符串参数,该字符串参数是一个正则表达式,用于匹配允许进行跨域请求的源。

使用 addAllowedOriginPattern 方法,你可以更灵活地控制哪些源可以访问你的应用资源。与 addAllowedOrigin 方法不同,addAllowedOriginPattern 允许你使用正则表达式来匹配多个源,而不是只设置单个具体的源。

以下是一个使用 addAllowedOriginPattern 方法的示例:

import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.web.cors.CorsConfiguration;  
import org.springframework.web.cors.reactive.CorsWebFilter;  
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;  
  
@Configuration  
public class CorsConfig {  
  
    @Bean  
    public CorsWebFilter corsWebFilter() {  
        CorsConfiguration corsConfiguration = new CorsConfiguration();  
          
        // 使用正则表达式允许所有以 .example.com 结尾的源进行跨域请求  
        corsConfiguration.addAllowedOriginPattern(".*\\.example\\.com$");  
          
        // 允许所有请求方法  
        corsConfiguration.addAllowedMethod("*");  
          
        // 允许所有请求头  
        corsConfiguration.addAllowedHeader("*");  
          
        // 是否允许携带凭证(如 cookies)  
        corsConfiguration.setAllowCredentials(true);  
          
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
        source.registerCorsConfiguration("/**", corsConfiguration); // 对所有路径应用 CORS 配置  
          
        return new CorsWebFilter(source);  
    }  
}

在这个例子中,我们设置了一个正则表达式 ".*\\.example\\.com$",它匹配所有以 .example.com 结尾的源。这样,任何符合这个模式的源都可以进行跨域请求。

请注意,当你使用 addAllowedOriginPattern 方法时,应该谨慎地编写正则表达式,以确保不会意外地允许不受信任的源进行跨域请求,从而可能导致安全风险。

UrlBasedCorsConfigurationSource 是 Spring Framework 中用于根据请求的 URL 获取 CORS(跨域资源共享)配置信息的类。它允许你基于不同的 URL 路径模式来定义不同的 CORS 配置。这对于需要根据不同路径或模式应用不同安全策略的场景非常有用。

使用 UrlBasedCorsConfigurationSource,你可以为应用中的不同部分定义不同的 CORS 设置。例如,你可能希望允许某些路径的跨域请求携带凭证(如 cookies),而其他路径则不允许。或者,你可能希望为不同的路径设置不同的允许源列表。

下面是一个简单的示例,展示了如何使用 UrlBasedCorsConfigurationSource 来配置 CORS:

import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.web.cors.CorsConfiguration;  
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;  
import org.springframework.web.filter.CorsFilter;  
  
@Configuration  
public class CorsConfig {  
  
    @Bean  
    public CorsFilter corsFilter() {  
        CorsConfiguration corsConfiguration = new CorsConfiguration();  
        corsConfiguration.addAllowedOrigin("http://example.com");  
        corsConfiguration.addAllowedMethod("GET", "POST");  
        corsConfiguration.addAllowedHeader("*");  
        corsConfiguration.setAllowCredentials(true);  
  
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
        source.registerCorsConfiguration("/api/**", corsConfiguration); // 对 /api/ 下的所有路径应用 CORS 配置  
  
        return new CorsFilter(source);  
    }  
}

在这个例子中,我们创建了一个 CorsConfiguration 对象,并设置了允许的源、HTTP 方法、头信息以及是否允许携带凭证。然后,我们使用 UrlBasedCorsConfigurationSource 来注册这个配置,使其仅应用于以 /api/ 开头的路径。最后,我们创建并返回了一个 CorsFilter bean,Spring 会将其添加到过滤器链中。

通过使用 UrlBasedCorsConfigurationSource,你可以更细粒度地控制哪些 URL 路径应该允许跨域请求,以及这些请求应该遵循哪些 CORS 规则。这有助于确保你的应用的安全性,同时仍然允许合法的跨域交互。

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因上精进,果上随缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值