当前位置:   article > 正文

【SpringCloud】集群下 Eureka 的启动 bug,大坑_spring eureka漏洞

spring eureka漏洞

如题,笔者创建了两个 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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

查阅了大量的博客,原因大概是 集群中每个 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/** 的所有请求
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

方案二:密码验证依然开启,仅仅关闭 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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/717673
推荐阅读
相关标签
  

闽ICP备14008679号