mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-09 00:45:27 +08:00
增加 spring cloud gateway 网关的示例
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo05-custom-gateway-filter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.filter;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthGatewayFilterFactory.Config> {
|
||||
|
||||
public AuthGatewayFilterFactory() {
|
||||
super(AuthGatewayFilterFactory.Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
// token 和 userId 的映射
|
||||
Map<String, Integer> tokenMap = new HashMap<>();
|
||||
tokenMap.put("yunai", 1);
|
||||
|
||||
// 创建 GatewayFilter 对象
|
||||
return new GatewayFilter() {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
// 获得 token
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
String token = headers.getFirst(config.getTokenHeaderName());
|
||||
|
||||
// 如果没有 token,则不进行认证。因为可能是无需认证的 API 接口
|
||||
if (!StringUtils.hasText(token)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
// 进行认证
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
Integer userId = tokenMap.get(token);
|
||||
|
||||
// 通过 token 获取不到 userId,说明认证不通过
|
||||
if (userId == null) {
|
||||
// 响应 401 状态码
|
||||
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
// 响应提示
|
||||
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap("认证不通过".getBytes());
|
||||
return response.writeWith(Flux.just(buffer));
|
||||
}
|
||||
|
||||
// 认证通过,将 userId 添加到 Header 中
|
||||
request = request.mutate().header(config.getUserIdHeaderName(), String.valueOf(userId))
|
||||
.build();
|
||||
return chain.filter(exchange.mutate().request(request).build());
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
|
||||
private static final String DEFAULT_TOKEN_HEADER_NAME = "token";
|
||||
private static final String DEFAULT_HEADER_NAME = "user-id";
|
||||
|
||||
private String tokenHeaderName = DEFAULT_TOKEN_HEADER_NAME;
|
||||
private String userIdHeaderName = DEFAULT_HEADER_NAME;
|
||||
|
||||
public String getTokenHeaderName() {
|
||||
return tokenHeaderName;
|
||||
}
|
||||
|
||||
public String getUserIdHeaderName() {
|
||||
return userIdHeaderName;
|
||||
}
|
||||
|
||||
public Config setTokenHeaderName(String tokenHeaderName) {
|
||||
this.tokenHeaderName = tokenHeaderName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Config setUserIdHeaderName(String userIdHeaderName) {
|
||||
this.userIdHeaderName = userIdHeaderName;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
# 默认过滤器,对应 FilterDefinition 数组
|
||||
default-filters:
|
||||
- name: Auth
|
||||
args:
|
||||
token-header-name: access-token
|
||||
@@ -0,0 +1,29 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
# 默认过滤器,对应 FilterDefinition 数组
|
||||
default-filters:
|
||||
- name: Auth
|
||||
args:
|
||||
token-header-name: access-token
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
<module>labx-08-sc-gateway-demo04</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo05-custom-gateway-filter</module>
|
||||
|
||||
<module>labx-08-sc-user-service</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user