当前位置:网站首页>关于已拦截跨源请求CORS 头缺少 ‘Access-Control-Allow-Origin‘问题解决
关于已拦截跨源请求CORS 头缺少 ‘Access-Control-Allow-Origin‘问题解决
2022-08-10 13:52:00 【努力且卑微的Ada】
1.报错问题描述
当我用vue+springboot写好后端页面时,我使用redis 用UUID随机存储一个token,登录之后后台会随机生成一个token,然后前端页面拿到这个token用于验证登录权限,这里登录之后发现页面无法显示,并且提示跨域错误。
然后点击网络看具体报错信息,发现请求头已经跨过域,而token显示undefined
后端控制台显示也显示undefined
2.问题解决
在前端vue这里用控制器前端页面显示token,并让它携带数据看看是否为空.。
从控制器可以看出前端页面使用data接受数据显示undefined。
打开后端页面发现后端页面用的是date传输数据,前端和后端对应的名字不一致导致的错误
所以发生跨域错误。
改为data成功访问页面
最后,这里我跨域请求并没有使用@CrossOrigin注解,我直接使用的是跨域工具类。
提供给大家,可以解决大部分的跨域问题。
@Configuration
public class CorsConfig {
// 当前跨域请求最大有效时长。这里默认1天
private static final long MAX_AGE = 24 * 60 * 60;
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
corsConfiguration.setMaxAge(MAX_AGE);
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
边栏推荐
- Send a post request at the front desk can't get the data
- Lithium battery technology
- C# InitializeComponent() does not exist in the current context
- 【219】慕课三千多的那个go工程师的培训课笔记 02 go语言的编程思想
- Borg Maze (bfs+最小生成树)
- laravel throws the error to Dingding
- A can make large data clustering method of 2000 times faster, don't poke
- Error: Rule can only have one resource source (provided resource and test + include + exclude)
- @RequestBody的使用[通俗易懂]
- MySQL - 数据库的存储引擎
猜你喜欢

【学习笔记】Redis的持久化

SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement

Error: Rule can only have one resource source (provided resource and test + include + exclude)

进程和计划任务管理

MySQL - 数据库的存储引擎

【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding

2022年中国软饮料市场洞察

List集合

发送post请求前台无法获取数据

接口自动化测试基础篇
随机推荐
leetcode 739. Daily Temperatures Daily Temperatures (Moderate)
CodeForces - 811A
Cloud Migration Practice of Redis
网络安全——XSS之被我们忽视的Cookie
ICML 2022 | 基于随机注意力机制的可解释可泛化图学习
实现一个深克隆
NAACL 2022 | 简单且高效!随机中间层映射指导的知识蒸馏方法
用低代码驱动IT现代化
Error: Rule can only have one resource source (provided resource and test + include + exclude)
【MinIO】Using tools
numpy.meshgrid()理解
舵机内部结及工作原理浅析[通俗易懂]
山水的高度
Short read or OOM loading DB. Unrecoverable error, aborting now
Redis上云迁移实践
Lithium battery technology
Fragment-hide和show
Calculate the number of combinations recursively
Stream通过findFirst()查找满足条件的一条数据
In the second half of 2012 system architecture designers afternoon paper II