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

用网站模板做新网站ppt背景图免费

用网站模板做新网站,ppt背景图免费,梅州市网站制作,网站建站网站496565在Spring Boot项目中配置跨域#xff08;CORS#xff0c;Cross-Origin Resource Sharing#xff09;主要是为了允许来自不同源#xff08;不同的协议、域名或端口#xff09;的前端应用能够访问后端API。Spring Boot提供了多种方式来配置跨域支持。 1. 使用CrossOrigin注…在Spring Boot项目中配置跨域CORSCross-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信息} }这个配置将允许任何源的跨域请求并且允许GET、POST、PUT和DELETE方法。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.properties或application.yml中添加相应的配置 # application.properties cors.allowedOrigins[0]http://example.com cors.allowedMethods[0]GET cors.allowedMethods[1]POST cors.allowedHeaders[0]Content-Type cors.allowCredentialstrue5. 使用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.pierceye.com/news/209983/

相关文章:

  • 网站做成微信小程序贵州企业seo
  • 在淘宝做印刷网站怎么办wordpress 主题 edu
  • 成都设计公司网站线上线下一体化营销
  • 网站你懂我意思正能量晚上下载注册公司需要多少钱手续费
  • 在线html网站开发广州网站排名优化公司
  • 如何在免费网站上做推扩自己怎么来建设网站
  • 福安市教育局建设网站做架构图简单的网站
  • 如何快速进行网站开发seo是什么东西
  • 网站建设需要具备哪些学编程多少钱学费
  • 建设工程许可证在那个网站办金融行业网站制作
  • 邢台专业做网站价格信息流广告是什么
  • 网站开发的母的目的和意义.建设购物平台网站
  • 立方米网站建设做淘宝客网站用什么程序好
  • 怎样做网站挣钱建筑资料软件
  • 涿州建设局网站苏州市高新区建设局网站
  • 个人soho要怎么做企业网站成都包装设计公司
  • 网站开发 chrome浏览器崩溃ruhe用dw做网站
  • 全屏网站 图片优化个人网站cms系统
  • 做我女朋友程序网站邵东做网站
  • 建设网站如何挂到网上wordpress首页添加幻灯
  • 汕头正规网站建设模板总部城乡建设网站 资料员
  • vs 2017c 怎么建设网站网站建设的数字化和互联网化
  • 南昌网站设计公司海南营销网站建设
  • 购物网站素材个人搭建网站教程
  • 青岛网站建设哪里好模板建站服务公司
  • 青色网站欣赏wordpress中文购物
  • 建站培训全国住房与城乡建设部网站
  • 唐山网站建设方案策划沧州网站建设联系电话
  • 网页制作和网站开发实验报告logo设计品牌
  • 摄影后期教程网站百度指数1000搜索量有多少