提交 dc409e8b 编写于 作者: 木下藤吉郎's avatar 木下藤吉郎

1234

上级 48c000ff
......@@ -30,6 +30,8 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'org.apache.commons:commons-pool2'
}
dependencyManagement {
......
package com.zkcloud.gatewayserver.config;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;
@Configuration
public class KeyResolverConfig {
/**
* 根据URL限流
*
* @return KeyResolver
*/
@Bean
public KeyResolver pathKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
}
}
package com.zkcloud.gatewayserver.filter;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
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.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class AuthTokenGlobalFilter implements GlobalFilter, Ordered {
@Override
public int getOrder() {
// 值越小,优先级越高
return 0;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String accessToken = request.getQueryParams().getFirst("access-token");
if (!StringUtils.equalsIgnoreCase(accessToken, "123")) {
ServerHttpResponse response = exchange.getResponse();
String redirectUrl = "http://localhost:8001/index.html";
response.getHeaders().set(HttpHeaders.LOCATION, redirectUrl);
response.setStatusCode(HttpStatus.SEE_OTHER);
response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
return response.setComplete();
}
return chain.filter(exchange);
}
}
......@@ -3,6 +3,8 @@ server:
spring:
application:
name: gateway-server
profiles:
active: test
cloud:
config:
discovery:
......@@ -10,10 +12,13 @@ spring:
service-id: config-center
name: common
label: dev
profiles:
active: test
thymeleaf:
cache: false
gateway:
discovery:
locator:
# 根据服务名转发
enabled: true
# 服务名一律转小写
lower-case-service-id: true
eureka:
instance:
hostname: localhost
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册