【SpringCloud】Eureka 安全认证

本文介绍了如何在Eureka注册中心启用安全认证,包括添加Spring Security依赖,配置安全认证信息,并修改服务提供者和服务消费者的配置以通过认证。同时,针对Eureka默认开启的CSRF防御机制可能导致的问题,提供了两种解决方案:一是忽略特定路径的CSRF检查,二是完全关闭CSRF防御。最后,文章提及成功配置后可以通过提供的用户名和密码访问注册中心,确保所有功能正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

Eureka 安全认证

1、添加依赖

注册中心添加依赖 security 依赖

<!-- spring security 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、配置文件

注册中心配置安全认证

spring:
  # 安全认证
  security:
    user:
      name: root
      password: 123456

3、修改访问集群节点的 url

注册中心的配置文件

server:
  port: 8761 # 端口

spring:
  application:
    name: eureka-server # 注册名称
  # 安全认证
  security:
    user:
      name: root
      password: 123456

# 配置 Eureka Server 注册中心
eureka:
  instance:
    hostname: eureka01 # 主机名,不配置的时候将根据操作系统的主机名来获取
    preferIpAddress: true # 是否使用 ip 地址注册
    instanceId: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    # 设置服务注册中心地址,指向另一个注册中心
    serviceUrl: # 注册中心对外暴露的注册地址
      defaultZone: http://root:123456@localhost:8762/eureka/

服务提供者的配置文件

server:
  port: 9090 # 端口

spring:
  application:
    name: service-consumer # 注册名称

# 配置 Eureka Server 注册中心
eureka:
  client:
    register-with-eureka: false # 是否将自己注册到注册中心,默认为 true
    registry-fetch-interval-seconds: 10 # 表示 Eureka Client 间隔多久去服务器拉取信息,默认为 30 秒
    service-url: # 设置服务注册中心地址
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/

服务消费者的配置文件

server:
  port: 7070 # 端口

spring:
  application:
    name: service-provider # 注册名称

# 配置 Eureka Server 注册中心
eureka:
  instance:
    prefer-ip-address: true # 是否使用 ip 地址注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    service-url: # 设置服务注册中心地址
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/

management:
  endpoints:
    web:
      exposure:
        include: shutdown # 开启 shutdown 访问
  endpoint:
    shutdown:
      enabled: true # 开启 shutdown 实现停服

4、过滤 CSRF

Eureka 会自动化配置 CSRF 防御机制,Spring Security 认为 POST,PUT,and DELETE http methods 都是有风险的,如果这些 method 发送过程中没有带上 CSRF token 的话,会被直接拦截并返回 403 forbidden。

官方给了解决办法,可以参考 spring cloud issue 2754,里面有大量的讨论,这里提供两种解决方法。

首先注册中心配置一个 @EnableWebSecurity 配置类,继承 WebSecurityConfigurerAdapter,然后重写 configure 方法。

方案一

让 CSRF 忽略 /eureka/** 的所有请求

package org.fengluo.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http); // 这句是为了访问 eureka 控制台和 /actuator 时能做安全控制
        http.csrf().ignoringAntMatchers("/eureka/**"); // 忽然 /eureka/** 的所有请求
    }
}
方案二

密码验证依然开启,仅仅关闭 CSRF 的防御机制

package org.fengluo.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 注意:这里如果直接 disable 的话,会把密码验证也关闭了
        http.csrf().disable().authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
    }
}

5、访问

image-20220501134817565

使用配置好的用户名和密码登录以后可以看到注册中心界面,启动服务提供者和服务消费者,功能正常使用!至此 Eureka 注册中心的所有知识点就学习结束了!

笔者后面会继续学习 Ribbon 负载均衡器,欢迎大家关注!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风落_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值