SpringBoot配置CORS跨域访问
来源:互联网 发布:xyz域名的价值 编辑:程序博客网 时间:2024/06/05 01:03
SpringBoot配置CORS跨域访问
添加依赖
<!--CORS只需要添加web依赖即可--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
创建CORS配置类
CORS配置类必须继承WebMvcConfigurerAdapter
import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {}
@Override addCorsMappings方法
// 设置跨域访问 @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowCredentials(true); }
说明:
addMapping:可以被跨域的路径,”/**”表示无限制。
allowedMethods:允许跨域访问资源服务器的请求方式,如:POST、GET、PUT、DELETE等,“*”表示无限制。
allowedOrigins:”*”允许所有的请求域名访问跨域资源,当设置具体URL时只有被设置的url可以跨域访问。例如:allowedOrigins(“https://www.baidu.com”),则只有https://www.baidu.com能访问跨域资源。
以上只是其中的一部分,具体根据项目需求自定义,不再一一介绍。
附:CorsRegistration源码
public class CorsRegistration { private final String pathPattern; private final CorsConfiguration config; public CorsRegistration(String pathPattern) { this.pathPattern = pathPattern; this.config = (new CorsConfiguration()).applyPermitDefaultValues(); } public CorsRegistration allowedOrigins(String... origins) { this.config.setAllowedOrigins(Arrays.asList(origins)); return this; } public CorsRegistration allowedMethods(String... methods) { this.config.setAllowedMethods(Arrays.asList(methods)); return this; } public CorsRegistration allowedHeaders(String... headers) { this.config.setAllowedHeaders(Arrays.asList(headers)); return this; } public CorsRegistration exposedHeaders(String... headers) { this.config.setExposedHeaders(Arrays.asList(headers)); return this; } public CorsRegistration maxAge(long maxAge) { this.config.setMaxAge(maxAge); return this; } public CorsRegistration allowCredentials(boolean allowCredentials) { this.config.setAllowCredentials(allowCredentials); return this; } protected String getPathPattern() { return this.pathPattern; } protected CorsConfiguration getCorsConfiguration() { return this.config; }}
阅读全文
0 0
- SpringBoot配置CORS跨域访问
- CORS 跨域访问
- SpringMVC支持跨域访问的CORS配置
- SpringMVC支持跨域访问的CORS配置
- springBoot的CORS支持跨域
- cors ajax跨域访问
- [CORS] 跨域访问的
- 开启CORS跨域访问
- CORS 跨域资源访问
- HTTP访问控制(CORS) 跨域访问
- jquery ajax CORS 跨域访问 WebService
- JavaScript跨域访问CORS问题
- CORS 解决跨域访问问题
- 使用CORS进行跨域访问
- CORS(跨域访问) 服务端流程图
- CORS解决ajax跨域访问问题
- 跨域访问--CORS
- springboot中通过cors协议解决跨域问题
- spring mail
- 解决The project description file (.project) for 'xxx' is missing
- Unity游戏开发图片纹理压缩方案
- 图像卷积与滤波的一些知识点
- InetAddress的常用方法
- SpringBoot配置CORS跨域访问
- 自增/自减
- 初学UE4遇到的坑Set Show Mouse Cursor
- Spring Cloud服务提供者与服务消费者怎么运作的?
- android arm linux下使用内存转储crash工具分析 kernel system dump问题
- 【阿里在线技术峰会】何登成:AliSQL性能优化与功能突破的演进之路
- 纯白帽seo技术也可以达到快速排名上首页效果
- 短时傅里叶变换和小波变换在处理EEG信号中的应用
- 过滤器(Filter)、拦截器(Interceptor)、监听器(Listener)