温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot 2.X怎么解决跨域问题

发布时间:2021-07-28 14:14:54 来源:亿速云 阅读:170 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Spring Boot 2.X怎么解决跨域问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、什么是源和跨域

源(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跨域端口号不同

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

Spring Boot 2.X怎么解决跨域问题

二、什么是同源策略?

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

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

  • DOM同源策略:禁止对不同源页面DOM 进行操作。这里主要场景是iframe跨域的情况,不同域名的iframe是限制互相访问的。

  • XMLHttpRequest同源策略:禁止使用XHR对象向不同源的服务器地址发起HTTP请求。

三、Spring Boot跨域解决方案

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

跨域页面:testOtherDomain.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>不同域名-Java碎碎念</title>
</head>
<body>
<button id="b1">点我测试</button>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
 $("#b1").click(function () {
  $.ajax({
   url: "http://localhost:8081/hello",
   type: "post",
   success:function (res) {
    console.log(res);
   }
  })
 });
</script>
</body>
</html>

接口类:HelloController

package com.example.helloSpringBoot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
 @RequestMapping("/hello")
 public String HelloSpring (){
  return "hello Java碎碎念!";
 }
}

未解决跨域前运行截图:

Spring Boot 2.X怎么解决跨域问题

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

1、@CrossOrigin注解示例代码

package com.example.helloSpringBoot.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

 @CrossOrigin
 @RequestMapping("/hello")
 public String HelloSpring (){
  return "hello Java碎碎念!";
 }
}

2. WebMvcConfigurer对象示例代码

package com.example.helloSpringBoot.config;

import org.springframework.context.annotation.Bean;
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 MyConfiguration {
 @Bean
 public WebMvcConfigurer corsConfigurer() {
  return new WebMvcConfigurer() {
   @Override
   public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/*")
       .allowedOrigins("*")
       .allowCredentials(true)
       .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
       .maxAge(3600);
   }
  };
 }
}

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

Spring Boot 2.X怎么解决跨域问题 

关于“Spring Boot 2.X怎么解决跨域问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI