集成 Spring Cloud Gateway

博客介绍了 hai-gateway 服务端的配置,包括引入依赖、添加配置以及在启动类加注解。还提及 hai-app 客户端注册到 Eureka,且可通过 gateway 进行访问,给出了访问地址。

hai-gateway 服务端
引入依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

添加配置

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: haiproduct  # 路由的ID,没有固定规则,但要求唯一,建议配合服务名
          # uri: http://localhost:8200  # 单个匹配
          uri: lb://HAI-PRODUCT # 负载均衡,可以访问 8200/8201
          predicates:
            - Path=/product/**  # 断言,根据路径路由  http://localhost:7000/product
        - id: haiapp  # 路由的ID,没有固定规则,但要求唯一,建议配合服务名
          uri: lb://HAI-APP # 负载均衡,可以访问 8080/8081
          predicates:
            - Path=/**  # 断言,根据路径路由    http://localhost:7000

启动类加上注解

@EnableEurekaClient
@SpringBootApplication

**hai-app 客户端注册到 Eureka **
可以通过 gateway 访问 hai-app:
http://localhost:7000

### 集成 Spring Cloud GatewaySpring Security 为了实现安全网关配置,在 Spring Boot 项目中集成 Spring Cloud GatewaySpring Security 是一种常见做法。这不仅能够保护 API 路由的安全性,还能提供统一的身份验证和授权机制。 #### 添加依赖项 首先,在 `pom.xml` 文件中添加必要的 Maven 依赖来引入 Spring Cloud GatewaySpring Security: ```xml <dependencies> <!-- Spring Cloud Gateway --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> </dependencies> ``` 上述代码片段展示了如何通过 Maven 来管理项目的依赖关系并导入所需的库文件[^1]。 #### 安全配置类定义 创建一个新的 Java 类用于自定义安全策略,该类需继承 `WebSecurityConfigurerAdapter` 或者采用基于函数的方式进行更简洁的设置。这里推荐使用最新的方式——即不继承任何基类而是直接利用 lambda 表达式的灵活性来进行配置: ```java import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.web.server.SecurityWebFilterChain; @EnableWebFluxSecurity public class SecurityConfig { @Bean public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) { return http .authorizeExchange(exchanges -> exchanges .pathMatchers("/actuator/**").permitAll() // 允许访问监控端点 .anyExchange().authenticated()) // 所有其他请求都需要认证 .oauth2ResourceServer(OAuth2ResourceServerSpec::jwt) // 使用 JWT 进行 OAuth2 认证 .build(); } } ``` 这段代码实现了对不同路径模式下的权限控制逻辑,并启用了 JSON Web Token (JWT) 的支持作为 OAuth2 协议的一部分。 #### 应用属性调整 最后一步是在应用程序的主要配置文件 (`application.yml`) 中指定一些额外参数以适应新的安全性需求: ```yaml spring: cloud: gateway: routes: - id: secured_api_route uri: lb://SECURED-SERVICE predicates: - Path=/api/** filters: - StripPrefix=1 security: oauth2: resourceserver: jwt: issuer-uri: https://example.com/issuer ``` 此部分设置了路由规则以及指定了颁发者的 URI 地址以便于解析传入令牌中的签名信息。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值