4.9 Gateway 网关以及跨域
1.创建 SpringBoot Module(qilemo-gateway) ,
Project Metadata
Group: com.qilemo
Artifact: qilemo-gateway
Type: Maven Project
Language: java
Packaging: jar
Java Version: 11
Version: 0.0.1-SNAPSHOT
Name: qilemo-gateway
Description: API网关
Package: com.qilemo.gateway
2.在 qilemo 聚合配置文件中添加新 module (pom.xml);
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qilemo</groupId>
<artifactId>qilemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qilemo</name>
<description>聚合服务</description>
<packaging>pom</packaging>
<modules>
<module>qilemo-product</module>
<module>qilemo-ware</module>
<module>qilemo-coupon</module>
<module>qilemo-member</module>
<module>qilemo-order</module>
<module>renren-fast</module>
<module>renren-generator</module>
<module>qilemo-common</module>
<module>qilemo-gateway</module>
</modules>
</project>
3.在 qilemo-gateway 下的配置文件(pom.xml)中引用 qilemo-common 依赖;
<dependency>
<groupId>com.qilemo</groupId>
<artifactId>qilemo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
4.开启注册发现,并且排除从 common 中引用的数据源相关的内容(QilemoGatewayApplication.java主程序),@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) ;
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class QilemoGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(QilemoGatewayApplication.class, args);
}
}
5.创建nacos注册发现(application.properties),nacos配置管理(boostrap.properties)配置文件
注册发现:application.properties
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.application.name=qilemo-gateway
server.port=88
配置管理:boostrap.properties
spring.cloud.nacos.config.server-addr=localhost:8848
spring.application.name=qilemo-gateway
#如果配置文件保存在线上nacos配置管理中,使用以下方法进行引用;
#spring.cloud.nacos.config.namespace=e6e1bdc5-d6eb-4137-8f1e-66ce78da6131
#spring.cloud.nacos.config.group=DEFAULT_GROUP
#
#spring.cloud.nacos.config.extension-configs[0].data-id=nacos-discovery
#spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
#spring.cloud.nacos.config.extension-configs[0].refresh=true
6.创建新的路由规则配置文件(application.yml);
路由规则:不管有多少条路由规则都需要遵守从小到大的原则,既下面的路径范围包含了上面的路径;最大的规则范围一定要放在最后面,如:所有来源于 /api/** 开头的请求,都转发到 /renren-fast/ 服务下;如果要添加新的路由规则,就需要在 admin-route 路由规则之上进行添加;
spring:
cloud:
gateway:
routes:
# 商品服务路由
- id: product-route
uri: lb://qilemo-product
predicates:
- Path=/api/product/**
filters:
- RewritePath=/api/(?<segment>.*), /$\{segment}
# admin全局路由,所有api开头的请求都转发到renren-fast
- id: admin-route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment}
7.跨源资源共享(CORS)
出于安全性,浏览器限制脚本内发起的跨源HTTP请求。不允许访问网段不相同下的资源,所以如果想要在不同的微服务之间进行通讯,就需要使用到 CORS 跨域资源共享;
7.1在网关微服务下创建Cors配置类(config.QilemoCorsConfiguration.java);
进行跨域配置:
@Configuration
public class QilemoCorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration configuration = new CorsConfiguration();
//1.跨域配置
configuration.addAllowedHeader("*");//允许那些请求头跨域,"*"表示所有头
configuration.addAllowedMethod("*");//允许那些请求方式跨域,"*"表示所有方式
// configuration.addAllowedOrigin("*");//如果使用这条语句配置的请求来源在运行时提示错误,请使用下面的方式;
configuration.addAllowedOriginPattern("*");//允许那个请求来源跨域,"*"表示所有来源
configuration.setAllowCredentials(true);//是否允许携带cookie进行跨域,true:表示允许。
source.registerCorsConfiguration("/**",configuration);
return new CorsWebFilter(source);
}
}
跨域配置类配置好后,提示如下错误:使用上面的代码;
java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
7.2.测试页面登陆是否成功,
如果登陆时依然提示错误,错误内容为:有多个请求头;需要将 renren-fast 后端脚手架中配置的 Cors跨域配置类的内容注释掉,使用上面我们自己配置的 Cors跨域配置。
@Configurationpublic class CorsConfig implements WebMvcConfigurer {// @Override// public void addCorsMappings(CorsRegistry registry) {// registry.addMapping("/**")// .allowedOrigins("*")// .allowCredentials(true)// .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")// .maxAge(3600);// }}