apollo内置eureka dashboard授权登录

发布于:2025-02-10 ⋅ 阅读:(28) ⋅ 点赞:(0)

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

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

  2. 确保 username 和 password 配置正确:你需要确保在配置文件中正确设置了 apollo.eureka.server.security.username 和 apollo.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页面,这在安全合规上通过不了


网站公告

今日签到

点亮在社区的每一天
去签到