当前位置: 首页 > news >正文

廊坊市固安县建设局网站好听好记的网站域名

廊坊市固安县建设局网站,好听好记的网站域名,汕头网站上排名,蔚县网站建设wl17581在Spring Boot项目中配置跨域(CORS,Cross-Origin Resource Sharing)主要是为了允许来自不同源(不同的协议、域名或端口)的前端应用能够访问后端API。Spring Boot提供了多种方式来配置跨域支持。 1. 使用CrossOrigin注…

在Spring Boot项目中配置跨域(CORS,Cross-Origin Resource Sharing)主要是为了允许来自不同源(不同的协议、域名或端口)的前端应用能够访问后端API。Spring Boot提供了多种方式来配置跨域支持。

1. 使用@CrossOrigin注解

最简单的方式是在控制器或者具体的方法上使用@CrossOrigin注解。例如:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@CrossOrigin(origins = "http://example.com") // 允许来自 http://example.com 的跨域请求
public class MyController {@GetMapping("/myEndpoint")public String myMethod() {return "Hello, World!";}
}

这将允许来自http://example.com的跨域请求访问/myEndpoint这个接口。

2. 全局跨域配置

如果你想要为整个应用配置跨域支持,可以在配置类中添加一个WebMvcConfigurer的实现:

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 WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**") // 为所有请求添加跨域支持.allowedOrigins("*")    // 允许任何源.allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的HTTP方法.allowCredentials(true); // 是否允许发送Cookie信息}
}

这个配置将允许任何源的跨域请求,并且允许GETPOSTPUTDELETE方法。allowCredentials设置为true表示允许客户端发送Cookie信息。

3. 使用CorsFilter

如果需要更细致的控制,可以创建一个CorsFilter并注册为Bean:

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;@Configuration
public class CorsConfig {@Beanpublic CorsFilter corsFilter() {CorsConfiguration configuration = new CorsConfiguration();configuration.setAllowedOrigins("*");configuration.setAllowedMethods("GET", "POST", "PUT", "DELETE");configuration.setAllowCredentials(true);configuration.setAllowedHeaders("*");UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", configuration);return new CorsFilter(source);}
}

这个配置与上面的全局跨域配置类似,但是通过CorsFilter提供了更多的灵活性和控制。

注意事项

  • allowedOrigins可以是一个具体的域名,如"http://example.com",或者使用"*"来允许任何源。
  • allowedMethods定义了允许的HTTP方法。
  • allowCredentials设置为true时,服务器将接受包含敏感信息(如Cookies和HTTP认证信息)的跨域请求。
  • allowedHeaders定义了允许的HTTP请求头。

根据你的项目需求和安全考虑,合理配置跨域支持是非常重要的。在生产环境中,通常不建议允许任何源("*"),而是应该明确指定可信的源。
当然,除了上述提到的使用@CrossOrigin注解、全局跨域配置和CorsFilter之外,还有其他一些方法可以在Spring Boot项目中配置跨域支持。

4. 配置WebMvcConfigurerProperties

在Spring Boot中,可以通过扩展WebMvcConfigurerProperties类来配置跨域。首先,需要创建一个配置类并继承WebMvcConfigurerProperties

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
@ConfigurationProperties(prefix = "cors")
public class CorsConfig implements WebMvcConfigurer {private String[] allowedOrigins;private String[] allowedMethods;private String[] allowedHeaders;private boolean allowCredentials;// getters and setters ...@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins(allowedOrigins).allowedMethods(allowedMethods).allowedHeaders(allowedHeaders).allowCredentials(allowCredentials);}
}

然后,在application.propertiesapplication.yml中添加相应的配置:

# application.properties
cors.allowedOrigins[0]=http://example.com
cors.allowedMethods[0]=GET
cors.allowedMethods[1]=POST
cors.allowedHeaders[0]=Content-Type
cors.allowCredentials=true

5. 使用Spring Security

如果你的项目中使用了Spring Security,可以通过配置HttpSecurity来实现跨域支持。首先,需要创建一个配置类并重写configure(HttpSecurity http)方法:

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http// ...其他配置....cors().and() // 启用默认的跨域配置// ...其他配置...}
}

如果需要自定义跨域配置,可以使用.cors()方法并传递一个CorsConfigurationSource实例:

 CorsConfiguration configuration = new CorsConfiguration();configuration.setAllowedOrigins(Arrays.asList("http://example.com"));configuration.setAllowedMethods(Arrays.asList("GET","POST", "PUT", "DELETE"));configuration.setAllowedHeaders(Arrays.asList("Content-Type", "Authorization"));configuration.setAllowCredentials(true);UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", configuration);http.cors(source).and();

6. 使用第三方库

还可以使用第三方库如cors-filter来实现跨域支持。这通常需要在项目的pom.xml中添加依赖,并在web.xml中配置过滤器。

以上是一些在Spring Boot项目中配置跨域支持的方法。选择最适合项目需求和架构的方法,并确保考虑到安全性和性能的影响。在实施跨域策略时,应当避免过度宽松的配置,以免引入安全风险。

http://www.yayakq.cn/news/247242/

相关文章:

  • 丹东 建设集团 招聘信息网站沈阳建设工程信息网 找到中项网
  • 足球外围网站怎么做网站开发素材代码
  • 深圳企业网站改版电子商务有限责任公司网站怎样建立
  • 网站正在建设中 打不开怎么办13岁找对象去哪个软件
  • 南昌网站建设培训ashu wordpress
  • 上海高端建站网站兰甘肃网站建设
  • seo网站建设公司哪家好青岛公司logo设计
  • iis7建网站曲阜网站设计
  • 湛江的网站怎样制作个人网站
  • wordpress 测评插件泰安网站优化推广
  • 东莞网站设计服务商小程序里48小时核酸是按照
  • php免费网站空间网页制作工具可分为
  • 快速建站完整版北京金山办公软件
  • 成都爱站网seo站长查询工具合肥高端网站建设公司哪家好
  • 网站开发的主要技术难点和重点企业管理软件有哪些
  • 教育网站设计案例电商软文范例
  • 网站没备案怎么做淘宝客山东专业网站开发公司
  • 签订网站制作协议需注意什么网络优化案例分析
  • 国产手机做系统下载网站jsp做网站组件
  • 商城网站建设二次开发互联网营销型网站
  • pos网站源码企业咨询服务
  • 网站地图在线制作工具百度识图官网
  • 做外贸阿里巴巴有哪些网站西安百度公司开户
  • 网络营销跟网站推广有啥区别做企业网站cms
  • 成都新津网站建设设计作品欣赏网站
  • 如何申请网站wordpress评论刷新查看
  • 如何设计旅游网站的域名一级a做爰片免费观看网站
  • 网站空间单位卖钢材做哪个宣传网站
  • 建立个人网站的费用怎么做美食的视频网站
  • 17.zwd一起做网站昆明做网站比较牛的