Eureka客户端注册失败:Cannot execute request on any known server

1、问题

spring boot启动异常如下:

Cannot execute request on any known server

2、原因分析

开启Eureka安全Security认证后,security默认加上了 csrf 拦截,

applicaton.yml

server:
  port: 8888
spring:
  application:
    name: eureka-server
  security:
    user:
      name: admin
      password: admin
eureka:
  instance:
    hostname: 127.0.0.1
    preferIpAddress: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    defaultZone: http://127.0.0.1:8888/eureka/
  server:
      waitTimeInMsWhenSyncEmpty: 0
      peer-eureka-nodes-update-interval-ms: 10000
      enableSelfPreservation: false

pom.xml


    2.2.2.RELEASE
    
    

    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server
    ${eureka-server.version}

 3、解决方案

        在注册中心,重写WebSecurityConfigurerAdapter中得方法configure(),忽略对注册中心url相对路径/eureka的拦截。

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
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

 

你可能感兴趣的:(系统架构,Spring)