一.跨域问题(无法登陆renrenfast)



解决方法:
1.修改前端config/index.js
尽量不要写localhost,端口号改为gateway网关端口

2.修改前端utils/httpRequest.js ,拼上‘/api’

3.修改confi/dev.env.js 开启代理

4.后端gateway-application.yml配置
spring:
cloud:
gateway:
routes:
- id: product_route
uri: lb://fdmall-product
predicates:
- Path=/api/product/**
filters:
- RewritePath=/api/(?<segment>.*),/$\{segment}
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
5.创建自定义过滤器
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
public class FdmallCorsConfiguration {
@Bean // 添加过滤器
public CorsWebFilter corsWebFilter(){
// 基于url跨域,选择reactive包下的
UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
// 跨域配置信息
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 允许跨域的头
corsConfiguration.addAllowedHeader("*");
// 允许跨域的请求方式
corsConfiguration.addAllowedMethod("*");
// 允许跨域的请求来源
corsConfiguration.addAllowedOriginPattern("*");
// 是否允许携带cookie跨域
corsConfiguration.setAllowCredentials(true);
// 任意url都要进行跨域配置
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
}
}
6.启动类添加@EnableDiscoveryClient
文章主要介绍了如何解决RenrenFast登录时的跨域问题,包括前端修改config/index.js和utils/httpRequest.js,开启代理,调整后端gateway-application.yml配置,创建自定义过滤器以处理CORS,并在启动类中添加@EnableDiscoveryClient注解。
2159





