Spring Boot跨域配置

简介: Spring Boot 2.0.x ---> 2.3.x 版本跨域配置

1,采用内置默认跨域过滤器的方式指定默认的跨域配置。

编写配置Bean

MagicalCorsProperties

importcom.google.common.base.MoreObjects;
importjava.io.Serializable;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.stereotype.Component;
/*** 跨域自动配置* @author Jeckxu*/@Component@ConfigurationProperties(prefix=MagicalCorsProperties.PRE_FIX)
publicclassMagicalCorsPropertiesimplementsSerializable {
publicstaticfinalStringPRE_FIX="magical.cors.default";
privateBooleanenabled=true;
privateStringpathPattern="/cors/**";
privateList<String>allowedOrigins=Collections.singletonList("*");
privateList<String>allowedHeaders=Collections.singletonList("*");
privateList<String>allowedMethods=Collections.singletonList("*");
privateList<String>exposedHeaders=newArrayList<>();
privateLongmaxAge=3600L;
privateBooleanallowCredentials=Boolean.TRUE;
publicBooleangetEnabled() {
returnenabled;
   }
publicvoidsetEnabled(Booleanenabled) {
this.enabled=enabled;
   }
publicStringgetPathPattern() {
returnpathPattern;
   }
publicvoidsetPathPattern(StringpathPattern) {
this.pathPattern=pathPattern;
   }
publicList<String>getAllowedOrigins() {
returnallowedOrigins;
   }
publicvoidsetAllowedOrigins(List<String>allowedOrigins) {
this.allowedOrigins=allowedOrigins;
   }
publicList<String>getAllowedHeaders() {
returnallowedHeaders;
   }
publicvoidsetAllowedHeaders(List<String>allowedHeaders) {
this.allowedHeaders=allowedHeaders;
   }
publicList<String>getAllowedMethods() {
returnallowedMethods;
   }
publicvoidsetAllowedMethods(List<String>allowedMethods) {
this.allowedMethods=allowedMethods;
   }
publicList<String>getExposedHeaders() {
returnexposedHeaders;
   }
publicvoidsetExposedHeaders(List<String>exposedHeaders) {
this.exposedHeaders=exposedHeaders;
   }
publicLonggetMaxAge() {
returnmaxAge;
   }
publicvoidsetMaxAge(LongmaxAge) {
this.maxAge=maxAge;
   }
publicBooleangetAllowCredentials() {
returnallowCredentials;
   }
publicvoidsetAllowCredentials(BooleanallowCredentials) {
this.allowCredentials=allowCredentials;
   }
@OverridepublicStringtoString() {
returnMoreObjects.toStringHelper(this)
         .add("enabled", enabled)
         .add("pathPattern", pathPattern)
         .add("allowedOrigins", allowedOrigins)
         .add("allowedHeaders", allowedHeaders)
         .add("allowedMethods", allowedMethods)
         .add("exposedHeaders", exposedHeaders)
         .add("maxAge", maxAge)
         .add("allowCredentials", allowCredentials)
         .toString();
   }
}

编写自动配置类

MagicalCorsAutoConfiguration

importorg.jeckxu.magical.core.boot.props.MagicalCorsProperties;
importorg.jeckxu.magical.core.launch.destroy.MagicalDestroying;
importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.cors.CorsConfiguration;
importorg.springframework.web.cors.UrlBasedCorsConfigurationSource;
importorg.springframework.web.filter.CorsFilter;
/*** @author xuchangjiang*/@Configuration@ConditionalOnProperty(value=MagicalCorsProperties.PRE_FIX+".enabled", havingValue="true", matchIfMissing=true)
publicclassMagicalCorsAutoConfigurationimplementsMagicalDestroying {
privatefinalMagicalCorsPropertiesmagicalCorsProperties;
publicMagicalCorsAutoConfiguration(MagicalCorsPropertiesmagicalCorsProperties) {
this.magicalCorsProperties=magicalCorsProperties;
   }
/*** 默认跨域过滤器* @return*/@BeanpublicCorsFiltercorsFilter() {
CorsConfigurationcorsConfiguration=newCorsConfiguration();
magicalCorsProperties.getAllowedOrigins().forEach(corsConfiguration::addAllowedOrigin);
magicalCorsProperties.getExposedHeaders().forEach(corsConfiguration::addExposedHeader);
magicalCorsProperties.getAllowedHeaders().forEach(corsConfiguration::addAllowedHeader);
magicalCorsProperties.getAllowedMethods().forEach(corsConfiguration::addAllowedMethod);
corsConfiguration.setAllowCredentials(magicalCorsProperties.getAllowCredentials());
UrlBasedCorsConfigurationSourcecorsConfigurationSource=newUrlBasedCorsConfigurationSource();
corsConfigurationSource.registerCorsConfiguration(magicalCorsProperties.getPathPattern(), corsConfiguration);
returnnewCorsFilter(corsConfigurationSource);
   }
}

2,注意点:

不同Spring Boot版本的自动配置类MagicalCorsAutoConfiguration中的@Configuration注解参数不同。Spring Boot 2.1.x以及之前的版本为:

@Configuration

Spring Boot 2.2.x以及之后的版本为

@Configuration(proxyBeanMethods=false)

以上配置项适用于Spring boot 2.0.x 到 2.3.x


目录
相关文章
|
2月前
|
Java Spring
Spring boot 运行服务jar外配置配置文件方式总结
Spring boot 运行服务jar外配置配置文件方式总结
374 0
|
2月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
3天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
39 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
4天前
|
Java 数据库连接 Maven
Spring基础1——Spring(配置开发版),IOC和DI
spring介绍、入门案例、控制反转IOC、IOC容器、Bean、依赖注入DI
Spring基础1——Spring(配置开发版),IOC和DI
|
15天前
|
IDE Java 开发工具
还在为繁琐的配置头疼吗?一文教你如何用 Spring Boot 快速启动,让开发效率飙升,从此告别加班——打造你的首个轻量级应用!
【9月更文挑战第2天】Spring Boot 是一款基于 Spring 框架的简化开发工具包,采用“约定优于配置”的原则,帮助开发者快速创建独立的生产级应用程序。本文将指导您完成首个 Spring Boot 项目的搭建过程,包括环境配置、项目初始化、添加依赖、编写控制器及运行应用。首先需确保 JDK 版本不低于 8,并安装支持 Spring Boot 的现代 IDE,如 IntelliJ IDEA 或 Eclipse。
51 5
|
19天前
|
Java 微服务 Spring
Spring Cloud全解析:配置中心之解决configserver单点问题
但是如果该configserver挂掉了,那就无法获取最新的配置了,微服务就出现了configserver的单点问题,那么如何避免configserver单点呢?
|
1月前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
1月前
|
安全 前端开发 Java
Web端系统开发解决跨域问题——以Java SpringBoot框架配置Cors为例
在Web安全上下文中,源(Origin)是指一个URL的协议、域名和端口号的组合。这三个部分共同定义了资源的来源,浏览器会根据这些信息来判断两个资源是否属于同一源。例如,https://www.example.com:443和http://www.example.com虽然域名相同,但由于协议和端口号不同,它们被视为不同的源。同源(Same-Origin)是指两个URL的协议、域名和端口号完全相同。只有当这些条件都满足时,浏览器才认为这两个资源来自同一源,从而允许它们之间的交互操作。
Web端系统开发解决跨域问题——以Java SpringBoot框架配置Cors为例
|
17天前
|
Java Spring 开发者
解锁 Spring Boot 自动化配置的黑科技:带你走进一键配置的高效开发新时代,再也不怕繁琐设置!
【8月更文挑战第31天】Spring Boot 的自动化配置机制极大简化了开发流程,使开发者能专注业务逻辑。通过 `@SpringBootApplication` 注解组合,特别是 `@EnableAutoConfiguration`,Spring Boot 可自动激活所需配置。例如,添加 JPA 依赖后,只需在 `application.properties` 配置数据库信息,即可自动完成 JPA 和数据源设置。这一机制基于多种条件注解(如 `@ConditionalOnClass`)实现智能配置。深入理解该机制有助于提升开发效率并更好地解决问题。
30 0
|
17天前
|
Java Spring 开发者
Spring 框架配置属性绑定大比拼:@Value 与 @ConfigurationProperties,谁才是真正的王者?
【8月更文挑战第31天】Spring 框架提供 `@Value` 和 `@ConfigurationProperties` 两种配置属性绑定方式。`@Value` 简单直接,适用于简单场景,但处理复杂配置时略显不足。`@ConfigurationProperties` 则以类级别绑定配置,简化代码并更好组织配置信息。本文通过示例对比两者特点,帮助开发者根据具体需求选择合适的绑定方式,实现高效且易维护的配置管理。
29 0