springboot项目跨域设置总结

方式一:

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许跨域的路径
                .allowedOrigins("*") // 允许跨域请求的域名
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(true); // 是否允许证书(cookies)
    }
}

方式二:

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

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

方式三:

@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 允许携带凭据
        config.setAllowCredentials(true);

        // 使用模式匹配允许源
        List<String> allowedOriginPatterns = Arrays.asList("*");
        config.setAllowedOriginPatterns(allowedOriginPatterns);

        // 其他配置,如允许的方法、头部等
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");

        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }
}

方式四:(Nginx解决跨域问题)

location / {
    # 允许跨域的请求,可以自定义变量$http_origin,*表示所有
    add_header 'Access-Control-Allow-Origin' *;
    # 允许携带cookie请求
    add_header 'Access-Control-Allow-Credentials' 'true';
    # 允许跨域请求的方法:GET,POST,OPTIONS,PUT
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT';
    # 允许请求时携带的头部信息,*表示所有
    add_header 'Access-Control-Allow-Headers' *;
    # 允许发送按段获取资源的请求
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    # 一定要有!!!否则Post请求无法进行跨域!
    # 在发送Post跨域请求前,会以Options方式发送预检请求,服务器接受时才会正式请求
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        # 对于Options方式的请求返回204,表示接受跨域请求
        return 204;
    }
}

在后端项目中,还可以通过继承HandlerInterceptorAdapter类、添加@CrossOrgin注解的方式实现接口之间的跨域配置。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值