SpringCloud之开启Eureka密码认证

本文档介绍了如何在Eureka注册中心启用密码认证,以保护服务注册的安全。首先,通过引入Spring Boot Starter Security依赖来开启安全功能。接着,配置了安全用户信息,并针对Spring Security的不同版本提供了相应的配置类代码。最后,展示了如何在服务端配置以使用认证后的用户名和密码进行服务注册。这为Eureka提供了一层基础的安全防护。
摘要由CSDN通过智能技术生成

突然想起来,一直都是直接注册服务到eureka
在实际开发中,注册中心是有有公网 IP 的,别人都可以把自己的服务注册到我的eureka中
在这里插入图片描述
于是了解到可以给eureka设置密码认证。

一、Eureka开启密码认证

1.引入依赖

<!--引入security-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2. 配置文件

spring:
  security:
    user:
      name: admin
      password: 123456

3. 配置类

看了网上一堆都是通过继承WebSecurityConfigurerAdapter来实现的。
一试发现这个类在Spring Security 5.7.1及更新版本或者Spring Boot 2.7.0及更新版本已经被弃用了。
在这里插入图片描述

Spring Security 5.6.5及更旧版本或Spring Boot 2.6.8

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭csrf
        http.csrf().disable();
        // 支持httpBasic
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

Spring Security 5.7.1及更新版本或者Spring Boot 2.7.0及更新版本

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().disable();
        //支持httpBasic
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
        return http.build();
    }
}

二、注册服务

修改配置文件,将服务注册到开启了密码认证的eureka中心。

secutiry:
	user:
		name: admin
      	password: 123456

eureka:
  client:
    service-url:
      defaultZone: http://${secutiry.user.name}:${secutiry.user.password}@localhost:8086/eureka
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值