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ù)加,不要寫 *,否則cookie就無(wú)法使用了 allowedCredentials: true # 代表攜帶cookie allowedHeaders: - "*" allowedMethods: - GET - POST - DELETE - PUT - OPTIONS - HEAD maxAge: 360000 filterPath: "/**"
第二步:寫一個(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;
}
第三步:寫一個(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);
//上面的寫法和下面這個(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)文章
PropertiesLoaderUtils 出現(xiàn)中文亂碼的解決方式
這篇文章主要介紹了PropertiesLoaderUtils 出現(xiàn)中文亂碼的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Java long 轉(zhuǎn)成 String的實(shí)現(xiàn)
這篇文章主要介紹了Java long 轉(zhuǎn)成 String的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互相關(guān)介紹全面總結(jié)
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開(kāi)發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),這篇文章主要介紹了SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互2022-10-10
Java中的volatile實(shí)現(xiàn)機(jī)制詳細(xì)解析
這篇文章主要介紹了Java中的volatile實(shí)現(xiàn)機(jī)制詳細(xì)解析,本文的主要內(nèi)容就在于要理解volatile的緩存的一致性協(xié)議導(dǎo)致的共享變量可見(jiàn)性,以及volatile在解析成為匯編語(yǔ)言的時(shí)候?qū)ψ兞考渔i兩塊理論內(nèi)容,需要的朋友可以參考下2024-01-01

