spring: application: name: crm-gateway cloud: gateway: routes: - uri: lb://auth-service #微服务名字 predicates: - Path=/crm/auth/** #所有以/auth开头的请求转发到auth-service微服务 filters: - StripPrefix=2 #去掉请求路径中的第1和第2个单词(/crm/auth) - uri: lb://clue-service predicates: - Path=/crm/clues/** filters: - StripPrefix=1 - uri: lb://business-service predicates: - Path=/crm/business/** filters: - StripPrefix=1转换成properties
时间: 2024-02-14 10:25:47 浏览: 156
spring.application.name=crm-gateway
spring.cloud.gateway.routes[0].uri=lb://auth-service
spring.cloud.gateway.routes[0].predicates[0].Path=/crm/auth/**
spring.cloud.gateway.routes[0].filters[0].StripPrefix=2
spring.cloud.gateway.routes[1].uri=lb://clue-service
spring.cloud.gateway.routes[1].predicates[0].Path=/crm/clues/**
spring.cloud.gateway.routes[1].filters[0].StripPrefix=1
spring.cloud.gateway.routes[2].uri=lb://business-service
spring.cloud.gateway.routes[2].predicates[0].Path=/crm/business/**
spring.cloud.gateway.routes[2].filters[0].StripPrefix=1
相关问题
spring: application: name: jsh-gateway cloud: gateway: routes: - uri: lb://jsh-auth predicates: - Path=/jshERP-boot/user/login,/jshERP-boot/user/logout filters: - StripPrefix=2 - uri: lb://jsh-system predicates: - Path=/** discovery: locator: enabled: true转换成properties
spring.application.name=jsh-gateway
spring.cloud.gateway.routes[0].uri=lb://jsh-auth
spring.cloud.gateway.routes[0].predicates[0]=Path=/jshERP-boot/user/login,/jshERP-boot/user/logout
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=2
spring.cloud.gateway.routes[1].uri=lb://jsh-system
spring.cloud.gateway.routes[1].predicates[0]=Path=/**
spring.cloud.gateway.discovery.locator.enabled=true
spring cloud gateway实现统一认证
Spring Cloud Gateway可以通过集成Spring Security实现统一认证。下面是步骤:
1. 集成Spring Security
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
```
2. 配置认证
在application.yml中配置Spring Security和Gateway的认证方式,如下所示:
```yaml
spring:
security:
user:
name: user
password: password
cloud:
gateway:
routes:
- id: auth-service
uri: lb://auth-service
predicates:
- Path=/auth/**
filters:
- StripPrefix=1
- name: AuthFilter
args:
type: basic
```
其中,auth-service是认证服务的名称,lb://auth-service是认证服务的负载均衡地址,/auth/**是Gateway的路由规则,AuthFilter是自定义的认证过滤器。
3. 实现认证过滤器
AuthFilter继承GlobalFilter并实现filter方法,实现认证逻辑,如下所示:
```java
@Component
public class AuthFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String type = exchange.getRequest().getQueryParams().getFirst("type");
if ("basic".equals(type)) {
return chain.filter(exchange);
}
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
}
```
该过滤器根据请求参数type判断认证方式,如果是basic则放行,否则返回401错误。
4. 启动认证服务
最后,启动认证服务,即可实现统一认证。
以上就是使用Spring Cloud Gateway实现统一认证的步骤。
阅读全文