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");
}
}

最新文章

  1. 第三章 Models模块属性详解
  2. webapp开发——‘手机屏幕分辨率’与‘浏览器分辨率’不要混淆
  3. 读写分离提高 SQL Server 并发性
  4. AngularJS高级程序设计读书笔记 -- 指令篇 之 自定义指令
  5. 注释中不允许出现字符串 "--"。
  6. ubuntu Pycharm 2017 3.3 Active
  7. Python 安装第三方库中常见问题总结
  8. MySQL(六)
  9. Unity3d之截图方法
  10. tomcat使用spring-loaded实现应用热部署
  11. [转]JSP中EL表达式三元运算符的使用
  12. MongoVUE破解
  13. SVN使用流程
  14. ansible-playbook快速入门填坑
  15. mybatis左连接需要输出左表的指定内容与筛选
  16. C# OracleHelper
  17. django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统
  18. jetty中war包解压路径
  19. Matlab零碎知识
  20. ssh免密码访问

热门文章

  1. 索引及explain 详解
  2. failed to push some refs to 'git@github.com:cq1415583094/MyBatis.git'解决办法
  3. java的hashCode和equals为什么要同时重写?
  4. 使用vs工具查看dll依赖(也可查看pyc文件的依赖)
  5. Vue首页加载过慢 解决方案
  6. C语言No such file or directory错误
  7. requests与BeautifulSoup
  8. 实体类与数据库字段不匹配问题,java.sql.SQLSyntaxErrorException: Unknown column 'xxx' in 'field list'
  9. 构建docker基本镜像
  10. 小米Air安装Arch Linux之图形界面配置(Gnome 和 sway)持续更新中……