Cors跨域不生效的问题:No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

Cors跨域不生效:前端:No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

这个问题是由于springboot 版本问题导致的(升级之后)

控制台:When allowCredentials is true, allowedOrigins cannot contain the special value since that cannot
   @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 设置支持cookie跨域
        corsConfiguration.setAllowCredentials(true);

        // 设置允许哪些域访问
        List<String> allowOrigins = new ArrayList<>();
        allowOrigins.add("*");
        
        // 注意 !!!!  将setAllowedOrigin 改为 setAllowedOriginPatterns 
        // corsConfiguration.setAllowedOrigin(allowOrigins);
        corsConfiguration.setAllowedOriginPatterns(allowOrigins);

        // 设置允许的头
        List<String> allowHeaders = new ArrayList<>();
        allowHeaders.add("*");
        corsConfiguration.setAllowedHeaders(allowHeaders);

        // 设置允许的请求方式
        List<String> allowMethod = new ArrayList<>();
        allowMethod.add("GET");
        allowMethod.add("POST");
        allowMethod.add("PUT");
        allowMethod.add("PATCH");
        allowMethod.add("DELETE");
        corsConfiguration.setAllowedMethods(allowMethod);

        corsConfiguration.setMaxAge(ALLOW_TIME);
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);

或者配置类直接实现 WebMvcConfigurer 接口 重写

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
        .allowedOriginPatterns("*")
        .allowedMethods("GET")
        .allowCredentials(Boolean.TRUE)
        .maxAge(ALLOW_TIME)
        .allowedHeaders("*");
    }

参考博客 :https://blog.csdn.net/threelifeadv/article/details/122453859?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-122453859-blog-124937330.pc_relevant_layerdownloadsortv1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-122453859-blog-124937330.pc_relevant_layerdownloadsortv1&utm_relevant_index=5

https://zhanjq.blog.csdn.net/article/details/123353689?spm=1001.2101.3001.6650.9&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-9-123353689-blog-122453859.pc_relevant_layerdownloadsortv1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-9-123353689-blog-122453859.pc_relevant_layerdownloadsortv1&utm_relevant_index=10

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值