springcloud gateway解决跨域问题
发布日期:2021-09-18 00:51:53 浏览次数:11 分类:技术文章

本文共 1337 字,大约阅读时间需要 4 分钟。

因为后端服务是springcloud gateway项目

参考博客:https://www.jianshu.com/p/a46e62f9ad1c
官文:https://docs.spring.io/spring-security/site/docs/5.2.0.RC1/reference/htmlsingle/#csrf
最终也解决了问题,代码如下:

@Configurationpublic class GwCorsFilter {    @Bean    public CorsWebFilter corsFilter() {        CorsConfiguration config = new CorsConfiguration();//        config.addAllowedMethod("*");//        config.addAllowedOrigin("*");//        config.addAllowedHeader("*");        config.setAllowCredentials(true); // 允许cookies跨域        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部        config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了        config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许        config.addAllowedMethod("HEAD");        config.addAllowedMethod("GET");        config.addAllowedMethod("PUT");        config.addAllowedMethod("POST");        config.addAllowedMethod("DELETE");        config.addAllowedMethod("PATCH");        org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =                new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource(new PathPatternParser());        source.registerCorsConfiguration("/**", config);        return new CorsWebFilter(source);    }}

 

转载地址:https://blog.csdn.net/yucaifu1989/article/details/105731319 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SpringCloudApiGateway之支持Cors跨域请求
下一篇:Spring Cloud Gateway重试机制

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月02日 06时54分03秒