使用Spring CROS解決項(xiàng)目中的跨域問(wèn)題詳解
CROS(Cross-Origin Resource Sharing) 用于解決瀏覽器中跨域請(qǐng)求的問(wèn)題。簡(jiǎn)單的Get請(qǐng)求可以使用JSONP來(lái)解決,而對(duì)于其它復(fù)雜的請(qǐng)求則需要后端應(yīng)用的支持CROS。Spring在4.2版本之后提供了@CrossOrigin 注解來(lái)實(shí)現(xiàn)對(duì)Cross的支持。
在Controller方法上配置
@CrossOrigin(origins = {"http://loaclhost:8088"})
@RequestMapping(value = "/crossTest",method = RequestMethod.GET)
public String greeting() {
return "corss test";
}
在Controller上配置,那么這個(gè)Controller中的所有方法都會(huì)支持CORS
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@CrossOrigin(origins = "http://localhost:8088",maxAge = 3600)
@Controller
@RequestMapping("/api")
public class AppController {
@RequestMapping(value = "/crossTest",method = RequestMethod.GET)
public String greeting() {
return "corss test";
}
}
Java Config全局配置
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class SpringWebConfig extends WebMvcConfigurerAdapter {
/**
* {@inheritDoc}
* <p>This implementation is empty.
*
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
super.addCorsMappings(registry);
// 對(duì)所有的URL配置
registry.addMapping("/**");
// 針對(duì)某些URL配置
registry.addMapping("/api/**").allowedOrigins("http:///localhost:8088")
.allowedMethods("PUT","DELETE")
.allowedHeaders("header1","header2","header3")
.exposedHeaders("header1","header2")
.allowCredentials(false).maxAge(3600);
}
}
XML全局配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:cors>
<!--<mvc:mapping path=""/>-->
<mvc:mapping path="/api/**"
allowed-origins="http://localhost:8088,http://localhost:8888"
allowed-methods="GET,PUT"
allowed-headers="header1,header2"
exposed-headers="header1,header2"
allow-credentials="false"
max-age="3600" />
</mvc:cors>
</beans>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java將一個(gè)目錄下的所有數(shù)據(jù)復(fù)制到另一個(gè)目錄下
這篇文章主要為大家詳細(xì)介紹了java將一個(gè)目錄下的所有數(shù)據(jù)復(fù)制到另一個(gè)目錄下,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Java練手小項(xiàng)目實(shí)現(xiàn)一個(gè)項(xiàng)目管理系統(tǒng)
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)項(xiàng)目管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-10-10
java如何使用自己的maven本地倉(cāng)庫(kù)詳解
這篇文章主要給大家介紹了關(guān)于java如何使用自己的maven本地倉(cāng)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
java程序員必須要學(xué)會(huì)的linux命令總結(jié)(推薦)
下面小編就為大家分享一篇java程序員必須要學(xué)會(huì)的linux命令總結(jié)(推薦)。具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
ElasticSearch如何設(shè)置某個(gè)字段不分詞淺析
最近在學(xué)習(xí)ElasticSearch官方文檔過(guò)程中發(fā)現(xiàn)的某個(gè)問(wèn)題,記錄一下 希望能幫助到后面的朋友,下面這篇文章主要給大家介紹了關(guān)于ElasticSearch如何設(shè)置某個(gè)字段不分詞的相關(guān)資料,需要的朋友可以參考下2022-04-04

