经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot 2.X优雅的解决跨域问题
来源:jb51  时间:2019/3/21 8:32:52  对本文有异议

一、什么是源和跨域

源(origin)就是协议、域名和端口号。

URL由协议、域名、端口和路径组成,如果两个URL的协议、域名和端口全部相同,则表示他们同源。否则,只要协议、域名、端口有任何一个不同,就是跨域。

对https://www.baidu.com/index.html进行跨域比较:

URL 是否跨域 原因
https://www.baidu.com/more/index.html 不跨域 三要素相同
https://map.baidu.com/ 跨域 域名不同
http://www.baidu.com/index.html 跨域 协议不同
https://www.baidu.com:81/index.html 跨域 端口号不同

随着前后端分离开发的越来越普及,会经常遇到跨域的问题,当我们在浏览器中看到这样的错误时,就需要意识到遇到了跨域:

二、什么是同源策略?

同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。

同源策略又分为以下两种:

  • DOM同源策略:禁止对不同源页面DOM 进行操作。这里主要场景是iframe跨域的情况,不同域名的iframe是限制互相访问的。
  • XMLHttpRequest同源策略:禁止使用XHR对象向不同源的服务器地址发起HTTP请求。

三、Spring Boot跨域解决方案

本例使用Spring Boot 2.1.2.RELEASE演示,分别用8080和8081端口启动,部分代码如下:

跨域页面:testOtherDomain.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>不同域名-Java碎碎念</title>
  6. </head>
  7. <body>
  8. <button id="b1">点我测试</button>
  9. <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
  10. <script>
  11. $("#b1").click(function () {
  12. $.ajax({
  13. url: "http://localhost:8081/hello",
  14. type: "post",
  15. success:function (res) {
  16. console.log(res);
  17. }
  18. })
  19. });
  20. </script>
  21. </body>
  22. </html>

接口类:HelloController

  1. package com.example.helloSpringBoot.controller;
  2.  
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5.  
  6. @RestController
  7. public class HelloController {
  8. @RequestMapping("/hello")
  9. public String HelloSpring (){
  10. return "hello Java碎碎念!";
  11. }
  12. }

未解决跨域前运行截图:

在Spring Boot 2.X应用程序中可以使用注解@CrossOrigin,也可以通过使用WebMvcConfigurer对象来定义全局CORS配置。

1、@CrossOrigin注解示例代码

  1. package com.example.helloSpringBoot.controller;
  2.  
  3. import org.springframework.web.bind.annotation.CrossOrigin;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. @RestController
  8. public class HelloController {
  9.  
  10. @CrossOrigin
  11. @RequestMapping("/hello")
  12. public String HelloSpring (){
  13. return "hello Java碎碎念!";
  14. }
  15. }

2. WebMvcConfigurer对象示例代码

  1. package com.example.helloSpringBoot.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7.  
  8. @Configuration
  9. public class MyConfiguration {
  10. @Bean
  11. public WebMvcConfigurer corsConfigurer() {
  12. return new WebMvcConfigurer() {
  13. @Override
  14. public void addCorsMappings(CorsRegistry registry) {
  15. registry.addMapping("/*")
  16. .allowedOrigins("*")
  17. .allowCredentials(true)
  18. .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
  19. .maxAge(3600);
  20. }
  21. };
  22. }
  23. }

按照上面两种方式的一种配置完成后,即可实现对跨域的支持,运行成功截图如下:

 

完整源码地址:https://github.com/suisui2019/helloSpringBoot (本地下载)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号