1. 程式人生 > >Spring Boot Web應用開發 CORS 跨域請求支持:

Spring Boot Web應用開發 CORS 跨域請求支持:

custom auth explorer itl addall sta padding ... per

Spring Boot Web應用開發 CORS 跨域請求支持:

一、Web開發經常會遇到跨域問題,解決方案有:jsonp,iframe,CORS等等
CORS與JSONP相比

1、 JSONP只能實現GET請求,而CORS支持所有類型的HTTP請求。
2、 使用CORS,開發者可以使用普通的XMLHttpRequest發起請求和獲得數據,比起JSONP有更好的錯誤處理。
3、 JSONP主要被老的瀏覽器支持,它們往往不支持CORS,而絕大多數現代瀏覽器都已經支持了CORS
瀏覽器支持情況

Chrome 3+
Firefox 3.5+
Opera 12+
Safari 4+
Internet Explorer 8+

二、在spring MVC 中可以配置全局的規則,也可以使用@CrossOrigin註解進行細粒度的配置。

全局配置:
@Configuration
public class CustomCorsConfiguration {

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void ad
};
}
}

或者是

/**
* 全局設置
*
* @author wujing
*/
@Configuration
public class CustomCorsConfiguration2 extends WebMvcConfigurerAdapter {


@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**").allowedOrigins("http://localhost:8080");
}
}
定義方法:


/**
* @author wujing
*/
@RestController
@RequestMapping("/api")
public class ApiController {

@RequestMapping(value = "/get")
public HashMap<String, Object> get(@RequestParam String name) {

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "hello world");
map.put("name", name);
return map;
}
}

測試js:

$.ajax({
url: "http://localhost:8081/api/get",
type: "POST",
data: {
name: "測試"
},
success: function(data, status, xhr) {
console.log(data);
alert(data.name);
}
});

細粒度配置:


/**
* @author wujing
*/
@RestController
@RequestMapping(value = "/api", method = RequestMethod.POST)
public class ApiController {

@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value = "/get")
public HashMap<String, Object> get(@RequestParam String name) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "hello world");
map.put("name", name);
return map;
}
}


案例

package com.toutiao.agent.apiimpl.conf;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

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

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

有道詞典 dCorsMappings(C ... 詳細X   dCorsMappings(CorsRegistry註冊表){   registry.addMapping(“/ api / * *”).allowedOrigins(http://localhost:8080);   }

Spring Boot Web應用開發 CORS 跨域請求支持: