SpringBoot工具库:解决SpringBoot2.*版本跨域问题

文章介绍了在Spring框架中处理跨域问题的不同方法,包括CorsConfig的配置、CorsFilter和拦截器的应用,以及如何通过Nginx和WebFilter进行跨域管理。特别强调了`allowedOrigins`和`allowedOriginPatterns`的区别以及在allowCredentials为true时的处理。

1.解决问题:When allowCredentials is true, xxxxxxx , using “allowedOriginPatterns“ instead

2.3版本跨域配置如下

/**
 * 跨域问题解决
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600)
                .allowCredentials(true);
    }
}

1.1.解决方法:

        Spring官网有类似问题:https://github.com/spring-projects/spring-framework/issues/26111

        大致意思为:提供了allowedOriginPatterns方法供使用。原本的allowCredentials为true时,allowedOrigins不能使用 * 号匹配

/**
 * 跨域问题解决
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600)
                .allowCredentials(true);
    }
}

其实解决跨域大分类有2种方式,一种是利用注解,一种写个配置类 

2.解决跨域的几种方式

2.1.CorsFilter 方式设置跨域

       &

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值