Spring Boot3 系列:Spring Boot3 跨域配置 Cors
什么是CORS?
CORS,全称是“跨源资源共享”(Cross-Origin Resource Sharing),是一种Web应用程序的安全机制,用于控制不同源的资源之间的交互。
在Web应用程序中,CORS定义了一种机制,通过该机制,浏览器能够限制哪些外部网页可以访问来自不同源的资源。源由协议、域名和端口组成。当一个网页请求另一个网页上的资源时,浏览器会检查请求是否符合CORS规范,以确定是否允许该请求。
CORS的工作原理是:当浏览器发送一个跨域请求时,它会附加一些额外的头部信息到请求中,这些头部信息包含了关于请求的来源和目的的信息。服务器可以检查这些头部信息并决定是否允许该请求。如果服务器允许请求,它会返回一个响应,其中包含一个名为“Access-Control-Allow-Origin”的头部信息,该信息指定了哪些源可以访问该资源。浏览器会检查返回的“Access-Control-Allow-Origin”头部信息,以确定是否允许该跨域请求。
通过使用CORS,开发人员可以控制哪些外部网页可以访问他们的资源,从而提高应用程序的安全性。
Spring Boot 如何配置CORS?
Spring Boot对于跨域请求的支持可以通过两种配置方式来实现:
- 注解配置:可以使用@CrossOrigin注解来启用CORS。例如,在需要支持跨域请求的方法上添加@CrossOrigin注解,并配置好origins和maxAge等参数。
- 全局配置:可以通过实现WebMvcConfigurer接口并注册一个WebMvcConfigurer bean来配置CORS的全局设置。在实现类中覆盖addCorsMappings方法,通过CorsRegistry对象添加映射规则。默认情况下,所有方法都支持跨域,并且GET、POST和HEAD请求是被允许的。如果需要自定义,可以配置CorsRegistry对象来指定允许的域名、端口和请求方法等。
- 过滤器配置:可以通过
CorsFilterbean来配置CORS的过滤器。这种方式可以更加灵活地控制CORS的配置,例如只允许特定的域名进行跨域访问等。
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<script>
$.ajax({
url: 'http://localhost:8080/hello',
type: 'GET',
//xhrFields: { withCredentials: true }, //开启认证
success: function (data) {
console.log(data)
}
})
</script>
</body>
</html>
注解配置
@CrossOrigin 是 Spring Framework 中的一个注解,用于处理跨域请求。当一个前端页面尝试从不同的源(域名、端口或协议)请求数据时,浏览器的同源策略会阻止这种请求,以防止潜在的安全问题。然而,在某些情况下,你可能希望允许跨域请求,这时就可以使用 @CrossOrigin 注解。
添加CorssOrigin注解
package com.mcode.springbootcorsdemo.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ClassName: HelloController
* Package: com.mcode.springbootcorsdemo.controller
* Description:
*
* @Author: robin
* @Version: v1.0
*/
@RestController
public class HelloController {
@CrossOrigin(value = "http://127.0.0.1:5500",allowedHeaders = "*",allowCredentials = "true")
@GetMapping("/hello")
public String hello(){
return "Hello";
}
}
属性:
@CrossOrigin 注解有几个属性,允许你更精细地控制跨域行为:
* origins: 允许的源列表,可以是域名、IP 或其他标识符。多个源可以使用逗号分隔。
* methods: 允许的 HTTP 方法列表。例如,只允许 GET 请求。
* allowedHeaders: 允许的请求头列表。默认情况下,允许所有请求头。
* allowCredentials:是否允许配置;值为true、false的字符串
* maxAge: 预检请求的缓存时间(以秒为单位)。默认是 86400 秒(24小时)。这些属性可以根据需要进行组合和配置。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CrossOrigin {
@AliasFor("origins")
String[] value() default {};
@AliasFor("value")
String[] origins() default {};
String[] originPatterns() default {};
String[] allowedHeaders() default {};
String[] exposedHeaders() default {};
RequestMethod[] methods() default {};
String allowCredentials() default "";
long maxAge() default -1L;
}
全局配置
addCorsMappings 是 Spring Boot 中用于配置跨域请求的方法。它允许你指定哪些路径的请求需要进行跨域处理,以及如何处理这些请求。
在 addCorsMappings 方法中,你可以使用 addMapping 方法来指定需要跨域处理的请求路径。例如:
package com.mcode.springbootcorsdemo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* ClassName: MyWebMvcConfigurer
* Package: com.mcode.springbootcorsdemo.config
* Description:
*
* @Author: robin
* @Version: v1.0
*/
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 允许所有请求路径跨域访问
.allowCredentials(true) // 是否携带Cookie,默认false
.allowedHeaders("*") // 允许的请求头类型
.maxAge(3600) // 预检请求的缓存时间(单位:秒)
.allowedMethods("*") // 允许的请求方法类型
.allowedOrigins("http://127.0.0.1:5500"); // 允许哪些域名进行跨域访问
}
}
过滤器配置
在Spring Boot中,CorsFilter用于处理跨域请求。它是一个过滤器,用于在Spring应用程序中启用CORS(跨源资源共享)支持。
package com.mcode.springbootcorsdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* ClassName: CorsConfig
* Package: com.mcode.springbootcorsdemo.config
* Description:
*
* @Author: robin
* @Version: v1.0
*/
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter(){
CorsConfiguration config = new CorsConfiguration();
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.addAllowedOrigin("http://127.0.0.1:5500");
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",config);
return new CorsFilter(source);
}
}
注意事项
当我们没有配置跨域的时候会提示:
Access to XMLHttpRequest at 'http://localhost:8080/hello' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
当我们开启withCredentials:true的时候没有配置allowCredentials为true会提示:
Access to XMLHttpRequest at 'http://localhost:8080/hello' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
当我们在后端配置了allowCredentials(true)那么就不能配置allowedOrigins("*"),必须指定来源
jakarta.servlet.ServletException: Request processing failed: java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
Spring Boot3 系列:Spring Boot3 跨域配置 Cors的更多相关文章
- 从零开始学 Java - Spring MVC 实现跨域资源 CORS 请求
论职业的重要性 问:为什么所有家长都希望自己的孩子成为公务员? 答:体面.有权.有钱又悠闲. 问:为什么所有家长都希望自己的孩子成为律师或医生? 答:体面.有钱.有技能. 问:为什么所有家长都不怎么知 ...
- Java - Spring MVC 实现跨域资源 CORS 请求
拦截器设置响应头 这种方式原理就是利用拦截器在方法执行前,我们增加请求的响应头,用来支持跨域请求.这种方案是可行的,大部分都是采用这种方案.我当时也是打算采用这种方案,直到我发现原来 Spring 框 ...
- Spring MVC 实现跨域资源 CORS 请求
说到 AJAX 跨域,很多人最先想到的是 JSONP.的确,JSONP 我们已经十分熟悉,也使用了多年,从本质上讲,JSONP 的原理是给页面注入一个 <script>,把远程 JavaS ...
- SpringBoot 配置CORS处理前后端分离跨域配置无效问题解析
前言 浏览器有跨域限制,非同源策略(协议.主机名或端口不同)被视为跨域请求,解决跨域有跨域资源共享(CORS).反向代理和 JSONP的方式.本篇通过 SpringBoot 的资源共享配置(CORS) ...
- Nginx配置跨域请求 CORS
当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is present on the requested resource,需要给Nginx服 ...
- Asp.Net Core跨域配置
在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...
- Springboot统一跨域配置
前言:跨域是什么? 要知道跨域的概念,我们先明确怎样算是同一个域: 同一个域指的是同一协议,同一ip,同一端口 如果这三同中有一者不同就产生了跨域. 在做前后端分离的项目中,通过ajax请求后台端口时 ...
- Asp.net跨域配置
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...
- asp.net (webapi) core 2.1 跨域配置
原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...
- .net core api服务端跨域配置
第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...
随机推荐
- 【短道速滑十】非局部均值滤波的指令集优化和加速(针对5*5的搜索特例,可达到单核1080P灰度图 28ms/帧的速度)。
非局部均值滤波(Non Local Means)作为三大最常提起来的去燥和滤波算法之一(双边滤波.非局部均值.BM3D),也是有着很多的论文作为研究和比较的对象,但是也是有着致命的缺点,速度慢,严重的 ...
- linux常见命令(五)
用于文本内容处理的相关命令 sort uniq cut comm diff sort:对文件中数据进行排序,并将结果显示在标准输出上 命令语法:sort [选项] [文件] 选项 选项含义 -m 如果 ...
- 解决 Steam 无法自动登录的问题
前言 劳动节假期闲的没事,重装一下电脑,结果电脑的 Steam 不会自动登录了,每次重启电脑就要重新输入密码和令牌.查了一下居然是 Windows 凭据管理器默认不会自动启动的问题. 解决方法 打开计 ...
- QUERIES
这个题解..u1s1我没看懂,但是我觉得这里面有一个重要的思想就是对于像异或这种最终值只是看一个数位的问题,我们可以考虑分解,把每一个子问题单独解决就可以了其实更难的应该是每个子区间的异或和之和这个方 ...
- js下IE和FF的一些兼容写法总结
一.脚本差异: 1.事件绑定:addEventListener 与 attachEvent 事件处理函数中this指向不同, IE中指向window 2.获取事件对象 :事件处理函数 win ...
- 导出所有容器id号
#!/bin/bash a=`docker ps|awk 'NR>1{print $1}'` FORMAT="%-12s\t,\t%-12s\t,\t%-12s\n" pri ...
- 2023寒鹭Tron-CTF迎新赛 CRYPTO Misc 全WP
CRYPTO 简简单单 1.题目信息 U2FsdGVkX1+2gTXPuTetdM1p+IETUDXAHe2eC33jQfgdJoOmmrJq 2.解题方法 兔子密码,在线工具直接解 简简单单2 1. ...
- `.NET Web`新人入门必学项目`EarthChat`
.NET Web新人入门必学项目EarthChat EarthChat是一个基于.NET 7的实战项目,EarthChat提供了很多的最佳实践,EarthChat的目标也是成为一个很多人都喜欢的大型聊 ...
- (Good topic)卡牌分组(3.27leetcode每日打卡)
给定一副牌,每张牌上都写着一个整数. 此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组: 每组都有 X 张牌. 组内所有的牌上都写着相同的整数. 仅当你可选的 X &g ...
- python中四种方法提升数据处理的速度
在数据科学计算.机器学习.以及深度学习领域,Python 是最受欢迎的语言.Python 在数据科学领域,有非常丰富的包可以选择,numpy.scipy.pandas.scikit-learn.mat ...