Eureka 与Spring Boot Admin配合,搭建客户端界面健康检查

Eureka 与Spring Boot Admin配合使用:

在Eureka搭建完成的基础上,配置生产者或者消费者使用Spring Boot Admin进行客户端式健康检查:

application,yml配置修改:

#
#server:
#  port: 8001
#eureka:
#  instance:
#    prefer-ip-address: true # 注册服务时,使用服务的IP地址
#  client:
#    service-url:
#      defaultZone: http://localhost:8761/eureka/
#spring:
#  application:
#    name: provider-ticket
​
server:
  port: 8001
spring:
  application:
    name: SpringBootAdmin
  security:
    user:
      name: sadmin
      password: 123456
  boot:
    admin:
      ui:
        title: SpringBootAdmin
​
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
eureka:
  instance:
    metadata-map:
      user.name: sadmin
      user.password: 123456
    easeRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    ip-address: 127.0.0.1
    prefer-ip-address: true
    instance-id: ${eureka.instance.ip-address}:${server.port}
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

pom.xml依赖修改:

//额外加入这两个依赖
<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.6</version>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

application.java 启动类的修改:

package com.ellisonpei.providerticket;
​
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
​
/**
 * @author pei
 */
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class ProviderTicketApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(ProviderTicketApplication.class, args);
    }
​
    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;
​
        public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }
​
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");
​
            http.authorizeRequests()
                    .antMatchers(adminContextPath + "/assets/**").permitAll()
                    .antMatchers(adminContextPath + "/login").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                    .logout().logoutUrl(adminContextPath + "/logout").and()
                    .httpBasic().and()
                    .csrf()
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                    .ignoringAntMatchers(
                            adminContextPath + "/instances",
                            adminContextPath + "/actuator/**"
                    );
        }
    }
}
​

启动测试:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Eureka 未授权访问漏洞是由于 Eureka Server 没有进行权限验证导致的,攻击者可以利用该漏洞获取 Eureka Server 的敏感信息。要修复该漏洞,可以采取以下措施: 1. 在 Eureka Server 中配置安全认证,限制访问权限。 2. 升级 Spring Cloud 版本,Spring Cloud Edgware 及以上版本中已经修复了该漏洞。 3. 禁用 Eureka Server 的 web 界面,只提供 API 调用方式。 4. 在 Eureka Server 的访问日志中监控未授权的访问行为,及时发现并阻止攻击。 具体操作步骤如下: 1. 在 Eureka Server 中添加 Spring Security 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 在 Eureka Server 的配置文件中添加以下配置: ```yaml # 开启安全认证 security: basic: enabled: true user: name: admin password: password # 配置访问权限 eureka: client: register-with-eureka: false fetch-registry: false server: enable-self-preservation: false wait-time-in-ms-when-sync-empty: 0 eviction-interval-timer-in-ms: 30000 response-cache-auto-expiration-in-seconds: 30 enable-self-preservation-when-should-not: false instance: hostname: localhost prefer-ip-address: true ``` 其中,security.basic.enabled 配置项开启基本认证,security.user.name 和 security.user.password 配置项指定了管理员的用户名和密码。eureka.client 和 eureka.server 配置项关闭了 Eureka Client 和 Eureka Server 的注册与发现功能,只开放了 Eureka Server 的 API。instance.hostname 和 instance.prefer-ip-address 配置项指定了 Eureka Server 的主机名和 IP 地址。 3. 重启 Eureka Server。 通过以上步骤,就可以对 Eureka Server 进行安全认证和访问权限控制,避免了未授权访问漏洞。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值