博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
跨域问题
阅读量:3914 次
发布时间:2019-05-23

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

在这里插入图片描述

package com.course.server.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class CorsConfig implements WebMvcConfigurer {
@Override public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders(CorsConfiguration.ALL) .allowedMethods(CorsConfiguration.ALL) .allowCredentials(true) .maxAge(3600); // 1小时内不需要再预检(发OPTIONS请求) }}

这里老师用的是.allowedOrigins("*")

然后报错了,改成用.allowedOriginPatterns("*")就可以了
在这里插入图片描述
如果用的是路由转发gateway的话,可以在gateway的启动文件GatewayApplication.java里这样写

package com.course.gateway;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.core.env.Environment;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.reactive.CorsWebFilter;import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;import org.springframework.web.util.pattern.PathPatternParser;@SpringBootApplication@EnableEurekaClientpublic class GatewayApplication {
private static final Logger LOG = LoggerFactory.getLogger(GatewayApplication.class); public static void main(String[] args) {
SpringApplication app = new SpringApplication(GatewayApplication.class); Environment env = app.run(args).getEnvironment(); LOG.info("启动成功!!"); LOG.info("Gateway地址: \thttp://127.0.0.1:{}", env.getProperty("server.port")); } /** * 配置跨域 * @return */ @Bean public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(Boolean.TRUE); config.addAllowedMethod("*"); config.addAllowedOriginPattern("*"); config.addAllowedHeader("*"); config.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); source.registerCorsConfiguration("/**", config); return new CorsWebFilter(source); }}

转载地址:http://eavrn.baihongyu.com/

你可能感兴趣的文章
【实战 Ids4】║ 在Swagger中调试认证授权中心
查看>>
.NET Core开发实战(第10课:环境变量配置提供程序)--学习笔记
查看>>
WTM系列视频教程:View和Taghelper
查看>>
面试官:你连HTTP请求Post和Get都不了解?
查看>>
.NET Core 3.0 即将结束生命周期,建议迁移 3.1
查看>>
开源、免费、企业级的SiteServer CMS .NET CORE 7.0 预览版发布
查看>>
基于.NET下的人工智能|利用ICSharpCore搭建基于.NET Core的机器学习和深度学习的本地开发环境...
查看>>
【朝夕Net社区技术专刊】Core3.1 WebApi集群实战专题---WebApi环境搭建运行发布部署篇...
查看>>
200行代码,7个对象——让你了解ASP.NET Core框架的本质[3.x版]
查看>>
.NET Core开发实战(第21课:中间件:掌控请求处理过程的关键)--学习笔记(下)...
查看>>
对比Java和.NET多线程编程
查看>>
[头脑风暴] 解读Docker Bridge网络模型
查看>>
集成平台集群任务动态分派
查看>>
【.net core】电商平台升级之微服务架构应用实战
查看>>
【翻译】.NET 5 Preview 1 发布
查看>>
使用GUI工具Portainer.io管控Docker容器
查看>>
Abp vNext发布v2.3!
查看>>
.NET Core开发实战(第27课:定义Entity:区分领域模型的内在逻辑和外在行为)--学习笔记...
查看>>
BeetleX之vue-autoui自匹配UI插件
查看>>
.NET Core开发实战(第28课:工作单元模式(UnitOfWork):管理好你的事务)--学习笔记...
查看>>