spring Cloud微服務(wù)跨域?qū)崿F(xiàn)步驟
這篇文章主要介紹了spring Cloud微服務(wù)跨域?qū)崿F(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
第一步:在gateway網(wǎng)關(guān)的配置文件中加上下面這些:
ly: cors: allowedOrigins: - http://manage.leyou.com - http://xxx.xxx.com # 允許哪些網(wǎng)址就繼續(xù)加,不要寫(xiě) *,否則cookie就無(wú)法使用了 allowedCredentials: true # 代表攜帶cookie allowedHeaders: - "*" allowedMethods: - GET - POST - DELETE - PUT - OPTIONS - HEAD maxAge: 360000 filterPath: "/**"
第二步:寫(xiě)一個(gè)配置類解析上面的配置文件信息
@Data
@ConfigurationProperties(prefix = "ly.cors")
public class CORSProperties {
private List<String> allowedOrigins;
private Boolean allowedCredentials;
private List<String> allowedMethods;
private List<String> allowedHeaders;
private long maxAge;
private String filterPath;
}
第三步:寫(xiě)一個(gè)跨域的過(guò)濾器
@Configuration
@EnableConfigurationProperties(CORSProperties.class)
public class GlobalCORSConfig {
@Autowired
private CORSProperties prop;
/**
* @Bean注解,將當(dāng)前方法的返回值對(duì)象放入到IOC容器中
* @return
*/
@Bean
public CorsFilter corsFilter() {
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
prop.getAllowedOrigins().forEach(config::addAllowedOrigin);
//上面的寫(xiě)法和下面這個(gè)效果一樣
// for (String allowedOrigin : prop.getAllowedOrigins()) {
// config.addAllowedOrigin(allowedOrigin);
// }
//2) 是否發(fā)送Cookie信息
config.setAllowCredentials(prop.getAllowedCredentials());
//3) 允許的請(qǐng)求方式
prop.getAllowedMethods().forEach(config::addAllowedMethod);
// 4)允許的頭信息
prop.getAllowedHeaders().forEach(config::addAllowedHeader);
// 5)有效期
config.setMaxAge(prop.getMaxAge());
//2.添加映射路徑,我們攔截一切請(qǐng)求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration(prop.getFilterPath(), config);
//3.返回新的CORSFilter.
return new CorsFilter(configSource);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring cloud實(shí)現(xiàn)前端跨域問(wèn)題的解決方案
- spring cloud如何修復(fù)zuul跨域配置異常的問(wèn)題
- springboot接入cachecloud redis示例實(shí)踐
- Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼
- Spring Cloud Feign接口返回流的實(shí)現(xiàn)
- 創(chuàng)建網(wǎng)關(guān)項(xiàng)目(Spring Cloud Gateway)過(guò)程詳解
- springcloud config配置讀取優(yōu)先級(jí)過(guò)程詳解
相關(guān)文章
使用maven的profile構(gòu)建不同環(huán)境配置的方法
這篇文章主要介紹了使用maven的profile構(gòu)建不同環(huán)境配置的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
解決idea2020 maven無(wú)法自動(dòng)導(dǎo)包的問(wèn)題
這篇文章主要介紹了解決idea2020 maven無(wú)法自動(dòng)導(dǎo)包的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
SpringBoot 如何使用 JWT 保護(hù) Rest Api&nbs
使用spring-boot開(kāi)發(fā)RESTful API非常的方便,在生產(chǎn)環(huán)境中,對(duì)發(fā)布的 API 增加授權(quán)保護(hù)是非常必要的,現(xiàn)在我們來(lái)看如何利用JWT技術(shù)為API 增加授權(quán)保護(hù),保證只有獲得授權(quán)的用戶才能夠訪問(wèn) API,感興趣的朋友跟隨小編一起看看吧2024-02-02
spring boot整合mybatis使用c3p0數(shù)據(jù)源連接mysql
這篇文章主要為大家詳細(xì)介紹了spring boot整合mybatis使用c3p0數(shù)據(jù)源連接mysql,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Java Spring框架簡(jiǎn)介與Spring IOC詳解
Spring 框架是一個(gè)輕量級(jí)的解決方案,可以一站式地構(gòu)建企業(yè)級(jí)應(yīng)用。它是為了解決 企業(yè)應(yīng)用開(kāi)發(fā)的復(fù)雜性而創(chuàng)建的。Spring 使用基本的 JavaBean 來(lái)完成以前只可能由 EJB 完成的事情。IOC 是 Inversion of Control 的縮寫(xiě),多數(shù)書(shū)籍翻譯成控制反轉(zhuǎn)2021-09-09
Java導(dǎo)出Excel統(tǒng)計(jì)報(bào)表合并單元格的方法詳解
我們?cè)谌粘>幊踢^(guò)程中,總是會(huì)碰見(jiàn)導(dǎo)出相關(guān)表格信息的需求,所以就讓我們一起來(lái)學(xué)習(xí)一下,這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出Excel統(tǒng)計(jì)報(bào)表合并單元格的相關(guān)資料,需要的朋友可以參考下2021-10-10
Java設(shè)計(jì)模式之原型模式(Prototype模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之原型模式(Prototype模式)介紹,本文講解了如何使用原型模式并給出了代碼實(shí)例,需要的朋友可以參考下2015-03-03

