Springboot 解决跨域请求
2024-09-02 07:33:44
Cors处理 跨域请求
细粒度
直接在controller层上 添加@CrossOrigin注解
@PostMapping("/")
@CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*")
public String addBook(String name) {
System.out.println("receive: " + name);
return "receive: " + name;
}
粗粒度
写一个config类, addMapping 即需要处理的url请求
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 MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/book/**")
.allowedHeaders("*")
.allowedMethods("*")
.maxAge(1800)
.allowedOrigins("http://localhost:8008");
}
}
最新文章
- 第三章 Models模块属性详解
- webapp开发——‘手机屏幕分辨率’与‘浏览器分辨率’不要混淆
- 读写分离提高 SQL Server 并发性
- AngularJS高级程序设计读书笔记 -- 指令篇 之 自定义指令
- 注释中不允许出现字符串 ";--";。
- ubuntu Pycharm 2017 3.3 Active
- Python 安装第三方库中常见问题总结
- MySQL(六)
- Unity3d之截图方法
- tomcat使用spring-loaded实现应用热部署
- [转]JSP中EL表达式三元运算符的使用
- MongoVUE破解
- SVN使用流程
- ansible-playbook快速入门填坑
- mybatis左连接需要输出左表的指定内容与筛选
- C# OracleHelper
- django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统
- jetty中war包解压路径
- Matlab零碎知识
- ssh免密码访问
热门文章
- 索引及explain 详解
- failed to push some refs to 'git@github.com:cq1415583094/MyBatis.git'解决办法
- java的hashCode和equals为什么要同时重写?
- 使用vs工具查看dll依赖(也可查看pyc文件的依赖)
- Vue首页加载过慢 解决方案
- C语言No such file or directory错误
- requests与BeautifulSoup
- 实体类与数据库字段不匹配问题,java.sql.SQLSyntaxErrorException: Unknown column 'xxx' in 'field list'
- 构建docker基本镜像
- 小米Air安装Arch Linux之图形界面配置(Gnome 和 sway)持续更新中……