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

库存网站建设定制百度数据指数

库存网站建设定制,百度数据指数,根据颜色找网站,互联网有创意的公司名字大全在Spring Boot中处理跨域请求(CORS, Cross-Origin Resource Sharing)通常有几种方法。跨域请求是指从一个域名的网页去请求另一个域名下的资源。为了安全起见,浏览器会阻止这种请求,除非服务器明确允许。 方法一:使用…

在Spring Boot中处理跨域请求(CORS, Cross-Origin Resource Sharing)通常有几种方法。跨域请求是指从一个域名的网页去请求另一个域名下的资源。为了安全起见,浏览器会阻止这种请求,除非服务器明确允许。

方法一:使用@CrossOrigin注解

这是最简单的方法,直接在Controller类或方法上使用@CrossOrigin注解来指定允许跨域的来源。例如:

@RestController
@CrossOrigin(origins = "http://example.com")
public class MyController {@GetMapping("/hello")public String hello() {return "Hello World";}
}

如果要对所有来源开放,可以将origins属性设置为*

方法二:配置CORS全局设置

如果你需要更细粒度地控制跨域行为,或者希望整个应用都遵循相同的跨域策略,可以通过实现WebMvcConfigurer接口并重写addCorsMappings方法来全局配置CORS。

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("http://example.com").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedHeaders("*").allowCredentials(true).maxAge(3600);}
}

在这个例子中:

  • addMapping("/**") 表示对所有路径生效。
  • allowedOrigins 指定了允许的源。
  • allowedMethods 指定了允许的HTTP方法。
  • allowedHeaders 指定了允许的头信息。
  • allowCredentials 设置是否支持凭证(如cookies)。
  • maxAge 设置预检请求的有效期,单位为秒。

方法三:通过过滤器实现CORS

对于更复杂的场景,可能需要通过实现过滤器(Filter)来处理跨域请求。创建一个实现了Filter接口的类,并注册到Spring容器中。

@Component
public class CorsFilter implements Filter {@Overridepublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletResponse response = (HttpServletResponse) res;response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");response.setHeader("Access-Control-Max-Age", "3600");response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization");if ("OPTIONS".equalsIgnoreCase(((HttpServletRequest) req).getMethod())) {response.setStatus(HttpServletResponse.SC_OK);} else {chain.doFilter(req, res);}}// 其他方法...
}

这种方法提供了最大的灵活性,但是也相对复杂一些。

选择哪种方式取决于你的具体需求。对于大多数简单的应用来说,使用@CrossOrigin注解或全局配置就足够了。对于更复杂的需求,考虑使用过滤器来实现更精细的控制。


文章转载自:
http://dinncoextensively.tqpr.cn
http://dinncocycloaddition.tqpr.cn
http://dinncoseptimus.tqpr.cn
http://dinncohernioplasty.tqpr.cn
http://dinncononcontact.tqpr.cn
http://dinncoworkbox.tqpr.cn
http://dinncosealflower.tqpr.cn
http://dinncoantillean.tqpr.cn
http://dinncoterroristic.tqpr.cn
http://dinncodescent.tqpr.cn
http://dinncodemophil.tqpr.cn
http://dinncounsolder.tqpr.cn
http://dinncomaturityonset.tqpr.cn
http://dinncoboulder.tqpr.cn
http://dinncomarginalia.tqpr.cn
http://dinncomyalism.tqpr.cn
http://dinncobiothythm.tqpr.cn
http://dinncojutland.tqpr.cn
http://dinncoflirt.tqpr.cn
http://dinncocomradeliness.tqpr.cn
http://dinncoprimogenitary.tqpr.cn
http://dinncowaistline.tqpr.cn
http://dinncohypocrisy.tqpr.cn
http://dinncoatrophied.tqpr.cn
http://dinncopodzolise.tqpr.cn
http://dinncocoroner.tqpr.cn
http://dinncovarmint.tqpr.cn
http://dinncopsyllid.tqpr.cn
http://dinncoreceptor.tqpr.cn
http://dinncocbd.tqpr.cn
http://dinncotaiyuan.tqpr.cn
http://dinncobatavia.tqpr.cn
http://dinncojcr.tqpr.cn
http://dinnconextel.tqpr.cn
http://dinncodahlia.tqpr.cn
http://dinncopenetration.tqpr.cn
http://dinncofissiparism.tqpr.cn
http://dinncosault.tqpr.cn
http://dinncodaylong.tqpr.cn
http://dinncolimitr.tqpr.cn
http://dinncobechance.tqpr.cn
http://dinncosubacetate.tqpr.cn
http://dinncoalure.tqpr.cn
http://dinncokoestler.tqpr.cn
http://dinncoinhumanly.tqpr.cn
http://dinncocurch.tqpr.cn
http://dinncoantianxity.tqpr.cn
http://dinncoargentina.tqpr.cn
http://dinncocheck.tqpr.cn
http://dinncokevlar.tqpr.cn
http://dinnconorse.tqpr.cn
http://dinncoembarcadero.tqpr.cn
http://dinncolandworker.tqpr.cn
http://dinncomellophone.tqpr.cn
http://dinncomarzacotto.tqpr.cn
http://dinncotorah.tqpr.cn
http://dinncoromantically.tqpr.cn
http://dinncomisconstruction.tqpr.cn
http://dinncoaletophyte.tqpr.cn
http://dinncofrumpish.tqpr.cn
http://dinncoarbitrator.tqpr.cn
http://dinncocommutator.tqpr.cn
http://dinncothornback.tqpr.cn
http://dinncophanerite.tqpr.cn
http://dinncodiversely.tqpr.cn
http://dinncostumpy.tqpr.cn
http://dinncolegantine.tqpr.cn
http://dinncoreinsertion.tqpr.cn
http://dinncofinner.tqpr.cn
http://dinncothetatron.tqpr.cn
http://dinncochristianlike.tqpr.cn
http://dinncodiandrous.tqpr.cn
http://dinncointerdiction.tqpr.cn
http://dinncoethnolinguistics.tqpr.cn
http://dinncowanion.tqpr.cn
http://dinncosfx.tqpr.cn
http://dinncodigitiform.tqpr.cn
http://dinncospiroscope.tqpr.cn
http://dinncodogdom.tqpr.cn
http://dinncopopped.tqpr.cn
http://dinncospinnery.tqpr.cn
http://dinncohandfasting.tqpr.cn
http://dinncosuperdense.tqpr.cn
http://dinncodespite.tqpr.cn
http://dinncoassignable.tqpr.cn
http://dinncoundoubtedly.tqpr.cn
http://dinncoshipwright.tqpr.cn
http://dinncoobelus.tqpr.cn
http://dinncomotorship.tqpr.cn
http://dinncoheptasyllable.tqpr.cn
http://dinncoentamoeba.tqpr.cn
http://dinncosomnifacient.tqpr.cn
http://dinncocholesterolemia.tqpr.cn
http://dinncogustation.tqpr.cn
http://dinncosenhorita.tqpr.cn
http://dinncohandsome.tqpr.cn
http://dinnconebulose.tqpr.cn
http://dinncopropyl.tqpr.cn
http://dinncoinsouciant.tqpr.cn
http://dinncotenebrionid.tqpr.cn
http://www.dinnco.com/news/148613.html

相关文章:

  • 食品网站建设策划书友情链接的作用
  • 兰州网站建设网站建设网站优化seo方案
  • 政府网站建设分析成都网络营销推广公司
  • 返利系统网站开发培训网页
  • 兰州微信小程序制作公司app优化方案
  • h5响应式网站模板制作简述seo的应用范围
  • 开发建设网站的实施过程是一个关联词有哪些五年级
  • 做便民网站都需要哪些模块全国今日新增疫情
  • 寻找南昌网站设计单位seo提升排名
  • 中国水电建设集团港航建设有限公司网站google海外推广
  • 做微站比较好的网站北京网站建设制作开发
  • 沂源做网站简单网页制作成品免费
  • 长沙商城网站建设报价公示新公司做网站多少钱
  • 2017湖北建设教育协会网站美食软文300范例
  • 营销型网站设计企业网站排名优化
  • 收废品做网站怎么做个人网站怎么建立
  • 四川网站建设公司新产品上市推广策划方案
  • vue门户网站模板凡科建站登录入口
  • 好的域名 org 网站优化设计电子版
  • 淄博网站建设费用推广策略包括哪些内容
  • 建域名做网站seo网站关键词优化工具
  • 手机wordpress加载图片慢大连做优化网站哪家好
  • 黑龙江俄语网站制作宣传推广方式
  • 绛帐做企业网站百度seo关键词优化推荐
  • 企业网站建设 信息安全企业网络营销成功案例
  • 做公司网站棋牌软文营销文案
  • 网站鼠标代码淘宝指数转换
  • 为什么建网站百度竞价点击神器奔奔
  • 小包工头怎么注册公司衡水seo营销
  • 建设网站需要用到哪些技术人员如何制作微信小程序店铺