Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin‘ he
1. @CrossOrigin注解解决跨域
@CrossOrigin(origins = “http://api.gmall.com:8888”) // 实现跨域 要求spring的版本必须4.2以上
如果你用的也是spring框架,并且版本在4.2以上,可以用@CrossOrigin这个注解,
括号中的url可以换成“”*“”,如果这个controller里有多个方法,注解可以直接写在类的最上边。
@RestController
@CrossOrigin(origins = "http://api.gmall.com:8888") // 实现跨域 要求spring的版本必须4.2以上
@RequestMapping("/admin/product")
public class ManagerController {@AutowiredManagerService managerService;@GetMapping("/getCategory1")public Result getCategory1(){List<BaseCategory1> category1List = managerService.getCategory1();return Result.ok(category1List);
// return Result.build(category1List, ResultCodeEnum.SUCCESS);}
}
2.写在类上
//写在类上,不用每个方法都写这个注解了
@CrossOrigin(origins="*",maxAge=3600)
@RestController
public class LoginController {}
3. 1 配置类
package com.kd.gmall.gateway.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;@Configuration
public class CorsConfig {@Beanpublic CorsWebFilter corsWebFilter(){// cors跨域配置对象CorsConfiguration configuration = new CorsConfiguration();configuration.addAllowedOrigin("*");configuration.setAllowCredentials(true);configuration.addAllowedMethod("*");configuration.addAllowedHeader("*");// 配置源对象UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();configurationSource.registerCorsConfiguration("/**", configuration);// cors过滤器对象return new CorsWebFilter(configurationSource);}
}
3.2 application.yml
server:port: 80
spring:application:name: api-gatewaycloud:nacos:discovery:server-addr: 192.168.200.128:8848gateway:discovery: #是否与服务发现组件进行结合,通过 serviceId(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。locator: #路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。enabled: trueroutes:- id: service-producturi: lb://service-productpredicates:- Path=/*/product/** # 路径匹配
3.3配置类目录结构
Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin‘ he相关推荐
- 关于跨域 Response to preflight request doesn‘t pass access control check
做项目的时候由于访问了不同的服务器,然后导致了跨域问题,报错情况为: has been blocked by CORS policy: Response to preflight request do ...
- Response to preflight request doesn‘t pass access control check: The value of the ‘Access-Control-Al
错误:Response to preflight request doesn't pass access control check: The value of the 'Access-Control ...
- GO + React + Axios Response to preflight request doesn't pass access control check: It does not hav
使用Go + Reat 使用 Axios 请求后端, 出现: Access to XMLHttpRequest at 'http://127.0.0.1:20002/v1/user/login' fr ...
- 浏览器预检请求返回400 has been blocked by CORS policy: Response to preflight request doesn’t pass access cont
这个问题也是很过分头一次遇到,原因是谷歌浏览器在有跨域(CORS)请求时,会先发送一个preflight(预检)请求,之后才会发送fetch请求. CORS:跨源资源共享 (CORS)(或通俗地译为跨 ...
- Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin跨域问题
出现问题:在本地运行,有配置vue.config.js中的proxy,所以解决了跨域问题.但将项目打包放到服务器上去,在打包的过程中,proxy是不会被打包进去的,也就是说代理所配置的跨域就已经失效了 ...
- 关于Springboot中跨域问题的解决(Response to preflight request doesn‘t pass access control check)
Springboot中跨域问题的解决 等不及的小伙伴,直接跳到结论部分即可,谢谢!!! 1. 背景 1.1 使用技术栈 Spring Security Springboot Vue.axios Jwt ...
- has been blocked by CORS policy: Response to preflight request doesn‘t pass access control check
has been blocked by CORS policy: Response to preflight request doesn't pass access control check 这个错 ...
- 跨域 Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status.
在登录的时候没有跨域问题, token 认证的时候就跨域了(请求拦截器中配置的请求头携带 token),有点玄学了. 后端没有报错,不过显示请求方法为 OPTION 预检请求.前端报错如下,解决也很简 ...
- vue跨域问题Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Ori
每天一个报错,个各不相同. 看见下面的这行报错是不是感觉跟熟悉? 没错,vue的跨域问题,记录一下我的解决方法并分享给大家 ~ . ~ Response to preflight request do ...
- 跨域Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status.
最近后端添加自定义请求头拦截器后,前端也增加请求头后,解决过程中先后出现两种跨域错误. 一.请求后台出现如下跨域问题: has been blocked by CORS policy: Respons ...
最新文章
- 如何利用ESP8266模块实现远程控制
- KERNELRELEASE
- 设计模式之“适配器模式”
- XAMPP 1.8.2-2 Apache Web Server won't start, always stops immediately
- set和dict理论和操作
- 图论--差分约束系统
- 模板:BSGS(数论)
- NotificationManager: notifyAsUser: tag=null, id=6, user=UserHandle{0}
- SpringBoot2.x整合Activiti7后,禁用SpringBootSecurity问题
- Trained Tesseract on 瘦金体 successfully!!
- web前端期末大作业 html+css+javascript防天天生鲜官网网页设计实例 企业网站制作
- css flex实现经典的三栏布局
- apfs扩容_MacBook Air 2015 换硬盘心得
- 使用Tensorflow实现声纹识别
- uni-app 应用换肤功能
- 离线部署NFS文件系统
- ThinkPad T14、ThinkPadT16 2022款 评测
- 7、Callable接口
- 如何用css3制作一个旋转的立方体
- [OpenGL] 捏脸系统