【SpringCloud】集群下 Eureka 的启动 bug,大坑

在配置Eureka Server集群并引入SpringSecurity时,遇到注册失败、心跳丢失等错误,原因是SpringCloud 2.0以上默认启用了csrf保护。解决方法是在每个Eureka Server中自定义WebSecurityConfigurer,通过忽略/eureka/**的csrf检查或完全禁用csrf。具体实现包括两种方案:一是忽略特定路径,二是关闭整个CSRF防御机制。
摘要由CSDN通过智能技术生成

如题,笔者创建了两个 Eureka Server 做集群,并引入了 Spring Security 在启动这两个 Server 就出现错误,会有各种 bug,比如

There was a problem with the instance info replicator com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

DiscoveryClient_EUREKA-SERVER/192.168.0.27:8762 - registration failed Cannot execute request on any known server

DiscoveryClient_EUREKA-SERVER/192.168.0.27:8762 - was unable to send heartbeat!

Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://root:123456@localhost:8761/eureka/}

Root name 'timestamp' does not match expected ('instance') for type [simple type, class com.netflix.appinfo.InstanceInfo]

查阅了大量的博客,原因大概是 集群中每个 Eureka Server 会把自己作为客户端 向其他的 Server 注册,当我们引入 Spring Security 的依赖时候,Spring Cloud 2.0 以上会默认启用 csrf 检验,需要在 Eureka Server 端配置Security 的 csrf 检验为 false,否则,会出现其他服务无法注册进 Eureka 注册中心的情况,就是一大堆 bug,我被这个 bug 卡了一天半了!

什么是 csrf:csrf 则通过伪装来自受信任用户的请求来利用受信任的网站

具体的解决办法:在每个 Eureka Server 端新建一个WebSecurityConfigurerAdapter 的继承类,并加上 @EnableWebSecurity 注解。

这里又有两种解决方案,可根据需要具体选择。

方案一:让 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();
    }
}

在这里插入图片描述
即可解决问题!快去试试吧!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风落_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值