Spring Cloud Gateway集成Actuator、prometheus监控方案以及相关安全漏洞的解决方案

需求说明

需要在prometheus中对gateway服务进行监控,例如jvm相关指标,计划gateway服务引入spring-boot-starter-actuator和micrometer-registry-prometheus,给prometheus提供端点获取指标数据。

依赖及配置

在gateway服务添加依赖及配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
management:
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    export:
      prometheus:
        enabled: true #开启prometheus端点

启动网关服务即可查看监控信息

通过actuator可以看到我们开放了很多端点,能获取到很多监控信息,但同时也存在安全风险,例如通过/actuator/env可以获取到服务的环境变量,通过actuator/gateway/routes可以查看路由信息,甚至可以修改路由,这些都是非常危险的操作;下边我们将对这些安全风险进行优化。

安全风险修复

风险修复可以有有以下三点:

1.把不需要的端点关掉

2.修改base-path

3.增加配置访问权限验证

针对1和2是在配置文件中修改相应配置,3我这里是用filter来拦截请求做验证。

把不需要的端点关掉,修改base-path,在配置文件中修改:

management:
  endpoints:
    web:
      base-path: /newPath  #修改base-path
      exposure:
        include: "*"
        exclude: env,beans   #关掉不需要的端点
  metrics:
    export:
      prometheus:
        enabled: true  #开启prometheus端点

修改完重启网关服务,访问新端点获取监控数据

验证被关掉的端点已不存在

增加配置访问权限验证:

在gateway中我们通过WebFlux中的WebFilter来实现过滤,对监控相关的请求做验证

import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Component
public class CustomAuthenticationWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
       // 获取请求路径
       String path = exchange.getRequest().getURI().getPath();
       // 是否Actuator请求,判断请求路径是否包含newPath
       if(!path.contains("newPath")){
          return chain.filter(exchange);
       }
       String token = exchange.getRequest().getHeaders().getFirst("Actuator-Authorization");
       // 验证Token
       // 如果验证成功,则继续传递请求
       if (token != null && token.equals("xxxxxxxxxxxxx")) {
          return chain.filter(exchange);
       } else {
          // 如果验证失败,则返回401 Unauthorized
          return Mono.error(new RuntimeException("Unauthorized"));
       }
    }
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebFilterConfiguration {
    @Bean
    public CustomAuthenticationWebFilter customAuthenticationWebFilter() {
       return new CustomAuthenticationWebFilter();
    }
}

以上代码我们是在请求头中设置约定的token信息,在filter中做验证,没有token 信息或者与约定的token值不一致则认定非法调用。

验证带约定token调用,可以返回正常数据:

验证不带token调用,提示Unauthorized:

### Spring Cloud Gateway 集成 Prometheus监控配置示例 为了实现 Spring Cloud GatewayPrometheus集成,可以按照以下方法完成设置和配置: #### 1. 添加依赖项 在项目的 `pom.xml` 文件中引入必要的依赖项。这些依赖项包括 Spring Boot Actuator 和 Micrometer 支持 Prometheus 的库。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> ``` 上述代码片段展示了如何通过 Maven 构建工具来添加所需的依赖项[^3]。 #### 2. 启用端点暴露 修改 `application.yml` 或 `application.properties` 文件以启用 `/actuator/prometheus` 端点并允许外部访问。 对于 YAML 格式的配置文件,如下所示: ```yaml management: endpoints: web: exposure: include: "prometheus,health" metrics: export: prometheus: enabled: true ``` 此部分配置启用了 Prometheus 数据导出功能以及健康检查接口的公开[^4]。 #### 3. 自定义指标(可选) 如果需要自定义网关特定的行为或路径统计信息,则可以通过创建 Bean 来扩展默认行为。例如,下面是一个简单的计数器例子: ```java import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Component; @Component public class CustomMetrics { private final Counter requestCounter; public CustomMetrics(MeterRegistry registry) { this.requestCounter = Counter.builder("custom.requests.total") .description("Total number of custom requests.") .register(registry); } public void increment() { requestCounter.increment(); } } ``` 在此 Java 类中实现了对请求次数进行跟踪的功能[^5]。 #### 4. 测试与验证 启动应用程序后,在浏览器或者命令行工具里打开地址 http://localhost:<port>/actuator/prometheus ,应该能看到类似这样的输出数据: ``` # HELP jvm_memory_used_bytes The amount of used memory # TYPE jvm_memory_used_bytes gauge jvm_memory_used_bytes{area="nonheap",id="CodeHeap 'profiled nmethods'",} 8.70912e+06 ... ``` 这表明已经成功设置了基本的度量标准收集机制[^6]。 --- ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值