网关
程序员文章站
2022-06-03 19:07:51
...
配置网关以及解决跨域问题
1、首先在前端页面,把基础请求路由给网关
// api接口请求地址
window.SITE_CONFIG['baseUrl'] = 'http://localhost:88/api';
2、在网关中首先引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
3、将网关注册到nacos
//spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
//spring.application.name=container-gateway
//server.port=88
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ContainerGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ContainerGatewayApplication.class, args);
}
}
4、在application.yml文件中配置路由
spring:
cloud:
gateway:
routes:
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
#这里是指以/api开头的所有请求全部进行拦截
filters:
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
#对路径进行重写
5、编写配置类,对跨域问题进行处理
@Configuration
public class ContainerOrgConfiguration {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration=new CorsConfiguration();
// 1、配置跨域
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
}
}
推荐阅读
-
基于Linux网关服务器squid配置过程详解
-
php使用ICQ网关发送手机短信
-
射手网关闭了怎么办?shooterdownloader字幕下载工具还能用(使用方法)
-
车联网关键技术研发或有滞后
-
Linux命令行修改IP、网关、DNS的方法
-
Spring Cloud系列-Zuul网关集成JWT身份验证
-
SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]
-
微服务架构下使用Spring Cloud Zuul作为网关将多个微服务整合到一个Swagger服务上
-
AspNetCore网关集成Swagger访问使用IdentityServer保护的webapi项目
-
数秦科技揭秘保全网关于司法存证领域的心路历程