当前位置:网站首页>6-接口跨域处理
6-接口跨域处理
2022-08-03 00:34:00 【张 邵】
SpringBoot使用CORS解决跨域
1.使用@CrossOrigin
可以在支持跨域的方法上或者是Controller上加上@CrossOrigin注解

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
@Autowired
private UserServcie userServcie;
@RequestMapping("/findAll")
public ResponseResult findAll(){
//调用service查询数据 ,进行返回
List<User> users = userServcie.findAll();
return new ResponseResult(200,users);
}
}
2.使用 WebMvcConfigurer 的 addCorsMappings 方法配置CorsInterceptor
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路径
registry.addMapping("/**")
// 设置允许跨域请求的域名
.allowedOriginPatterns("*")
// 是否允许cookie
.allowCredentials(true)
// 设置允许的请求方式
.allowedMethods("GET", "POST", "DELETE", "PUT")
// 设置允许的header属性
.allowedHeaders("*")
// 跨域允许时间
.maxAge(3600);
}
}
边栏推荐
- NVM和NRM
- 开源聚力,共创未来 | 麒麟信安祝贺openKylin首个体验版正式发布!
- 优秀论文以及思路分析01
- Linear DP
- JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- 淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口代码对接分享
- 如何修复 SAP UI5 aggregation with cardinality 0..1 相关的错误消息
- php一维数组合并
- 精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
猜你喜欢
随机推荐
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第一节:深度学习概论
【SQL】—数据库操作、表操作
提高测试覆盖率的四大步骤
UPC2022暑期个人训练赛第23场(Credit Card Payment)
Nuxt 所有页面都设置上SEO相关标签
增删改查这么多年,最后栽在MySQL的架构设计上!
[NCTF2019]SQLi-1||SQL注入
NLP commonly used Backbone model cheat sheet (1)
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
PAT甲级 1051 Pop Sequence
2022 Shandong International Youth Eye Health Industry Exhibition, Vision Health Exhibition, Optometry Exhibition
向往的生活
担心的事情
Visual Studio中vim模拟器
全栈---Proxy
Oracle 暴跌,倒下了!
MySQL删库不跑路
作业8.2 线程同步互斥机制——互斥锁
letcode 第20题-有效的括号
v-if条件判断及v-show








![[NCTF2019]SQLi-1||SQL注入](/img/18/6483cd9d5d2722860652fea193c13a.png)
