日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

CORS support in Spring Framework--官方

發(fā)布時間:2025/4/5 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CORS support in Spring Framework--官方 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文地址:https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

For security reasons, browsers prohibit AJAX calls to resources residing outside the current origin. For example, as you’re checking your bank account in one tab, you could have the evil.com website in another tab. The scripts from evil.com shouldn’t be able to make AJAX requests to your bank API (withdrawing money from your account!) using your credentials.

Cross-origin resource sharing?(CORS) is a?W3C specification?implemented by?most browsers?that allows you to specify in a flexible way what kind of cross domain requests are authorized, instead of using some less secured and less powerful hacks like IFrame or JSONP.

Spring Framework 4.2 GA?provides?first class support for CORS?out-of-the-box, giving you an easier and more powerful way to configure it than typical?filter based?solutions.

Spring MVC provides high-level configuration facilities, described bellow.

Controller method CORS configuration

You can add to your?@RequestMapping?annotated handler method a?@CrossOrigin?annotation in order to enable CORS on it (by default?@CrossOrigin?allows all origins and the HTTP methods specified in the?@RequestMapping?annotation):

@RestController @RequestMapping("/account") public class AccountController { @CrossOrigin @RequestMapping("/{id}") public Account retrieve(@PathVariable Long id) { // ... } @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") public void remove(@PathVariable Long id) { // ... } }

It is also possible to enable CORS for the whole controller:

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600) @RestController @RequestMapping("/account") public class AccountController { @RequestMapping("/{id}") public Account retrieve(@PathVariable Long id) { // ... } @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") public void remove(@PathVariable Long id) { // ... } }

In this example CORS support is enabled for both?retrieve()?and?remove()?handler methods, and you can also see how you can customize the CORS configuration using?@CrossOrigin?attributes.

You can even use both controller and method level CORS configurations, Spring will then combine both annotation attributes to create a merged CORS configuration.

@CrossOrigin(maxAge = 3600) @RestController @RequestMapping("/account") public class AccountController { @CrossOrigin(origins = "http://domain2.com") @RequestMapping("/{id}") public Account retrieve(@PathVariable Long id) { // ... } @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") public void remove(@PathVariable Long id) { // ... } }

Global CORS configuration

In addition to fine-grained, annotation-based configuration you’ll probably want to define some global CORS configuration as well. This is similar to using filters but can be declared withing Spring MVC and combined with fine-grained?@CrossOrigin?configuration. By default all origins and?GET,?HEAD?and?POST?methods are allowed.

JavaConfig

Enabling CORS for the whole application is as simple as:

@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }

You can easily change any properties, as well as only apply this CORS configuration to a specific path pattern:

@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://domain2.com") .allowedMethods("PUT", "DELETE") .allowedHeaders("header1", "header2", "header3") .exposedHeaders("header1", "header2") .allowCredentials(false).maxAge(3600); } }

XML namespace

It is also possible to configure CORS with the?mvc XML namespace.

This minimal XML configuration enable CORS on?/**?path pattern with the same default properties than the JavaConfig one:

<mvc:cors><mvc:mapping path="/**" /> </mvc:cors>

It is also possible to declare several CORS mappings with customized properties:

<mvc:cors><mvc:mapping path="/api/**" allowed-origins="http://domain1.com, http://domain2.com" allowed-methods="GET, PUT" allowed-headers="header1, header2, header3" exposed-headers="header1, header2" allow-credentials="false" max-age="123" /> <mvc:mapping path="/resources/**" allowed-origins="http://domain1.com" /> </mvc:cors>

How does it work?

CORS requests (including preflight ones with an?OPTIONS?method) are automatically dispatched to the various?HandlerMappings registered. They handle CORS preflight requests and intercept CORS simple and actual requests thanks to a?CorsProcessor?implementation (DefaultCorsProcessor?by default) in order to add the relevant CORS response headers (like?Access-Control-Allow-Origin).?CorsConfiguration?allows you to specify how the CORS requests should be processed: allowed origins, headers, methods, etc. It can be provided in various ways:

  • AbstractHandlerMapping#setCorsConfiguration()?allows to specify a?Map?with several?CorsConfiguration?mapped on path patterns like?/api/**
  • Subclasses can provide their own?CorsConfiguration?by overriding?AbstractHandlerMapping#getCorsConfiguration(Object, HttpServletRequest)?method
  • Handlers can implement?CorsConfigurationSource?interface (like?ResourceHttpRequestHandler?now does) in order to provide a?CorsConfiguration?for each request.

Spring Boot integration

CORS support will be available in the upcoming Spring Boot 1.3 release, and is already available in the 1.3.0.BUILD-SNAPSHOT builds.

Using controller method CORS configuration with?@CrossOrigin?annotations in your Spring Boot application does not require any specific configuration.

Global CORS configuration can be defined by registering a?WebMvcConfigurer?bean with a customized?addCorsMappings(CorsRegistry)?method:

@Configuration public class MyConfiguration { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**"); } }; } }

Filter based CORS support

In order to support CORS with filter-based security framework like Spring Security, or with other projects that does not support natively CORS yet like?Spring Data REST, we also provide a?CorsFilter. In that case, instead of using?@CrossOrigin?or?WebMvcConfigurer#addCorsMappings(CorsRegistry), you can for example declare the filter as following in your Spring Boot application:

@Configuration public class MyConfiguration { @Bean public FilterRegistrationBean corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOrigin("http://domain1.com"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); bean.setOrder(0); return bean; } }

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/6510940.html

總結

以上是生活随笔為你收集整理的CORS support in Spring Framework--官方的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人夜晚视频 | 成人精品在线看 | 最新91在线 | 在线观看日韩国产 | 黄色三级免费网站 | 亚洲一级中文字幕 | 日韩美女激情 | 亚洲一区在线不卡 | 精品99999| 国产亚州av| 亚洲精品少妇 | 日韩欧美中文字幕一区二区三区 | 国产精品探花一区二区三区 | 男人天堂av在线播放 | 欧美国产大片 | 日韩激情在线视频 | 婷婷激情久久 | 久久精品中文字幕 | 免费国产羞羞网站视频 | 毛茸茸亚洲孕妇孕交片 | 欧美中文字幕 | 亚洲精品永久免费 | 国产在线观看无码免费视频 | 超碰资源总站 | 欧美精品一二三区 | 免费毛片看片 | 亚洲色图自拍 | 美女看片| 日日艹 | 三年大片在线观看 | 午夜激情免费视频 | 久久久久久激情 | 国产男女无套免费网站 | 日韩少妇中文字幕 | 夜夜操夜夜骑 | 日本一本二本三区免费 | 亚洲天堂高清 | 亚洲综合日韩在线 | 2019中文字幕在线视频 | 可以免费看毛片的网站 | 亚洲一级在线播放 | 91成人免费观看 | 精品人妻一区二区三区久久 | 中国黄色三级 | 精品国产乱码久久久久久108 | 欧美人妖老妇 | 天堂8中文 | 男女污污网站 | 麻豆视频免费入口 | 三级在线国产 | 调教撅屁股啪调教打臀缝av | 亚洲av无码一区二区二三区 | 苍井空亚洲精品aa片在线播放 | 精品视频三区 | 四季av一区二区凹凸精品 | 很黄的网站在线观看 | 欧美h在线观看 | 青青在线观看视频 | 999久久久国产精品 韩国精品一区二区 | 国产精品普通话 | 亚洲激情视频在线 | 久久影视 | chinese hd xxxx tube麻豆tv | 又黄又爽视频在线观看 | 快播在线视频 | 白丝久久| 天堂va蜜桃一区二区三区漫画版 | 日批免费在线观看 | 中文字幕日韩精品无码内射 | www.久久久 | 女人裸体免费网站 | 91国内精品久久久 | 少妇av在线 | 亚洲国产综合在线 | 在线免费观看的av | 国产乱淫片视频 | 日本免费小视频 | 国产精品一区视频 | 成人午夜视频免费观看 | 日本欧美色 | 伊人天堂网 | 美女黄站 | 97欧美视频| 成人福利免费视频 | 99久久久无码国产精品6 | 粉嫩av一区二区三区免费观看 | 欧美888| 亚洲国产成人久久 | 欧美一级性片 | 欧美成人第一页 | 大尺度一区二区 | 成人福利小视频 | 成年人免费高清视频 | jzjzz成人免费视频 | 亚洲激情自拍偷拍 | 欧美三级在线 | 18岁免费观看电视连续剧 | 国产精品第二十页 | 亚洲乱码视频在线观看 |