Springboot通過配置WebMvcConfig處理Cors非同源訪問跨域問題
關(guān)于Cors跨域的問題,前端有代理和jsonp的常用方式解決這種非同源的訪問拒絕策略,什么是同源?即域名一致端口一致但是端口下訪問的接口api不同的兩種或者幾種的互相訪問叫做同源訪問,但是若是接口不一致或者域名不一致(這里泛指IP不一致),那么對(duì)應(yīng)的就屬于非同源訪問,瀏覽器會(huì)拒絕發(fā)出請(qǐng)求,直接回復(fù)404,有時(shí)候我也見過恢復(fù)202的就是發(fā)出去了但是被后端的Mvc處理hander鏈給拒絕了。那么配置MVC是后端處理Cors問題的一種解決思路。
之前學(xué)習(xí)過MVC的處理鏈路,從一次請(qǐng)求發(fā)過來到回復(fù)數(shù)據(jù)總共11次處理:

請(qǐng)求發(fā)送到服務(wù)器端時(shí)是由我們的MVC進(jìn)行處理的,而統(tǒng)一調(diào)配任務(wù)流程的則是我們的請(qǐng)求分發(fā)器,注意這里請(qǐng)求到處理器之后回去尋找處理器適配器(符合校驗(yàn)處理的請(qǐng)求才能被允許例如接口含有的合法api,以及跨域原則),之前我們的微信小程序開發(fā)過程中是沒有考慮跨域問題的,原因是我們知道小程序的請(qǐng)求處理都是由微信后臺(tái)進(jìn)行分發(fā)處理的,也就是在微信的后臺(tái)時(shí)就做了前端的跨域處理,大概是采用動(dòng)態(tài)代理的方式解決了小程序的跨域。
那么我們先看看MVC的配置接口 WebMvcConfigurer 的源代碼:
public interface WebMvcConfigurer {
default void configurePathMatch(PathMatchConfigurer configurer) {
}
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
}
default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
default void addFormatters(FormatterRegistry registry) {
}
default void addInterceptors(InterceptorRegistry registry) {
}
default void addResourceHandlers(ResourceHandlerRegistry registry) {
}
default void addCorsMappings(CorsRegistry registry) {
}
default void addViewControllers(ViewControllerRegistry registry) {
}
default void configureViewResolvers(ViewResolverRegistry registry) {
}
default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
}
default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
}
default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
}
default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
}
default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
}
default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
}
@Nullable
default Validator getValidator() {
return null;
}
@Nullable
default MessageCodesResolver getMessageCodesResolver() {
return null;
}
}它的內(nèi)部是具備一些處理器解析器以及映射的添加與配置的方法的,那么我們要解決Cros跨域問題就是要考慮addCorsMappings 配置Cros映射,所以我們點(diǎn)進(jìn)去看看這注冊(cè)Cros的 CorsRegistry 的源碼:
public class CorsRegistry {
private final List<CorsRegistration> registrations = new ArrayList();
public CorsRegistry() {
}
public CorsRegistration addMapping(String pathPattern) {
CorsRegistration registration = new CorsRegistration(pathPattern);
this.registrations.add(registration);
return registration;
}
protected Map<String, CorsConfiguration> getCorsConfigurations() {
Map<String, CorsConfiguration> configs = CollectionUtils.newLinkedHashMap(this.registrations.size());
Iterator var2 = this.registrations.iterator();
while(var2.hasNext()) {
CorsRegistration registration = (CorsRegistration)var2.next();
configs.put(registration.getPathPattern(), registration.getCorsConfiguration());
}
return configs;
}
}從上述代碼中不難發(fā)現(xiàn),內(nèi)部有一個(gè)不可改變的 CorsRegistration 數(shù)組鏈表,以及增加映射的方法,主要還是看看它具備的元素 CorsRegistration 含有什么配置項(xiàng):
public class CorsRegistration {
private final String pathPattern;
private CorsConfiguration config;
public CorsRegistration(String pathPattern) {
this.pathPattern = pathPattern;
this.config = (new CorsConfiguration()).applyPermitDefaultValues();
}
public CorsRegistration allowedOrigins(String... origins) {
this.config.setAllowedOrigins(Arrays.asList(origins));
return this;
}
public CorsRegistration allowedOriginPatterns(String... patterns) {
this.config.setAllowedOriginPatterns(Arrays.asList(patterns));
return this;
}
public CorsRegistration allowedMethods(String... methods) {
this.config.setAllowedMethods(Arrays.asList(methods));
return this;
}
public CorsRegistration allowedHeaders(String... headers) {
this.config.setAllowedHeaders(Arrays.asList(headers));
return this;
}
public CorsRegistration exposedHeaders(String... headers) {
this.config.setExposedHeaders(Arrays.asList(headers));
return this;
}
public CorsRegistration allowCredentials(boolean allowCredentials) {
this.config.setAllowCredentials(allowCredentials);
return this;
}
public CorsRegistration maxAge(long maxAge) {
this.config.setMaxAge(maxAge);
return this;
}
public CorsRegistration combine(CorsConfiguration other) {
this.config = this.config.combine(other);
return this;
}
protected String getPathPattern() {
return this.pathPattern;
}
protected CorsConfiguration getCorsConfiguration() {
return this.config;
}
}我們可以發(fā)現(xiàn)內(nèi)部是具備允許放行:請(qǐng)求頭,請(qǐng)求路徑,請(qǐng)求方法,請(qǐng)求源策略的方法的,所以我們?cè)谶@里的 重寫addCorsMappings方法配置一個(gè) CorsRegistry 添加相應(yīng)的路徑方法與請(qǐng)求策略放行不就可以解決跨域的問題了?
我們寫一個(gè)WebMvcConfig配置類實(shí)現(xiàn)剛剛研究的WebMvcConfigurer接口重寫addCrosMappings配置CrosRegistry即可(或者在api與Controller控制類上打上@CrossOrigin注解也可以解決問題(注解默認(rèn)放行所有來源的請(qǐng)求)):
/**
* 配置前端跨域訪問請(qǐng)求
*/
@Configuration
public class WbMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("Content-Type","X-Request-With","Access-Control-Request-Method","Access-Control-Request-Headers","token")
.allowedMethods("*")
.allowedOriginPatterns("*")
/*注意當(dāng)這個(gè)配置為真是我們不能將允許源設(shè)置為*而是將源路徑設(shè)置為*即可*/
.allowCredentials(true);
}
@Bean
public FormContentFilter httpPutFormContentFilter(){
return new FormContentFilter();
}
}我們利用axios寫一個(gè)簡(jiǎn)單的請(qǐng)求發(fā)送按鈕:
<input type="button" value="get" class="get">
<script>
document.querySelector(".get").onclick = function () {
// 跨域一般是是后端解決的事情
axios.get("http://127.0.0.1:8080/all").then(
function (response) {
console.log(response)
}
)
}
</script>再用SpringBoot寫一個(gè)簡(jiǎn)單的controller的api:
@RestController
public class testController {
@Autowired
private ProductServiceImpl productService;
@GetMapping("/all")
@ResponseBody
public List<Product> all() {
Page<Product> page = productService.page(1L);
List<Product> productList = new LinkedList<>();
productList.add(page.getRecords().iterator().next());
return productList;
}
}這里我們?cè)跒g覽器打開5050端口下的這個(gè)html文件就可以點(diǎn)擊按鈕訪問接口了:

這里可以看到請(qǐng)求訪問數(shù)據(jù)成功了!
到此這篇關(guān)于Springboot通過配置WebMvcConfig處理Cors非同源訪問跨域問題的文章就介紹到這了,更多相關(guān)Springboot Cors非同源訪問跨域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud與Consul集成實(shí)現(xiàn)負(fù)載均衡功能
負(fù)載均衡基本概念有:實(shí)服務(wù)、實(shí)服務(wù)組、虛服務(wù)、調(diào)度算法、持續(xù)性等,其常用應(yīng)用場(chǎng)景主要是服務(wù)器負(fù)載均衡,鏈路負(fù)載均衡。這篇文章主要介紹了SpringCloud與Consul集成實(shí)現(xiàn)負(fù)載均衡 ,需要的朋友可以參考下2018-09-09
Java調(diào)用CXF WebService接口的兩種方式實(shí)例
今天小編就為大家分享一篇關(guān)于Java調(diào)用CXF WebService接口的兩種方式實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
使用Java代碼實(shí)現(xiàn)RocketMQ的生產(chǎn)與消費(fèi)消息
這篇文章介紹一下其他的小組件以及使用Java代碼實(shí)現(xiàn)生產(chǎn)者對(duì)消息的生成,消費(fèi)者消費(fèi)消息等知識(shí)點(diǎn),并通過代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
Java中的對(duì)象和對(duì)象引用實(shí)例淺析
這篇文章主要介紹了Java中的對(duì)象和對(duì)象引用,實(shí)例分析了對(duì)象與對(duì)象引用的概念與相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
dockerfile-maven-plugin極簡(jiǎn)教程(推薦)
這篇文章主要介紹了dockerfile-maven-plugin極簡(jiǎn)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Springboot處理CORS跨域請(qǐng)求的三種方法
這篇文章主要介紹了Springboot處理CORS跨域請(qǐng)求的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java設(shè)計(jì)模式之單例模式簡(jiǎn)單解析
這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式簡(jiǎn)單解析,單例模式的優(yōu)點(diǎn)在于在內(nèi)存中某個(gè)類只有一個(gè)實(shí)例,減少了內(nèi)存的開銷,尤其是頻繁的創(chuàng)建和銷毀實(shí)例,避免對(duì)資源的多重暫用,需要的朋友可以參考下2023-12-12

