apollo内置eureka dashboard授权登录

要确保访问Eureka Server时要求输入账户和密码,需要确保以下几点:

  1. 确保 eurekaSecurityEnabled 配置为 true :这个配置项控制是否启用Eureka的安全认证。如果它被设置为 false,即使配置了用户名和密码,也不会启用安全认证。

  2. 确保 usernamepassword 配置正确 :你需要确保在配置文件中正确设置了 apollo.eureka.server.security.usernameapollo.eureka.server.security.password

  3. 优化 configure(HttpSecurity http) 方法 :当前的配置允许所有请求通过 permitAll(),即使启用了安全认证,某些路径仍然可以匿名访问。你需要调整这些路径的权限。

优化前的代码,此代码为apollo-configserver的2.x.x以上代码

复制代码
  @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
      http.httpBasic();
      if (eurekaSecurityEnabled) {
        http.authorizeRequests()
            .antMatchers("/eureka/apps/**", "/eureka/instances/**", "/eureka/peerreplication/**")
            .hasRole(EUREKA_ROLE)
            .antMatchers("/**").permitAll();
      }
    }

如果不修改访问eureka dashboard时,直接进入到dashboard界面,安全合规审核不过,要求增加账户审核。

优化后的代码:

复制代码
  @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
      http.httpBasic();
      if (eurekaSecurityEnabled) {
        http.authorizeRequests()
            .antMatchers(
                            "/",
                             "/eureka/apps/**",
                              "/eureka/instances/**",
                               "/eureka/peerreplication/**"
                            )
            .hasRole(EUREKA_ROLE)
            .antMatchers("/**").permitAll();
      }
    }

application.yml添加如下内容:

复制代码
apollo:
  eureka:
    server:
      security:
        username: demo1
        password: demo1
        enabled: true

修改的作用

  1. 根路径 (/) 需要认证

    • 当用户访问根路径(例如 http://localhost:8080/)时,会要求输入用户名和密码。

    • 满足合规要求,用户访问Eureka Dashboard时进行身份验证。

  2. Eureka相关路径需要认证

    • /eureka/apps/**/eureka/instances/**/eureka/peerreplication/** 这些路径仍然需要认证。
  3. 其他路径允许匿名访问/** 表示所有其他路径允许匿名访问。如果希望所有路径都需要认证,可以将 /** 改为 .anyRequest().authenticated()。代码如下修改:

    复制代码
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.httpBasic();
        if (eurekaSecurityEnabled) {
            http.authorizeRequests()
                .antMatchers(
                              "/", 
                              "/eureka/apps/**",
                              "/eureka/instances/**",
                              "/eureka/peerreplication/**"
                             )
                .hasRole(EUREKA_ROLE) 
                .anyRequest().authenticated(); 
        } else {
            http.authorizeRequests()
                .anyRequest().permitAll(); 
        }
    }

    增加项动启:执行后,如下:不然就直接进入到dashboard页面,这在安全合规上通过不了

相关推荐
艾伦_耶格宇11 小时前
【DOCKER进阶】-3 cgroups
docker·容器·eureka
阿里云云原生16 小时前
RCA 决定 AgenticOps 的行动方向:STAROps 如何把根因定位做成系统能力
云原生
仍然.16 小时前
SpringCloud---Eureka介绍
java·spring cloud·eureka
键盘鼓手苏苏19 小时前
AI 内容生成管线设计:从多模态编排到质量守门的生产级架构
云原生·kubernetes·k8
Henry-SAP21 小时前
具身智能新突破:开源生态与IPO热潮
人工智能·云原生·sap·erp
Kismet_nvi1 天前
Kubernetes 核心模块详细总结
云原生·容器·kubernetes
微擎应用市场1 天前
微擎面板 W7Panel:一站式云原生管理平台,让 Kubernetes 触手可及
云原生·容器·kubernetes
张洛闻Eren1 天前
云原生k8s【第一课】: Docker 容器技术
运维·云原生·容器·k8s
聊浮游1 天前
运维2026全套最新学习资料
运维·微服务·云原生·容器
xiaoxiangsiyan2 天前
RHCE2026云原生路线EX188和EX288完整备考指南
运维·网络·云原生·自动化