当前位置: 首页 > news >正文

spring 2.7.14 cors 设置 allowedOrigins(“*“)通配符 失效怎么解决

失效代码:

package com.yukuanyan.searcher_web.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*").allowCredentials(true).maxAge(3600);}
}

失效原因:

allowCredentials(true)被设置成了ture,说明浏览器应该在请求时携带凭证(credential),如果使用*作为通配符会导致安全问题,所以会导致通配符
allowedOrigins("*")
失效

 解决方法:
将allowCredentials()设置为false

package com.yukuanyan.searcher_web.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*")// allowOrigins 使用通配符时需要设置为false.allowCredentials(false).maxAge(3600);}
}

http://www.lryc.cn/news/123234.html

相关文章:

  • 一、Go的前景与优势、基础语法
  • shell脚本循环语句
  • 二叉树题目:二叉树的直径
  • 嵌入式:C高级 Day4
  • cmake常用命令(1)——函数相关
  • 阿里三年功能测试的一些感悟
  • React源码解析18(4)------ completeWork的工作流程【mount】
  • Kafka: 详解、使用教程和示例
  • 【LeetCode周赛】LeetCode第358场周赛
  • Node.js学习笔记-04
  • 基于dbn+svr的交通流量预测,dbn详细原理
  • 【第一阶段】kotlin中反引号中的函数名特点
  • 数据分析-python学习 (1)numpy相关
  • 数据库的游标
  • 【设计模式】前端控制器模式
  • SQL | 过滤数据
  • 【力扣每日一题】2023.8.13 合并两个有序数组
  • 数据结构篇七:排序
  • Vue组件的边界情况
  • less、sass的使用及其区别
  • [保研/考研机试] 猫狗收容所 C++实现
  • Kotlin 基础教程一
  • 数据结构笔记--前缀树的实现
  • C/C++时间获取函数
  • sql中判断日期是否是同一天
  • NAS搭建指南一——服务器的选择与搭建
  • 豪越HYDO智能运维助力智慧医院信息化建设
  • Week1题目重刷
  • 考研数据结构:第七章 查找
  • 【Linux进程篇】环境变量