当前位置:网站首页>关于已拦截跨源请求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);
}
}
边栏推荐
- 安装mysql报错处理
- ABAP file operations involved in the Chinese character set of problems and solutions for trying to read
- 1W字详解线程本地存储 ThreadLocal
- 一汽奥迪:持续34年聚焦品质与体验 立足市场需求推进产品迭代
- PEST 分析法
- ES5和SE6来实现一个Promise效果
- Existing in the rain of PFAS chemical poses a threat to the safety of drinking water
- [219] The training course notes of the go engineer with more than 3,000 MOOCs 02 Programming ideas in the go language
- 锂电池技术
- How does vue clear the tab switching cache problem?
猜你喜欢
1W字详解线程本地存储 ThreadLocal
图式图例规范尺寸
如何完成新媒体产品策划?
Short read or OOM loading DB. Unrecoverable error, aborting now
使用决策树对鸢尾花进行分类
2022-08-09: What does the following go code output?A: No, it will panic; B: Yes, it can run correctly; C: Not sure, see the voting result.package main import (“fmt“ “syn
Cloud Migration Practice of Redis
R语言实战应用案例:论文篇(一)-特殊柱形图绘制
发送post请求前台无法获取数据
第三方软件测评有什么作用?权威软件检测机构推荐
随机推荐
【JS高级】ES5标准规范之创建子对象以及替换this_10
【POI 2008, BLO】Cut Point
Borg Maze (bfs+最小生成树)
普林斯顿微积分读本05第四章--求解多项式的极限问题
FPN详解
YTU 2295: KMP pattern match one (string)
MySQL interview questions
PHP 判断文件是否有内容,没有内容则复制另一个文件写入
【剑指offer】---数组中的重复数字
Drive IT Modernization with Low Code
缺少比较器,运放来救场!(运放当做比较器电路记录)
Matrix Keyboard & Calculator Small Project Based on 51 (UcosII)
写不完的数学试卷-----试卷生成器(Qt含源码)
Fragment's show and hide
需要被记录的OpenStack相关的命令_自己用
记录几道整型提升的题目
池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!
sql语句 异常 Err] 1064 – You have an error in your SQL syntax; check the manual that corresponds to your
List集合
Send a post request at the front desk can't get the data