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页面,这在安全合规上通过不了

相关推荐
阿里云云原生4 天前
阿里云获评 Agentic AI 开发平台领导者,函数计算 AgentRun 赢下关键分!
云原生
阿里云云原生5 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生5 天前
当 AI Agent 接管手机:移动端如何进行观测
云原生·agent
阿里云云原生5 天前
AI 原生应用开源开发者沙龙·深圳站精彩回顾 & PPT下载
云原生
阿里云云原生5 天前
灵感启发:日产文章 100 篇,打造“实时热点洞察”引擎
云原生
1candobetter5 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
~莫子5 天前
Haproxy七层负载详解+实验详细代码
云原生
阿里云云原生5 天前
OpenTelemetry + 云监控 2.0:打造你的云原生全栈可观测
云原生
阿狸猿5 天前
云原生数据库
云原生·软考
至此流年莫相忘5 天前
Kubernetes实战篇之配置与存储
云原生·容器·kubernetes